Line data Source code
1 : import type { BillingPlansData } from '@comfyorg/account-core/billing'
2 :
3 : import type {
4 : BillingPlansResponse,
5 : Plan,
6 : TeamCreditStops
7 : } from '@comfyorg/billing-core'
8 :
9 : import { asSafeNumber } from './safeInt64'
10 :
11 : type DecodedPlan = BillingPlansData['plans'][number]
12 : type DecodedFeatureItem = NonNullable<DecodedPlan['feature_items']>[number]
13 : type DecodedStops = NonNullable<BillingPlansData['team_credit_stops']>
14 : type DecodedStop = DecodedStops['stops'][number]
15 : type FeatureItem = NonNullable<Plan['feature_items']>[number]
16 : type StopPrice = TeamCreditStops['stops'][number]['monthly']
17 :
18 : /**
19 : * The generated zod schema coerces every int64 in the catalog to `bigint` —
20 : * prices, credits, seat caps, the credit-stop ladder — while the generated
21 : * type the pricing surfaces read them through says `number`. Each is read
22 : * back as a number when a number holds it exactly; one that does not fails
23 : * the whole projection, so a catalog is never published with a rounded price.
24 : * Nothing is filtered, ranked or priced here.
25 : */
26 : /** An absent grant stays absent; one a number cannot hold exactly fails the plan. */
27 0 : function projectCredits(
28 : raw: DecodedPlan['credits']
29 : ): Pick<Plan, 'credits'> | undefined {
30 0 : if (raw === undefined) return {}
31 0 : const credits = asSafeNumber(raw)
32 0 : return credits === undefined ? undefined : { credits }
33 : }
34 :
35 0 : function projectListPrice(
36 : raw: DecodedPlan['list_price_cents']
37 : ): Pick<Plan, 'list_price_cents'> | undefined {
38 0 : if (raw === undefined) return {}
39 0 : const list_price_cents = asSafeNumber(raw)
40 0 : return list_price_cents === undefined ? undefined : { list_price_cents }
41 : }
42 :
43 0 : function projectFeatureItem({
44 : params,
45 : ...item
46 : }: DecodedFeatureItem): FeatureItem {
47 0 : if (params === undefined) return item
48 0 : const count = asSafeNumber(params.count)
49 0 : return count === undefined ? item : { ...item, params: { count } }
50 : }
51 :
52 0 : function projectFeatureItems(
53 : raw: DecodedPlan['feature_items']
54 : ): Pick<Plan, 'feature_items'> {
55 0 : if (raw === undefined) return {}
56 0 : return { feature_items: raw.map(projectFeatureItem) }
57 : }
58 :
59 0 : function projectPlan({
60 : credits: rawCredits,
61 : feature_items: rawFeatureItems,
62 : list_price_cents: rawListPrice,
63 : ...plan
64 : }: DecodedPlan): Plan | undefined {
65 0 : const credits = projectCredits(rawCredits)
66 0 : const listPrice = projectListPrice(rawListPrice)
67 0 : const featureItems = projectFeatureItems(rawFeatureItems)
68 0 : const credits_cents = asSafeNumber(plan.credits_cents)
69 0 : const max_seats = asSafeNumber(plan.max_seats)
70 0 : const price_cents = asSafeNumber(plan.price_cents)
71 0 : const total_cost_cents = asSafeNumber(plan.seat_summary.total_cost_cents)
72 0 : const total_credits_cents = asSafeNumber(
73 : plan.seat_summary.total_credits_cents
74 : )
75 0 : if (
76 : credits === undefined ||
77 : listPrice === undefined ||
78 : credits_cents === undefined ||
79 : max_seats === undefined ||
80 : price_cents === undefined ||
81 : total_cost_cents === undefined ||
82 : total_credits_cents === undefined
83 : )
84 0 : return undefined
85 0 : return {
86 : ...plan,
87 : ...credits,
88 : ...listPrice,
89 : ...featureItems,
90 : credits_cents,
91 : max_seats,
92 : price_cents,
93 : seat_summary: {
94 : seat_count: plan.seat_summary.seat_count,
95 : total_cost_cents,
96 : total_credits_cents
97 : }
98 : }
99 : }
100 :
101 0 : function projectStopPrice(
102 : price: DecodedStop['monthly']
103 : ): StopPrice | undefined {
104 0 : const list_price_cents = asSafeNumber(price.list_price_cents)
105 0 : const price_cents = asSafeNumber(price.price_cents)
106 0 : const period_charge_cents = asSafeNumber(price.period_charge_cents)
107 0 : if (
108 : list_price_cents === undefined ||
109 : price_cents === undefined ||
110 : period_charge_cents === undefined
111 : )
112 0 : return undefined
113 0 : return { ...price, list_price_cents, price_cents, period_charge_cents }
114 : }
115 :
116 0 : function projectStop(
117 : stop: DecodedStop
118 : ): TeamCreditStops['stops'][number] | undefined {
119 0 : const credits = asSafeNumber(stop.credits)
120 0 : const monthly = projectStopPrice(stop.monthly)
121 0 : const yearly = projectStopPrice(stop.yearly)
122 0 : if (credits === undefined || monthly === undefined || yearly === undefined)
123 0 : return undefined
124 0 : return { id: stop.id, credits, monthly, yearly }
125 : }
126 :
127 0 : function projectAll<T, U>(
128 : items: readonly T[],
129 : project: (item: T) => U | undefined
130 : ): U[] | undefined {
131 0 : const projected: U[] = []
132 0 : for (const item of items) {
133 0 : const result = project(item)
134 0 : if (result === undefined) return undefined
135 0 : projected.push(result)
136 : }
137 0 : return projected
138 : }
139 :
140 : /**
141 : * The SDK's decoded catalog in the shape the host's plan state holds, or
142 : * undefined when a value in it cannot be held exactly.
143 : */
144 0 : export function projectBillingPlans(
145 : data: BillingPlansData
146 : ): BillingPlansResponse | undefined {
147 0 : const { team_credit_stops, ...rest } = data
148 0 : const plans = projectAll(data.plans, projectPlan)
149 0 : if (plans === undefined) return undefined
150 0 : if (team_credit_stops === undefined) return { ...rest, plans }
151 0 : const stops = projectAll(team_credit_stops.stops, projectStop)
152 0 : if (stops === undefined) return undefined
153 0 : return {
154 : ...rest,
155 : plans,
156 : team_credit_stops: {
157 : default_stop_index: team_credit_stops.default_stop_index,
158 : stops
159 : }
160 : }
161 : }
|