Skip to content

Commit ed2a1d9

Browse files
committed
Remove subscription_count. Add more stripe status enums
1 parent 8976298 commit ed2a1d9

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type SubscriptionStatus = (typeof schema.subscriptionStatusEnum.enumValues)[numb
1919
* Maps a Stripe subscription status to our local enum.
2020
*/
2121
function mapStripeStatus(status: Stripe.Subscription.Status): SubscriptionStatus {
22-
if (status === 'past_due') return 'past_due'
23-
if (status === 'canceled') return 'canceled'
22+
const validStatuses: readonly string[] = schema.subscriptionStatusEnum.enumValues
23+
if (validStatuses.includes(status)) return status as SubscriptionStatus
2424
return 'active'
2525
}
2626

packages/billing/src/subscription.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,8 @@ export async function isSubscriber(params: {
510510
/**
511511
* Handles the first-time-subscribe side-effects:
512512
* 1. Moves `next_quota_reset` to Stripe's `current_period_end`.
513-
* 2. Increments `subscription_count`.
514-
* 3. Migrates unused free/referral credits into a single grant aligned to
515-
* the new reset date.
513+
* 2. Migrates unused credits into a single grant aligned to the new reset
514+
* date.
516515
*
517516
* All operations run inside an advisory-locked transaction.
518517
*/
@@ -542,13 +541,10 @@ export async function handleSubscribe(params: {
542541
return
543542
}
544543

545-
// Move next_quota_reset and bump subscription_count
544+
// Move next_quota_reset to align with Stripe billing period
546545
await tx
547546
.update(schema.user)
548-
.set({
549-
next_quota_reset: newResetDate,
550-
subscription_count: sql`${schema.user.subscription_count} + 1`,
551-
})
547+
.set({ next_quota_reset: newResetDate })
552548
.where(eq(schema.user.id, userId))
553549

554550
// Migrate unused credits so nothing is lost

packages/internal/src/db/schema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ export const agentStepStatus = pgEnum('agent_step_status', [
5353
])
5454

5555
export const subscriptionStatusEnum = pgEnum('subscription_status', [
56+
'incomplete',
57+
'incomplete_expired',
58+
'trialing',
5659
'active',
5760
'past_due',
5861
'canceled',
62+
'unpaid',
63+
'paused',
5964
])
6065

6166
export const user = pgTable('user', {
@@ -83,7 +88,6 @@ export const user = pgTable('user', {
8388
auto_topup_threshold: integer('auto_topup_threshold'),
8489
auto_topup_amount: integer('auto_topup_amount'),
8590
banned: boolean('banned').notNull().default(false),
86-
subscription_count: integer('subscription_count').notNull().default(0),
8791
})
8892

8993
export const account = pgTable(

0 commit comments

Comments
 (0)