(userId: string)
| 343 | * Get usage limit information for a user |
| 344 | */ |
| 345 | export async function getUserUsageLimitInfo(userId: string): Promise<UsageLimitInfo> { |
| 346 | try { |
| 347 | const [subscription, userStatsRecord] = await Promise.all([ |
| 348 | getHighestPrioritySubscription(userId), |
| 349 | db.select().from(userStats).where(eq(userStats.userId, userId)).limit(1), |
| 350 | ]) |
| 351 | |
| 352 | if (userStatsRecord.length === 0) { |
| 353 | throw new Error(`User stats not found for userId: ${userId}`) |
| 354 | } |
| 355 | |
| 356 | const stats = userStatsRecord[0] |
| 357 | const orgScoped = isOrgScopedSubscription(subscription, userId) |
| 358 | |
| 359 | let currentLimit: number |
| 360 | let minimumLimit: number |
| 361 | let canEdit: boolean |
| 362 | |
| 363 | if (orgScoped && subscription) { |
| 364 | const orgLimit = await getOrgUsageLimit( |
| 365 | subscription.referenceId, |
| 366 | subscription.plan, |
| 367 | subscription.seats |
| 368 | ) |
| 369 | currentLimit = orgLimit.limit |
| 370 | minimumLimit = orgLimit.minimum |
| 371 | canEdit = false |
| 372 | } else { |
| 373 | currentLimit = stats.currentUsageLimit |
| 374 | ? toNumber(toDecimal(stats.currentUsageLimit)) |
| 375 | : getFreeTierLimit() |
| 376 | minimumLimit = getPerUserMinimumLimit(subscription) |
| 377 | canEdit = canEditUsageLimit(subscription) |
| 378 | } |
| 379 | |
| 380 | return { |
| 381 | currentLimit, |
| 382 | canEdit, |
| 383 | minimumLimit, |
| 384 | plan: subscription?.plan || 'free', |
| 385 | updatedAt: stats.usageLimitUpdatedAt, |
| 386 | scope: orgScoped ? 'organization' : 'user', |
| 387 | organizationId: orgScoped && subscription ? subscription.referenceId : null, |
| 388 | } |
| 389 | } catch (error) { |
| 390 | logger.error('Failed to get usage limit info', { userId, error }) |
| 391 | throw error |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Initialize usage limits for a new user |
no test coverage detected