MCPcopy Index your code
hub / github.com/simstudioai/sim / checkAndBillOverageThreshold

Function checkAndBillOverageThreshold

apps/sim/lib/billing/threshold-billing.ts:43–246  ·  view source on GitHub ↗
(userId: string)

Source from the content-addressed store, hash-verified

41}
42
43export async function checkAndBillOverageThreshold(userId: string): Promise<void> {
44 try {
45 const threshold = OVERAGE_THRESHOLD
46
47 const userSubscription = await getHighestPrioritySubscription(userId)
48 const billingStatus = await getEffectiveBillingStatus(userId)
49
50 if (
51 !userSubscription ||
52 !hasUsableSubscriptionAccess(userSubscription.status, billingStatus.billingBlocked)
53 ) {
54 logger.debug('No active subscription for threshold billing', { userId })
55 return
56 }
57
58 if (isFree(userSubscription.plan) || isEnterprise(userSubscription.plan)) {
59 return
60 }
61
62 // Org-scoped subs are billed at the org level regardless of plan name.
63 if (isOrgScopedSubscription(userSubscription, userId)) {
64 logger.debug('Org-scoped subscription detected - triggering org-level threshold billing', {
65 userId,
66 organizationId: userSubscription.referenceId,
67 plan: userSubscription.plan,
68 })
69 await checkAndBillOrganizationOverageThreshold(userSubscription.referenceId)
70 return
71 }
72
73 const usageSnapshot = await getPersonalUsageSnapshot(userId)
74 if (!usageSnapshot) {
75 logger.warn('User stats not found for threshold billing', { userId })
76 return
77 }
78
79 const currentOverage = await calculateSubscriptionOverage({
80 id: userSubscription.id,
81 plan: userSubscription.plan,
82 referenceId: userSubscription.referenceId,
83 seats: userSubscription.seats,
84 periodStart: userSubscription.periodStart,
85 periodEnd: userSubscription.periodEnd,
86 })
87
88 if (currentOverage < threshold) {
89 logger.debug('Threshold billing check below threshold before locking user stats', {
90 userId,
91 plan: userSubscription.plan,
92 currentOverage,
93 threshold,
94 })
95 return
96 }
97
98 const stripeSubscriptionId = userSubscription.stripeSubscriptionId
99 if (!stripeSubscriptionId) {
100 logger.error('No Stripe subscription ID found', { userId })

Callers 5

recordExecutionUsageMethod · 0.90
processDocumentAsyncFunction · 0.90
updateUserStatsForWandFunction · 0.90
updateCostInnerFunction · 0.90

Calls 15

isFreeFunction · 0.90
isEnterpriseFunction · 0.90
isOrgScopedSubscriptionFunction · 0.90
toNumberFunction · 0.90
toDecimalFunction · 0.90
enqueueOutboxEventFunction · 0.90
getPersonalUsageSnapshotFunction · 0.85

Tested by

no test coverage detected