Line data Source code
1 : /**
2 : * An SDK operation state as the record the legacy poller handed out. The
3 : * surfaces that look an operation up by id, or ask which one is waiting on the
4 : * customer, read these fields off `billingOperationStore` today; projecting
5 : * them here is what lets those call sites move to the rail without their
6 : * templates or their conditions changing.
7 : */
8 : import type { BillingOperationState } from '@comfyorg/account-core/billing'
9 : import { validateActionUrl } from '@comfyorg/account-core/billing'
10 :
11 : import type {
12 : BillingAuthenticationState,
13 : BillingOperationPhase
14 : } from '@comfyorg/billing-core'
15 :
16 : import { declineDetail } from './topupOperationView'
17 :
18 : type OperationKind = BillingOperationState['kind']
19 :
20 : type OperationStatus =
21 : | 'pending'
22 : | 'succeeded'
23 : | 'failed'
24 : | 'timeout'
25 : | 'reconciliation_needed'
26 :
27 : export interface BillingOperationRecordView {
28 : readonly opId: string
29 : readonly kind: OperationKind
30 : readonly workspaceId: string
31 : readonly status: OperationStatus
32 : readonly actionUrl: string | null
33 : readonly phase: BillingOperationPhase | null
34 : readonly authenticationState: BillingAuthenticationState | null
35 : readonly isAuthenticating: boolean
36 : readonly canRetryAuthentication: boolean
37 : /** The declined-attempt line a surface shows under its own summary. */
38 : readonly errorMessage: string | null
39 : /** True only when the server says cancelling this pending operation would take effect. */
40 : readonly cancelable: boolean
41 : }
42 :
43 : /**
44 : * `actionUrl` is validated here for the same reason the legacy store validates
45 : * it at registration: the surfaces that read this record hand the URL to
46 : * `window.open`.
47 : *
48 : * `superseded` has no counterpart in the legacy record: the scope moved on
49 : * under the operation, so there is nothing for this workspace's surfaces to
50 : * read — the same outcome `readOnRail` gives a superseded read.
51 : */
52 0 : export function projectOperationRecord(
53 : state: BillingOperationState
54 : ): BillingOperationRecordView | undefined {
55 0 : if (state.phase === 'superseded') return undefined
56 :
57 0 : const identity = {
58 : opId: state.id,
59 : kind: state.kind,
60 : workspaceId: state.scope.workspaceId
61 : } as const
62 :
63 0 : if (state.phase !== 'pending') {
64 0 : return {
65 : ...identity,
66 : status: state.phase === 'timed_out' ? 'timeout' : state.phase,
67 : actionUrl: null,
68 : phase: null,
69 : authenticationState:
70 : state.phase === 'reconciliation_needed'
71 : ? 'reconciliation_needed'
72 : : null,
73 : isAuthenticating: false,
74 : canRetryAuthentication: false,
75 : errorMessage: null,
76 : cancelable: false
77 : }
78 : }
79 :
80 0 : return {
81 : ...identity,
82 : status: 'pending',
83 : actionUrl: validateActionUrl(state.actionUrl) ?? null,
84 : phase: state.serverPhase ?? null,
85 : authenticationState: state.authenticationState ?? null,
86 : isAuthenticating: state.challenge?.status === 'in_progress',
87 : canRetryAuthentication: state.challenge?.status === 'required',
88 : errorMessage:
89 : state.authenticationState === 'failed_retryable'
90 : ? declineDetail(state.declineReason ?? 'authentication_failed')
91 : : null,
92 : cancelable: state.cancelable === true
93 : }
94 : }
|