Line data Source code
1 : import type { AutoReloadSettings, AutoTopUpSetup } from '@comfyorg/ingest-types'
2 :
3 : import { canSetUpAutoTopUp } from '#/core/billing/billingTab/autoTopUpCard'
4 :
5 : export const CREDITS_PER_USD = 211
6 :
7 2356 : export const AUTO_TOP_UP_STARTING_THRESHOLD_CREDITS = 1_000
8 :
9 2356 : export const AUTO_TOP_UP_STARTING_LIMIT_CENTS = 1_000_000
10 :
11 : export interface AutoTopUpFormInput {
12 : readonly threshold: string
13 : readonly target: string
14 : readonly limit: string
15 : readonly limitOn: boolean
16 : }
17 :
18 : export interface AutoTopUpFormValues {
19 : readonly thresholdCredits: number | null
20 : readonly targetCredits: number | null
21 : readonly limitOn: boolean
22 : readonly budgetCents: number | null
23 : }
24 :
25 : /** The served rules from GET /api/billing/auto-reload the fields are checked against. */
26 : export interface AutoTopUpRules {
27 : readonly minReloadCredits: number
28 : readonly maxReloadCredits: number | null
29 : readonly maxMonthlyBudgetCents: number
30 : }
31 :
32 : export type AutoTopUpField = 'threshold' | 'target' | 'limit'
33 :
34 : export type AutoTopUpFieldError =
35 : | 'notWhole'
36 : | 'belowMinimum'
37 : | 'belowTarget'
38 : | 'aboveTarget'
39 : | 'invalid'
40 : | 'belowTopUp'
41 : | 'aboveCeiling'
42 :
43 : export type AutoTopUpErrors = Partial<
44 : Record<AutoTopUpField, AutoTopUpFieldError>
45 : >
46 :
47 : export interface AutoTopUpActivation {
48 : readonly threshold_credits: number
49 : readonly target_credits: number
50 : readonly monthly_budget_cents: number | null
51 : }
52 :
53 0 : function stripped(raw: string): string {
54 0 : const text = raw.replace(/[\s$]/g, '')
55 0 : return /^\d{1,3}(,\d{3})+(\.\d*)?$/.test(text) ? text.replace(/,/g, '') : text
56 : }
57 :
58 0 : function parseWhole(raw: string): number | null {
59 0 : const text = stripped(raw)
60 0 : const value = Number(text)
61 0 : return /^\d+$/.test(text) && Number.isSafeInteger(value) ? value : null
62 : }
63 :
64 0 : function parseCents(raw: string): number | null {
65 0 : const text = stripped(raw)
66 0 : return /^\d+(\.\d{1,2})?$/.test(text) ? Math.round(Number(text) * 100) : null
67 : }
68 :
69 0 : export function creditsToCents(credits: number): number {
70 0 : return (credits * 100) / CREDITS_PER_USD
71 : }
72 :
73 : /** Fields prefilled with the starting threshold and a target that adds the credits just picked. */
74 0 : export function startAutoTopUpForm(creditsPicked: number): AutoTopUpFormInput {
75 0 : return {
76 : threshold: String(AUTO_TOP_UP_STARTING_THRESHOLD_CREDITS),
77 : target: autoTopUpTargetFor(
78 : String(AUTO_TOP_UP_STARTING_THRESHOLD_CREDITS),
79 : creditsPicked
80 : ),
81 : limit: String(AUTO_TOP_UP_STARTING_LIMIT_CENTS / 100),
82 : limitOn: false
83 : }
84 : }
85 :
86 0 : export function autoTopUpTargetFor(
87 : threshold: string,
88 : creditsPicked: number
89 : ): string {
90 0 : const credits = parseWhole(threshold)
91 0 : return credits === null ? '' : String(credits + creditsPicked)
92 : }
93 :
94 0 : export function parseAutoTopUpForm(
95 : input: AutoTopUpFormInput
96 : ): AutoTopUpFormValues {
97 0 : return {
98 : thresholdCredits: parseWhole(input.threshold),
99 : targetCredits: parseWhole(input.target),
100 : limitOn: input.limitOn,
101 : budgetCents: input.limitOn ? parseCents(input.limit) : null
102 : }
103 : }
104 :
105 : /** What one top-up costs at least, as the server prices it when it checks the limit. */
106 0 : export function smallestTopUpCents(
107 : { thresholdCredits, targetCredits }: AutoTopUpFormValues,
108 : maxReloadCredits: number | null
109 : ): number | null {
110 0 : if (
111 : thresholdCredits === null ||
112 : targetCredits === null ||
113 : targetCredits <= thresholdCredits
114 : ) {
115 0 : return null
116 : }
117 0 : const reload = targetCredits - thresholdCredits
118 : const atMaximum = maxReloadCredits !== null && reload >= maxReloadCredits
119 0 : return Math.ceil(creditsToCents(atMaximum ? reload : reload + 1))
120 : }
121 :
122 : /** The dollar value of target minus threshold, rounded down to the cent, so "at least" stays true. */
123 0 : export function eachTopUpAtLeastCents({
124 : thresholdCredits,
125 : targetCredits
126 : }: AutoTopUpFormValues): number | null {
127 0 : if (
128 : thresholdCredits === null ||
129 : targetCredits === null ||
130 : targetCredits <= thresholdCredits
131 : ) {
132 0 : return null
133 : }
134 0 : return Math.floor(creditsToCents(targetCredits - thresholdCredits))
135 : }
136 :
137 0 : function thresholdError(
138 : thresholdCredits: number | null
139 : ): AutoTopUpFieldError | undefined {
140 0 : if (thresholdCredits === null) return 'notWhole'
141 0 : return thresholdCredits < 1 ? 'belowMinimum' : undefined
142 : }
143 :
144 0 : function targetError(
145 : { thresholdCredits, targetCredits }: AutoTopUpFormValues,
146 : rules: AutoTopUpRules
147 : ): AutoTopUpFieldError | undefined {
148 0 : if (targetCredits === null || targetCredits < 1) return 'notWhole'
149 0 : if (thresholdCredits === null) return undefined
150 0 : if (targetCredits < thresholdCredits + rules.minReloadCredits) {
151 0 : return 'belowTarget'
152 : }
153 0 : return rules.maxReloadCredits !== null &&
154 : targetCredits > thresholdCredits + rules.maxReloadCredits
155 : ? 'aboveTarget'
156 : : undefined
157 : }
158 :
159 0 : function limitError(
160 : values: AutoTopUpFormValues,
161 : rules: AutoTopUpRules
162 : ): AutoTopUpFieldError | undefined {
163 0 : const { limitOn, budgetCents } = values
164 0 : if (!limitOn) return undefined
165 0 : if (budgetCents === null || budgetCents < 1) return 'invalid'
166 0 : if (budgetCents > rules.maxMonthlyBudgetCents) return 'aboveCeiling'
167 0 : const smallest = smallestTopUpCents(values, rules.maxReloadCredits)
168 0 : return smallest !== null && budgetCents < smallest ? 'belowTopUp' : undefined
169 : }
170 :
171 0 : export function validateAutoTopUp(
172 : values: AutoTopUpFormValues,
173 : rules: AutoTopUpRules
174 : ): AutoTopUpErrors {
175 0 : const threshold = thresholdError(values.thresholdCredits)
176 0 : const target = targetError(values, rules)
177 0 : const limit = limitError(values, rules)
178 0 : return {
179 : ...(threshold && { threshold }),
180 : ...(target && { target }),
181 : ...(limit && { limit })
182 : }
183 : }
184 :
185 : /** The request settings for valid values; null while any field is in error. */
186 0 : export function autoTopUpActivationFor(
187 : values: AutoTopUpFormValues,
188 : rules: AutoTopUpRules
189 : ): AutoTopUpActivation | null {
190 0 : if (Object.keys(validateAutoTopUp(values, rules)).length > 0) return null
191 0 : if (values.thresholdCredits === null || values.targetCredits === null) {
192 0 : return null
193 : }
194 0 : return {
195 : threshold_credits: values.thresholdCredits,
196 : target_credits: values.targetCredits,
197 : monthly_budget_cents: values.budgetCents
198 : }
199 : }
200 :
201 0 : function autoTopUpRulesFrom(settings: AutoReloadSettings): AutoTopUpRules {
202 0 : return {
203 : minReloadCredits: settings.min_reload_credits,
204 : maxReloadCredits: settings.max_reload_credits,
205 : maxMonthlyBudgetCents: settings.max_monthly_budget_cents
206 : }
207 : }
208 :
209 : export interface AutoTopUpDefaultCard {
210 : readonly brand: string
211 : readonly last4: string
212 : }
213 :
214 : export type AddCreditsAutoTopUpOffer =
215 : | { readonly kind: 'hidden' }
216 : | { readonly kind: 'on' }
217 : | {
218 : readonly kind: 'checkbox'
219 : readonly rules: AutoTopUpRules
220 : readonly defaultCard: AutoTopUpDefaultCard | null
221 : }
222 :
223 : export interface AddCreditsAutoTopUpFacts {
224 : readonly autoTopUpEnabled: boolean
225 : readonly canManageAutoTopUp: boolean
226 : readonly setup: AutoTopUpSetup | null
227 : readonly settings: AutoReloadSettings | null
228 : readonly defaultCard: AutoTopUpDefaultCard | null
229 : }
230 :
231 : /** The checkbox for a manager whose served status is off, the on line while it is on, otherwise nothing. */
232 8 : export function addCreditsAutoTopUpOffer(
233 : facts: AddCreditsAutoTopUpFacts
234 : ): AddCreditsAutoTopUpOffer {
235 8 : const { settings } = facts
236 8 : if (!facts.autoTopUpEnabled || settings === null) return { kind: 'hidden' }
237 0 : if (settings.status === 'on') return { kind: 'on' }
238 8 : if (settings.status !== 'off' || !canSetUpAutoTopUp(facts)) {
239 0 : return { kind: 'hidden' }
240 : }
241 0 : return {
242 : kind: 'checkbox',
243 : rules: autoTopUpRulesFrom(settings),
244 : defaultCard: facts.defaultCard
245 : }
246 : }
|