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

Function getOrgUsageLimit

apps/sim/lib/billing/core/usage.ts:98–140  ·  view source on GitHub ↗
(
  organizationId: string,
  plan: string,
  seats: number | null,
  executor: DbClient = db
)

Source from the content-addressed store, hash-verified

96 * floor. Returns `{ limit, minimum }` where `limit = max(configured, minimum)`.
97 */
98export async function getOrgUsageLimit(
99 organizationId: string,
100 plan: string,
101 seats: number | null,
102 executor: DbClient = db
103): Promise<OrgUsageLimitResult> {
104 const orgData = await executor
105 .select({ orgUsageLimit: organization.orgUsageLimit })
106 .from(organization)
107 .where(eq(organization.id, organizationId))
108 .limit(1)
109
110 const configured =
111 orgData.length > 0 && orgData[0].orgUsageLimit
112 ? toNumber(toDecimal(orgData[0].orgUsageLimit))
113 : null
114
115 if (isEnterprise(plan)) {
116 // Enterprise: Use configured limit directly (no per-seat minimum)
117 if (configured !== null) {
118 return { limit: configured, minimum: configured }
119 }
120 logger.warn('Enterprise org missing usage limit', { orgId: organizationId })
121 return { limit: 0, minimum: 0 }
122 }
123
124 const { basePrice } = getPlanPricing(plan)
125 // `||` not `??` — 0 is never a valid seat count for a paid sub.
126 const seatCount = seats || 1
127 const minimum = seatCount * basePrice
128
129 if (configured !== null) {
130 return { limit: Math.max(configured, minimum), minimum }
131 }
132
133 logger.warn('Org missing usage limit, using plan-driven minimum as fallback', {
134 orgId: organizationId,
135 plan,
136 seats: seatCount,
137 minimum,
138 })
139 return { limit: minimum, minimum }
140}
141
142/**
143 * Handle new user setup when they join the platform

Callers 5

getUserUsageDataFunction · 0.85
getUserUsageLimitInfoFunction · 0.85
getUserUsageLimitFunction · 0.85

Calls 6

toNumberFunction · 0.90
toDecimalFunction · 0.90
isEnterpriseFunction · 0.90
getPlanPricingFunction · 0.90
warnMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected