Line data Source code
1 : import type { BillingTierKey } from '@comfyorg/account-core/billing'
2 : import { TIER_CATALOG } from '@comfyorg/account-ui/billing/catalog'
3 : import type { SubscriptionTier as IngestSubscriptionTier } from '@comfyorg/ingest-types'
4 :
5 : import { remoteConfig } from '@/platform/remoteConfig/remoteConfig'
6 : import type { components } from '@/types/comfyRegistryTypes'
7 :
8 : export type { IngestSubscriptionTier }
9 :
10 : export type RegistrySubscriptionTier = components['schemas']['SubscriptionTier']
11 :
12 : export type TierKey = BillingTierKey
13 :
14 : // Self-serve personal-plan tiers only. TEAM and ENTERPRISE are workspace-level
15 : // and sales-managed respectively, and intentionally have no catalog key (see
16 : // toTierKey/isSalesManagedTier below).
17 : type CatalogTier = Exclude<RegistrySubscriptionTier, 'TEAM' | 'ENTERPRISE'>
18 :
19 2356 : const TIER_TO_KEY: Record<CatalogTier, TierKey> = {
20 : FREE: 'free',
21 : STANDARD: 'standard',
22 : CREATOR: 'creator',
23 : PRO: 'pro',
24 : FOUNDERS_EDITION: 'founder'
25 : }
26 :
27 2356 : export const KEY_TO_TIER: Record<TierKey, RegistrySubscriptionTier> = {
28 : free: 'FREE',
29 : standard: 'STANDARD',
30 : creator: 'CREATOR',
31 : pro: 'PRO',
32 : founder: 'FOUNDERS_EDITION'
33 : }
34 :
35 : export interface TierPricing {
36 : monthly: number
37 : yearly: number
38 : credits: number
39 : videoEstimate: number
40 : }
41 :
42 2356 : export const TIER_PRICING: Record<
43 : Exclude<TierKey, 'free' | 'founder'>,
44 : TierPricing
45 : > = {
46 : standard: { ...TIER_CATALOG.standard, videoEstimate: 380 },
47 : creator: { ...TIER_CATALOG.creator, videoEstimate: 670 },
48 : pro: { ...TIER_CATALOG.pro, videoEstimate: 1915 }
49 : }
50 :
51 2356 : const MONTHS_PER_YEAR = 12
52 :
53 : // Annual plans grant the whole year up front (catalog `*-annual` credit_grant
54 : // is 12x the monthly grant), so a yearly cycle shows the year's total.
55 0 : export function amountForBillingCycle(
56 : monthlyAmount: number,
57 : isYearly: boolean
58 : ): number {
59 0 : return isYearly ? monthlyAmount * MONTHS_PER_YEAR : monthlyAmount
60 : }
61 :
62 : interface TierFeatures {
63 : customLoRAs: boolean
64 : maxMembers: number
65 : }
66 :
67 2356 : const TIER_FEATURES: Record<TierKey, TierFeatures> = {
68 : free: { customLoRAs: false, maxMembers: 1 },
69 : standard: { customLoRAs: false, maxMembers: 1 },
70 : creator: { customLoRAs: true, maxMembers: 5 },
71 : pro: { customLoRAs: true, maxMembers: 20 },
72 : founder: { customLoRAs: false, maxMembers: 1 }
73 : }
74 :
75 2356 : export const DEFAULT_TIER_KEY: TierKey = 'standard'
76 :
77 : // Membership is tested rather than listing the exclusions, so a tier added to
78 : // the ingest enum narrows to false and returns null instead of failing the
79 : // build. Two details are load-bearing:
80 : // - `typeof`, because tier is unvalidated backend JSON and need not be a
81 : // string; hasOwnProperty coerces its argument to a property key, so
82 : // ['FREE'] would be accepted as FREE and a null toString would throw.
83 : // - own-property rather than `in`, which walks the prototype chain and would
84 : // return an inherited function for 'constructor' or 'toString'.
85 26 : function isRegistrySubscriptionTier(tier: unknown): tier is CatalogTier {
86 26 : return (
87 : typeof tier === 'string' &&
88 : Object.prototype.hasOwnProperty.call(TIER_TO_KEY, tier)
89 : )
90 : }
91 :
92 : // Workspace-level tiers (TEAM, and any added later) map to no key in this
93 : // personal plan catalog.
94 26 : export function toTierKey(tier: IngestSubscriptionTier): TierKey | null {
95 26 : return isRegistrySubscriptionTier(tier) ? TIER_TO_KEY[tier] : null
96 : }
97 :
98 : // Enterprise plans are intentionally absent from the self-serve catalog, so a
99 : // scheduled change to one cannot be resolved through `plans` and carries no
100 : // tier to read. The slug is the only signal on that path; everywhere a tier
101 : // exists, compare it to 'ENTERPRISE' directly.
102 10 : export function isEnterprisePlanSlug(slug: string | null | undefined): boolean {
103 10 : return slug?.toLowerCase().startsWith('enterprise') === true
104 : }
105 :
106 : // A tier the frontend cannot map to its catalog and that is not one of the
107 : // workspace-level tiers it knows about. Price and feature claims must never be
108 : // borrowed for a plan we cannot identify (FE-1662 story 6).
109 10 : export function isUnknownTier(
110 : tier: IngestSubscriptionTier | null | undefined
111 : ): boolean {
112 10 : if (tier == null || tier === 'TEAM' || tier === 'ENTERPRISE') return false
113 10 : return toTierKey(tier) === null
114 : }
115 :
116 : // Enterprise and unrecognized tiers share one presentation: no catalog price,
117 : // benefits, or pricing surfaces. The server hides their lifecycle capabilities
118 : // (billing-api hideLifecycleCapabilities); this helper only drives rendering.
119 : // Where a sales conversation starts. Shared by the pricing table's footnote
120 : // and the ended-plan Contact sales actions so the destination cannot drift.
121 2356 : export const ENTERPRISE_URL = 'https://comfy.org/cloud/enterprise/'
122 :
123 10 : export function isSalesManagedTier(
124 : tier: IngestSubscriptionTier | null | undefined
125 : ): boolean {
126 10 : return tier === 'ENTERPRISE' || isUnknownTier(tier)
127 : }
128 :
129 0 : export function hasEndingNoticeStarted(
130 : startsAt: string | null | undefined,
131 : now: number = Date.now()
132 : ): boolean {
133 0 : if (!startsAt) return false
134 0 : const start = Date.parse(startsAt)
135 0 : return !Number.isNaN(start) && now >= start
136 : }
137 :
138 0 : export function isWithinEnterpriseEndingNotice(
139 : endDate: string | null | undefined,
140 : now: number = Date.now()
141 : ): boolean {
142 0 : if (!endDate) return false
143 0 : const end = Date.parse(endDate)
144 0 : return !Number.isNaN(end) && end - now <= 14 * 24 * 60 * 60 * 1000
145 : }
146 :
147 : // Includes the workspace-level TEAM, which toTierKey maps to null: a catalog
148 : // key is not a usable test for "is on a paid plan".
149 0 : export function hasActivePaidPlan(
150 : tier: IngestSubscriptionTier | null | undefined
151 : ): boolean {
152 0 : return tier != null && tier !== 'FREE'
153 : }
154 :
155 2356 : const FOUNDER_MONTHLY_PRICE = 20
156 2356 : const FOUNDER_MONTHLY_CREDITS = 5461
157 :
158 0 : export function getTierPrice(tierKey: TierKey, isYearly = false): number {
159 0 : if (tierKey === 'free') return 0
160 0 : if (tierKey === 'founder') return FOUNDER_MONTHLY_PRICE
161 0 : const pricing = TIER_PRICING[tierKey]
162 0 : return isYearly ? pricing.yearly : pricing.monthly
163 : }
164 :
165 6 : export function getTierCredits(tierKey: TierKey): number | null {
166 6 : if (tierKey === 'free') return remoteConfig.value.free_tier_credits ?? null
167 6 : if (tierKey === 'founder') return FOUNDER_MONTHLY_CREDITS
168 6 : return TIER_PRICING[tierKey]?.credits
169 : }
170 :
171 0 : export function getTierFeatures(tierKey: TierKey): TierFeatures {
172 0 : return TIER_FEATURES[tierKey]
173 : }
|