Line data Source code
1 : import type { BillingStatusData } from '@comfyorg/account-core/billing'
2 :
3 : import type {
4 : BillingStatusResponse,
5 : RenewalInvoice,
6 : TeamCreditStopSummary
7 : } from '@comfyorg/billing-core'
8 :
9 : import { asSafeNumber } from './safeInt64'
10 :
11 : type DecodedCreditStop = NonNullable<BillingStatusData['team_credit_stop']>
12 : type DecodedRenewalInvoice = NonNullable<BillingStatusData['renewal_invoice']>
13 : type DecodedPeriodCreditGrant = NonNullable<
14 : BillingStatusData['period_credit_grant']
15 : >
16 : type DecodedNextCharge = NonNullable<BillingStatusData['next_charge']>
17 : type PeriodCreditGrant = NonNullable<
18 : BillingStatusResponse['period_credit_grant']
19 : >
20 : type NextCharge = NonNullable<BillingStatusResponse['next_charge']>
21 :
22 : /**
23 : * The credit stop's two int64 fields — a monthly credit count and a monthly
24 : * USD commitment — read back as the numbers the host holds; undefined when
25 : * either is past what a number can hold exactly.
26 : */
27 0 : function projectCreditStop(
28 : stop: DecodedCreditStop
29 : ): TeamCreditStopSummary | undefined {
30 0 : const credits_monthly = asSafeNumber(stop.credits_monthly)
31 0 : const stop_usd = asSafeNumber(stop.stop_usd)
32 0 : if (credits_monthly === undefined || stop_usd === undefined) return undefined
33 0 : return { id: stop.id, credits_monthly, stop_usd }
34 : }
35 :
36 0 : function projectRenewalInvoice(
37 : invoice: DecodedRenewalInvoice
38 : ): RenewalInvoice | undefined {
39 0 : const amount_due = asSafeNumber(invoice.amount_due)
40 0 : if (amount_due === undefined) return undefined
41 0 : return { ...invoice, amount_due }
42 : }
43 :
44 0 : function projectPeriodCreditGrant({
45 : credits: rawCredits,
46 : ...grant
47 : }: DecodedPeriodCreditGrant): PeriodCreditGrant | undefined {
48 0 : if (rawCredits === undefined) return grant
49 0 : const credits = asSafeNumber(rawCredits)
50 0 : return credits === undefined ? undefined : { ...grant, credits }
51 : }
52 :
53 0 : function projectNextCharge({
54 : amount: rawAmount,
55 : ...charge
56 : }: DecodedNextCharge): NextCharge | undefined {
57 0 : const amount = asSafeNumber(rawAmount)
58 0 : return amount === undefined ? undefined : { ...charge, amount }
59 : }
60 :
61 : /**
62 : * The SDK's decoded status in the shape the host's billing state holds, or
63 : * undefined when a value in it cannot be held exactly — a read the caller
64 : * reports as malformed rather than publishes rounded.
65 : */
66 0 : export function projectBillingStatus(
67 : status: BillingStatusData
68 : ): BillingStatusResponse | undefined {
69 0 : const {
70 : team_credit_stop,
71 : scheduled_change,
72 : renewal_invoice,
73 : period_credit_grant,
74 : next_charge,
75 : ...rest
76 : } = status
77 0 : const stop =
78 : team_credit_stop === null ? null : projectCreditStop(team_credit_stop)
79 0 : if (stop === undefined) return undefined
80 0 : const scheduledStop =
81 : scheduled_change === null || scheduled_change.team_credit_stop === null
82 : ? null
83 : : projectCreditStop(scheduled_change.team_credit_stop)
84 0 : if (scheduledStop === undefined) return undefined
85 0 : const invoice =
86 : renewal_invoice === undefined
87 : ? undefined
88 : : projectRenewalInvoice(renewal_invoice)
89 0 : const grant =
90 : period_credit_grant === undefined
91 : ? undefined
92 : : projectPeriodCreditGrant(period_credit_grant)
93 0 : const nextCharge =
94 : next_charge === null || next_charge === undefined
95 : ? next_charge
96 : : (projectNextCharge(next_charge) ?? null)
97 0 : return {
98 : ...rest,
99 : ...(invoice && { renewal_invoice: invoice }),
100 : ...(grant && { period_credit_grant: grant }),
101 : ...(nextCharge !== undefined && { next_charge: nextCharge }),
102 : team_credit_stop: stop,
103 : scheduled_change:
104 : scheduled_change === null
105 : ? null
106 : : { ...scheduled_change, team_credit_stop: scheduledStop }
107 : }
108 : }
|