hasConsoleBillingAccess function

bool hasConsoleBillingAccess({
  1. required bool isDisableCostWarnings(),
  2. required bool isNeomageAiSubscriber(),
  3. required AuthTokenSource getAuthTokenSource(),
  4. required bool hasApiKey(),
  5. required BillingConfig getGlobalConfig(),
})

Check if user has Console billing access.

Checks authentication state, subscriber status, and org/workspace roles to determine if the user can see billing information.

Implementation

bool hasConsoleBillingAccess({
  required bool Function() isDisableCostWarnings,
  required bool Function() isNeomageAiSubscriber,
  required AuthTokenSource Function() getAuthTokenSource,
  required bool Function() hasApiKey,
  required BillingConfig Function() getGlobalConfig,
}) {
  // Check if cost reporting is disabled via environment variable.
  if (isDisableCostWarnings()) {
    return false;
  }

  final isSubscriber = isNeomageAiSubscriber();

  // This might be wrong if user is signed into Max but also using an API key,
  // but we already show a warning on launch in that case.
  if (isSubscriber) return false;

  // Check if user has any form of authentication.
  final authSource = getAuthTokenSource();
  final apiKeyPresent = hasApiKey();

  // If user has no authentication at all (logged out), don't show costs.
  if (!authSource.hasToken && !apiKeyPresent) {
    return false;
  }

  final config = getGlobalConfig();
  final orgRole = config.oauthAccount?.organizationRole;
  final workspaceRole = config.oauthAccount?.workspaceRole;

  if (orgRole == null || workspaceRole == null) {
    return false; // hide cost for grandfathered users who have not re-authed
  }

  // Users have billing access if they are admins or billing roles at either
  // workspace or organization level.
  return ['admin', 'billing'].contains(orgRole) ||
      ['workspace_admin', 'workspace_billing'].contains(workspaceRole);
}