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

Function getUserUsageLimit

apps/sim/lib/billing/core/usage.ts:508–589  ·  view source on GitHub ↗
(
  userId: string,
  preloadedSubscription?: HighestPrioritySubscription
)

Source from the content-addressed store, hash-verified

506 * write still resolves to the fallback instead of blocking execution.
507 */
508export async function getUserUsageLimit(
509 userId: string,
510 preloadedSubscription?: HighestPrioritySubscription
511): Promise<number> {
512 const subscription =
513 preloadedSubscription !== undefined
514 ? preloadedSubscription
515 : await getHighestPrioritySubscription(userId)
516
517 if (isOrgScopedSubscription(subscription, userId) && subscription) {
518 const orgExists = await db
519 .select({ id: organization.id })
520 .from(organization)
521 .where(eq(organization.id, subscription.referenceId))
522 .limit(1)
523
524 if (orgExists.length === 0) {
525 throw new Error(`Organization not found: ${subscription.referenceId} for user: ${userId}`)
526 }
527
528 const orgLimit = await getOrgUsageLimit(
529 subscription.referenceId,
530 subscription.plan,
531 subscription.seats
532 )
533 return orgLimit.limit
534 }
535
536 const userStatsQuery = await db
537 .select({ currentUsageLimit: userStats.currentUsageLimit })
538 .from(userStats)
539 .where(eq(userStats.userId, userId))
540 .limit(1)
541
542 if (userStatsQuery.length === 0) {
543 throw new Error(
544 `No user stats record found for userId: ${userId}. User must be properly initialized before execution.`
545 )
546 }
547
548 if (!userStatsQuery[0].currentUsageLimit) {
549 const fallbackLimit =
550 subscription && hasPaidSubscriptionStatus(subscription.status)
551 ? getPerUserMinimumLimit(subscription)
552 : getFreeTierLimit()
553
554 try {
555 const healed = await db
556 .update(userStats)
557 .set({
558 currentUsageLimit: fallbackLimit.toString(),
559 usageLimitUpdatedAt: new Date(),
560 })
561 .where(and(eq(userStats.userId, userId), isNull(userStats.currentUsageLimit)))
562 .returning({ currentUsageLimit: userStats.currentUsageLimit })
563
564 if (healed.length === 0) {
565 const concurrent = await db

Callers 2

usage.test.tsFile · 0.90
checkUsageStatusFunction · 0.90

Calls 13

isOrgScopedSubscriptionFunction · 0.90
getPerUserMinimumLimitFunction · 0.90
getFreeTierLimitFunction · 0.90
toNumberFunction · 0.90
toDecimalFunction · 0.90
getOrgUsageLimitFunction · 0.85
errorMethod · 0.80
setMethod · 0.65
warnMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected