|
1 | 1 | import { and, eq } from 'drizzle-orm' |
| 2 | +import { DEFAULT_FREE_CREDITS } from '@/lib/billing/constants' |
2 | 3 | import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription' |
3 | 4 | import { calculateDefaultUsageLimit, canEditUsageLimit } from '@/lib/billing/subscriptions/utils' |
4 | 5 | import type { BillingData, UsageData, UsageLimitInfo } from '@/lib/billing/types' |
@@ -29,7 +30,7 @@ export async function getUserUsageData(userId: string): Promise<UsageData> { |
29 | 30 | await initializeUserUsageLimit(userId) |
30 | 31 | return { |
31 | 32 | currentUsage: 0, |
32 | | - limit: 5, |
| 33 | + limit: DEFAULT_FREE_CREDITS, |
33 | 34 | percentUsed: 0, |
34 | 35 | isWarning: false, |
35 | 36 | isExceeded: false, |
@@ -98,7 +99,7 @@ export async function getUserUsageLimitInfo(userId: string): Promise<UsageLimitI |
98 | 99 | let minimumLimit: number |
99 | 100 | if (!subscription || subscription.status !== 'active') { |
100 | 101 | // Free plan users |
101 | | - minimumLimit = 5 |
| 102 | + minimumLimit = DEFAULT_FREE_CREDITS |
102 | 103 | } else if (subscription.plan === 'pro') { |
103 | 104 | // Pro plan users: $20 minimum |
104 | 105 | minimumLimit = 20 |
@@ -131,9 +132,9 @@ export async function getUserUsageLimitInfo(userId: string): Promise<UsageLimitI |
131 | 132 | if (userStatsRecord.length === 0) { |
132 | 133 | await initializeUserUsageLimit(userId) |
133 | 134 | return { |
134 | | - currentLimit: 5, |
| 135 | + currentLimit: DEFAULT_FREE_CREDITS, |
135 | 136 | canEdit: false, |
136 | | - minimumLimit: 5, |
| 137 | + minimumLimit: DEFAULT_FREE_CREDITS, |
137 | 138 | plan: 'free', |
138 | 139 | setBy: null, |
139 | 140 | updatedAt: null, |
@@ -171,16 +172,16 @@ export async function initializeUserUsageLimit(userId: string): Promise<void> { |
171 | 172 | return // User already has usage stats, don't override |
172 | 173 | } |
173 | 174 |
|
174 | | - // Create initial usage stats with default $5 limit |
| 175 | + // Create initial usage stats with default free credits limit |
175 | 176 | await db.insert(userStats).values({ |
176 | 177 | id: crypto.randomUUID(), |
177 | 178 | userId, |
178 | | - currentUsageLimit: '5', // Default $5 for new users |
| 179 | + currentUsageLimit: DEFAULT_FREE_CREDITS.toString(), // Default free credits for new users |
179 | 180 | usageLimitUpdatedAt: new Date(), |
180 | 181 | billingPeriodStart: new Date(), // Start billing period immediately |
181 | 182 | }) |
182 | 183 |
|
183 | | - logger.info('Initialized usage limit for new user', { userId, limit: 5 }) |
| 184 | + logger.info('Initialized usage limit for new user', { userId, limit: DEFAULT_FREE_CREDITS }) |
184 | 185 | } catch (error) { |
185 | 186 | logger.error('Failed to initialize usage limit', { userId, error }) |
186 | 187 | throw error |
@@ -233,7 +234,7 @@ export async function updateUserUsageLimit( |
233 | 234 |
|
234 | 235 | if (!subscription || subscription.status !== 'active') { |
235 | 236 | // Free plan users (shouldn't reach here due to canEditUsageLimit check above) |
236 | | - minimumLimit = 5 |
| 237 | + minimumLimit = DEFAULT_FREE_CREDITS |
237 | 238 | } else if (subscription.plan === 'pro') { |
238 | 239 | // Pro plan users: $20 minimum |
239 | 240 | minimumLimit = 20 |
@@ -311,7 +312,7 @@ export async function getUserUsageLimit(userId: string): Promise<number> { |
311 | 312 | if (userStatsQuery.length === 0) { |
312 | 313 | // User doesn't have stats yet, initialize and return default |
313 | 314 | await initializeUserUsageLimit(userId) |
314 | | - return 5 // Default free plan limit |
| 315 | + return DEFAULT_FREE_CREDITS // Default free plan limit |
315 | 316 | } |
316 | 317 |
|
317 | 318 | return Number.parseFloat(userStatsQuery[0].currentUsageLimit) |
@@ -380,16 +381,16 @@ export async function syncUsageLimitsFromSubscription(userId: string): Promise<v |
380 | 381 |
|
381 | 382 | // Only update if subscription is free plan or if current limit is below new minimum |
382 | 383 | if (!subscription || subscription.status !== 'active') { |
383 | | - // User downgraded to free plan - cap at $5 |
| 384 | + // User downgraded to free plan - cap at default free credits |
384 | 385 | await db |
385 | 386 | .update(userStats) |
386 | 387 | .set({ |
387 | | - currentUsageLimit: '5', |
| 388 | + currentUsageLimit: DEFAULT_FREE_CREDITS.toString(), |
388 | 389 | usageLimitUpdatedAt: new Date(), |
389 | 390 | }) |
390 | 391 | .where(eq(userStats.userId, userId)) |
391 | 392 |
|
392 | | - logger.info('Synced usage limit to free plan', { userId, limit: 5 }) |
| 393 | + logger.info('Synced usage limit to free plan', { userId, limit: DEFAULT_FREE_CREDITS }) |
393 | 394 | } else if (currentLimit < defaultLimit) { |
394 | 395 | // User upgraded and current limit is below new minimum - raise to minimum |
395 | 396 | await db |
@@ -451,7 +452,7 @@ export async function getTeamUsageLimits(organizationId: string): Promise< |
451 | 452 | userId: memberData.userId, |
452 | 453 | userName: memberData.userName, |
453 | 454 | userEmail: memberData.userEmail, |
454 | | - currentLimit: Number.parseFloat(memberData.currentLimit || '5'), |
| 455 | + currentLimit: Number.parseFloat(memberData.currentLimit || DEFAULT_FREE_CREDITS.toString()), |
455 | 456 | currentUsage: Number.parseFloat(memberData.currentPeriodCost || '0'), |
456 | 457 | totalCost: Number.parseFloat(memberData.totalCost || '0'), |
457 | 458 | lastActive: memberData.lastActive, |
|
0 commit comments