* Initialize usage limits for a new user
(userId: string)
| 396 | * Initialize usage limits for a new user |
| 397 | */ |
| 398 | async function initializeUserUsageLimit(userId: string): Promise<void> { |
| 399 | // Check if user already has usage stats |
| 400 | const existingStats = await db |
| 401 | .select() |
| 402 | .from(userStats) |
| 403 | .where(eq(userStats.userId, userId)) |
| 404 | .limit(1) |
| 405 | |
| 406 | if (existingStats.length > 0) { |
| 407 | return |
| 408 | } |
| 409 | |
| 410 | const subscription = await getHighestPrioritySubscription(userId) |
| 411 | const orgScoped = isOrgScopedSubscription(subscription, userId) |
| 412 | |
| 413 | await db.insert(userStats).values({ |
| 414 | id: generateId(), |
| 415 | userId, |
| 416 | currentUsageLimit: orgScoped ? null : getFreeTierLimit().toString(), |
| 417 | usageLimitUpdatedAt: new Date(), |
| 418 | }) |
| 419 | |
| 420 | logger.info('Initialized user stats', { |
| 421 | userId, |
| 422 | plan: subscription?.plan || 'free', |
| 423 | hasIndividualLimit: !orgScoped, |
| 424 | }) |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Update a user's custom usage limit |
nothing calls this directly
no test coverage detected