Skip to content

Commit 214a035

Browse files
authored
fix(billing): fix upgrade to team plan (#1045)
1 parent bbb5e53 commit 214a035

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

apps/sim/lib/auth.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,26 @@ export const auth = betterAuth({
12961296
if (user.length > 0) {
12971297
const currentUser = user[0]
12981298

1299+
// Store the original user ID before we change the referenceId
1300+
const originalUserId = subscription.referenceId
1301+
1302+
// First, sync usage limits for the purchasing user with their new plan
1303+
try {
1304+
const { syncUsageLimitsFromSubscription } = await import('@/lib/billing')
1305+
await syncUsageLimitsFromSubscription(originalUserId)
1306+
logger.info(
1307+
'Usage limits synced for purchasing user before organization creation',
1308+
{
1309+
userId: originalUserId,
1310+
}
1311+
)
1312+
} catch (error) {
1313+
logger.error('Failed to sync usage limits for purchasing user', {
1314+
userId: originalUserId,
1315+
error,
1316+
})
1317+
}
1318+
12991319
// Create organization for the team
13001320
const orgId = `org_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`
13011321
const orgSlug = `${currentUser.name?.toLowerCase().replace(/\s+/g, '-') || 'team'}-${Date.now()}`
@@ -1376,17 +1396,21 @@ export const auth = betterAuth({
13761396
}
13771397
}
13781398

1379-
// Sync usage limits and initialize billing period for the user/organization
1399+
// Initialize billing period for the user/organization
13801400
try {
1381-
const { syncUsageLimitsFromSubscription } = await import('@/lib/billing')
13821401
const { initializeBillingPeriod } = await import(
13831402
'@/lib/billing/core/billing-periods'
13841403
)
13851404

1386-
await syncUsageLimitsFromSubscription(subscription.referenceId)
1387-
logger.info('Usage limits synced after subscription creation', {
1388-
referenceId: subscription.referenceId,
1389-
})
1405+
// Note: Usage limits are already synced above for team plan users
1406+
// For non-team plans, sync usage limits using the referenceId (which is the user ID)
1407+
if (subscription.plan !== 'team') {
1408+
const { syncUsageLimitsFromSubscription } = await import('@/lib/billing')
1409+
await syncUsageLimitsFromSubscription(subscription.referenceId)
1410+
logger.info('Usage limits synced after subscription creation', {
1411+
referenceId: subscription.referenceId,
1412+
})
1413+
}
13901414

13911415
// Initialize billing period for new subscription using Stripe dates
13921416
if (subscription.plan !== 'free') {

0 commit comments

Comments
 (0)