|
| 1 | +import type { GraphQLFormattedError } from 'graphql'; |
| 2 | +import { config } from '../config/constants.ts'; |
| 3 | +import { requireAccessToken } from '../service/auth.svc.ts'; |
| 4 | +import { debugLogger } from '../service/log.svc.ts'; |
| 5 | +import { withRetries } from '../utils/retry.ts'; |
| 6 | +import { ApiError, type ApiErrorCode, isApiErrorCode } from './errors.ts'; |
| 7 | +import { completeUserSetupMutation, userSetupStatusQuery } from './gql-operations.ts'; |
| 8 | +import { getGraphQLErrors } from './graphql-errors.ts'; |
| 9 | +import { createApollo } from './nes.client.ts'; |
| 10 | + |
| 11 | +const USER_SETUP_MAX_ATTEMPTS = 3; |
| 12 | +const USER_SETUP_RETRY_DELAY_MS = 500; |
| 13 | + |
| 14 | +type UserSetupStatusResponse = { |
| 15 | + eol?: { |
| 16 | + userSetupStatus?: boolean; |
| 17 | + }; |
| 18 | +}; |
| 19 | + |
| 20 | +type CompleteUserSetupResponse = { |
| 21 | + eol?: { |
| 22 | + completeUserSetup?: boolean; |
| 23 | + }; |
| 24 | +}; |
| 25 | + |
| 26 | +const getGraphqlUrl = () => `${config.graphqlHost}${config.graphqlPath}`; |
| 27 | + |
| 28 | +function extractErrorCode(errors: ReadonlyArray<GraphQLFormattedError>): ApiErrorCode | undefined { |
| 29 | + const code = (errors[0]?.extensions as { code?: string })?.code; |
| 30 | + if (!code || !isApiErrorCode(code)) return; |
| 31 | + return code; |
| 32 | +} |
| 33 | + |
| 34 | +export async function getUserSetupStatus(): Promise<boolean> { |
| 35 | + const client = createApollo(getGraphqlUrl(), requireAccessToken); |
| 36 | + const res = await client.query<UserSetupStatusResponse>({ query: userSetupStatusQuery }); |
| 37 | + |
| 38 | + const errors = getGraphQLErrors(res); |
| 39 | + if (res?.error || errors?.length) { |
| 40 | + debugLogger('Error returned from userSetupStatus query: %o', res.error || errors); |
| 41 | + if (errors?.length) { |
| 42 | + const code = extractErrorCode(errors); |
| 43 | + if (code) { |
| 44 | + throw new ApiError(errors[0].message, code); |
| 45 | + } |
| 46 | + } |
| 47 | + throw new Error('Failed to check user setup status'); |
| 48 | + } |
| 49 | + |
| 50 | + const isComplete = res.data?.eol?.userSetupStatus; |
| 51 | + if (typeof isComplete !== 'boolean') { |
| 52 | + debugLogger('Unexpected userSetupStatus query response: %o', res.data); |
| 53 | + throw new Error('Failed to check user setup status'); |
| 54 | + } |
| 55 | + |
| 56 | + return isComplete; |
| 57 | +} |
| 58 | + |
| 59 | +export async function completeUserSetup(): Promise<boolean> { |
| 60 | + const client = createApollo(getGraphqlUrl(), requireAccessToken); |
| 61 | + const res = await client.mutate<CompleteUserSetupResponse>({ mutation: completeUserSetupMutation }); |
| 62 | + |
| 63 | + const errors = getGraphQLErrors(res); |
| 64 | + if (res?.error || errors?.length) { |
| 65 | + debugLogger('Error returned from completeUserSetup mutation: %o', res.error || errors); |
| 66 | + if (errors?.length) { |
| 67 | + const code = extractErrorCode(errors); |
| 68 | + if (code) { |
| 69 | + throw new ApiError(errors[0].message, code); |
| 70 | + } |
| 71 | + } |
| 72 | + throw new Error('Failed to complete user setup'); |
| 73 | + } |
| 74 | + |
| 75 | + const success = res.data?.eol?.completeUserSetup; |
| 76 | + if (!success) { |
| 77 | + debugLogger('completeUserSetup mutation returned unsuccessful response: %o', res.data); |
| 78 | + throw new Error('Failed to complete user setup'); |
| 79 | + } |
| 80 | + |
| 81 | + return success; |
| 82 | +} |
| 83 | + |
| 84 | +export async function ensureUserSetup(): Promise<void> { |
| 85 | + const isComplete = await withRetries('user-setup-status', () => getUserSetupStatus(), { |
| 86 | + attempts: USER_SETUP_MAX_ATTEMPTS, |
| 87 | + baseDelayMs: USER_SETUP_RETRY_DELAY_MS, |
| 88 | + }); |
| 89 | + if (isComplete) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + await withRetries('user-setup-complete', () => completeUserSetup(), { |
| 94 | + attempts: USER_SETUP_MAX_ATTEMPTS, |
| 95 | + baseDelayMs: USER_SETUP_RETRY_DELAY_MS, |
| 96 | + }); |
| 97 | +} |
0 commit comments