()
| 424 | * @returns The organization UUID or null if not authenticated |
| 425 | */ |
| 426 | export async function getOrganizationUUID(): Promise<string | null> { |
| 427 | // Check global config first to avoid unnecessary API call |
| 428 | const globalConfig = getGlobalConfig() |
| 429 | const orgUUID = globalConfig.oauthAccount?.organizationUuid |
| 430 | if (orgUUID) { |
| 431 | return orgUUID |
| 432 | } |
| 433 | |
| 434 | // Restored/dev builds may have token metadata populated without the full |
| 435 | // config write path. Prefer local token-derived values before making a |
| 436 | // profile request or giving up. |
| 437 | const cachedTokens = getClaudeAIOAuthTokens() |
| 438 | const tokenOrgUUID = |
| 439 | cachedTokens?.tokenAccount?.organizationUuid ?? |
| 440 | cachedTokens?.profile?.organization?.uuid |
| 441 | if (tokenOrgUUID) { |
| 442 | return tokenOrgUUID |
| 443 | } |
| 444 | |
| 445 | // Fall back to fetching from profile (requires user:profile scope) |
| 446 | const accessToken = cachedTokens?.accessToken |
| 447 | if (accessToken === undefined || !hasProfileScope()) { |
| 448 | return null |
| 449 | } |
| 450 | const profile = await getOauthProfileFromOauthToken(accessToken) |
| 451 | const profileOrgUUID = profile?.organization?.uuid |
| 452 | if (!profileOrgUUID) { |
| 453 | return null |
| 454 | } |
| 455 | return profileOrgUUID |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Populate the OAuth account info if it has not already been cached in config. |
no test coverage detected