Line data Source code
1 : import type { BillingCapabilityDenials } from '@comfyorg/ingest-types'
2 :
3 : export type BillingTopUpDenial = NonNullable<
4 : BillingCapabilityDenials['can_top_up']
5 : >
6 :
7 : export type AddCreditsAvailability =
8 : | { kind: 'enabled' }
9 : | { kind: 'hidden' }
10 : | { kind: 'disabled'; reasonKey: string }
11 :
12 2355 : const MEMBER_TOP_UP_DENIALS: ReadonlySet<BillingTopUpDenial> = new Set([
13 : 'not_workspace_owner',
14 : 'not_a_member'
15 : ])
16 :
17 2355 : const REFUSAL_REASON_KEYS: Partial<Record<BillingTopUpDenial, string>> = {
18 : topup_purchases_paused: 'credits.addCreditsRefusal.topupPurchasesPaused',
19 : payment_past_due: 'credits.addCreditsRefusal.paymentPastDue'
20 : }
21 :
22 2355 : const UNAVAILABLE_REASON_KEY = 'credits.addCreditsRefusal.unavailable'
23 :
24 : /** Whether the served `can_top_up` refusal names someone who is not the workspace owner. */
25 0 : export function isMemberTopUpDenial(
26 : denial: BillingTopUpDenial | undefined
27 : ): boolean {
28 0 : return denial !== undefined && MEMBER_TOP_UP_DENIALS.has(denial)
29 : }
30 :
31 : /** How Add credits renders for the served `can_top_up` and its denial reason. */
32 13 : export function addCreditsAvailability(facts: {
33 : resolved: boolean
34 : canTopUp: boolean
35 : denial: BillingTopUpDenial | undefined
36 : }): AddCreditsAvailability {
37 13 : if (facts.canTopUp) return { kind: 'enabled' }
38 13 : if (!facts.resolved || isMemberTopUpDenial(facts.denial)) {
39 0 : return { kind: 'hidden' }
40 : }
41 : const reasonKey =
42 : (facts.denial && REFUSAL_REASON_KEYS[facts.denial]) ??
43 : UNAVAILABLE_REASON_KEY
44 13 : return { kind: 'disabled', reasonKey }
45 : }
|