MCPcopy
hub / github.com/simstudioai/sim / updateOrganizationUsageLimit

Function updateOrganizationUsageLimit

apps/sim/lib/billing/core/organization.ts:279–359  ·  view source on GitHub ↗
(
  organizationId: string,
  newLimit: number
)

Source from the content-addressed store, hash-verified

277 * Update organization usage limit (cap)
278 */
279export async function updateOrganizationUsageLimit(
280 organizationId: string,
281 newLimit: number
282): Promise<{ success: boolean; error?: string }> {
283 try {
284 // Validate the organization exists
285 const orgRecord = await db
286 .select()
287 .from(organization)
288 .where(eq(organization.id, organizationId))
289 .limit(1)
290
291 if (orgRecord.length === 0) {
292 return { success: false, error: 'Organization not found' }
293 }
294
295 // Get subscription to validate minimum
296 const subscription = await getOrganizationSubscription(organizationId)
297 if (!subscription) {
298 return { success: false, error: 'No active subscription found' }
299 }
300
301 if (
302 !hasUsableSubscriptionStatus(subscription.status) ||
303 (await isOrganizationBillingBlocked(organizationId))
304 ) {
305 return { success: false, error: 'An active subscription is required to edit usage limits' }
306 }
307
308 if (isEnterprise(subscription.plan)) {
309 return {
310 success: false,
311 error: 'Enterprise plans have fixed usage limits that cannot be changed',
312 }
313 }
314
315 if (!isPaid(subscription.plan)) {
316 return {
317 success: false,
318 error: 'Organization is not on a paid plan',
319 }
320 }
321
322 const { basePrice } = getPlanPricing(subscription.plan)
323 const seatCount = subscription.seats || 1
324 const minimumLimit = seatCount * basePrice
325
326 if (newLimit < minimumLimit) {
327 return {
328 success: false,
329 error: `Usage limit cannot be less than minimum billing amount of $${roundCurrency(minimumLimit).toFixed(2)}`,
330 }
331 }
332
333 await db
334 .update(organization)
335 .set({
336 orgUsageLimit: roundCurrency(newLimit).toFixed(2),

Callers 1

route.tsFile · 0.85

Calls 11

isEnterpriseFunction · 0.90
isPaidFunction · 0.90
getPlanPricingFunction · 0.90
roundCurrencyFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
setMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected