Skip to content

Commit 1b1176c

Browse files
committed
Fixes from reviewer
1 parent 1509a09 commit 1b1176c

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@codebuff/internal/util/stripe'
1111
import { eq } from 'drizzle-orm'
1212

13-
import { handleSubscribe } from './subscription'
13+
import { expireActiveBlockGrants, handleSubscribe } from './subscription'
1414

1515
import type { SubscriptionTierPrice } from '@codebuff/common/constants/subscription-plans'
1616
import type { Logger } from '@codebuff/common/types/contracts/logger'
@@ -85,6 +85,15 @@ export async function handleSubscriptionInvoicePaid(params: {
8585
return
8686
}
8787

88+
const tier = getTierFromPriceId(priceId)
89+
if (!tier) {
90+
logger.debug(
91+
{ subscriptionId, priceId },
92+
'Price ID does not match a Strong tier — skipping',
93+
)
94+
return
95+
}
96+
8897
// Look up the user for this customer
8998
const userId = (await getUserByStripeCustomerId(customerId))?.id ?? null
9099

@@ -104,6 +113,8 @@ export async function handleSubscriptionInvoicePaid(params: {
104113
}
105114
}
106115

116+
const status = mapStripeStatus(stripeSub.status)
117+
107118
// Upsert subscription row
108119
await db
109120
.insert(schema.subscription)
@@ -112,19 +123,19 @@ export async function handleSubscriptionInvoicePaid(params: {
112123
stripe_customer_id: customerId,
113124
user_id: userId,
114125
stripe_price_id: priceId,
115-
tier: getTierFromPriceId(priceId),
116-
status: 'active',
126+
tier,
127+
status,
117128
billing_period_start: new Date(stripeSub.current_period_start * 1000),
118129
billing_period_end: new Date(stripeSub.current_period_end * 1000),
119130
cancel_at_period_end: stripeSub.cancel_at_period_end,
120131
})
121132
.onConflictDoUpdate({
122133
target: schema.subscription.stripe_subscription_id,
123134
set: {
124-
status: 'active',
135+
status,
125136
...(userId ? { user_id: userId } : {}),
126137
stripe_price_id: priceId,
127-
tier: getTierFromPriceId(priceId),
138+
tier,
128139
billing_period_start: new Date(
129140
stripeSub.current_period_start * 1000,
130141
),
@@ -209,6 +220,15 @@ export async function handleSubscriptionUpdated(params: {
209220
return
210221
}
211222

223+
const tier = getTierFromPriceId(priceId)
224+
if (!tier) {
225+
logger.debug(
226+
{ subscriptionId, priceId },
227+
'Price ID does not match a Strong tier — skipping',
228+
)
229+
return
230+
}
231+
212232
const customerId = getStripeId(stripeSubscription.customer)
213233
const userId = (await getUserByStripeCustomerId(customerId))?.id ?? null
214234

@@ -223,7 +243,7 @@ export async function handleSubscriptionUpdated(params: {
223243
stripe_customer_id: customerId,
224244
user_id: userId,
225245
stripe_price_id: priceId,
226-
tier: getTierFromPriceId(priceId),
246+
tier,
227247
status,
228248
cancel_at_period_end: stripeSubscription.cancel_at_period_end,
229249
billing_period_start: new Date(
@@ -238,7 +258,7 @@ export async function handleSubscriptionUpdated(params: {
238258
set: {
239259
...(userId ? { user_id: userId } : {}),
240260
stripe_price_id: priceId,
241-
tier: getTierFromPriceId(priceId),
261+
tier,
242262
status,
243263
cancel_at_period_end: stripeSubscription.cancel_at_period_end,
244264
billing_period_start: new Date(
@@ -286,6 +306,10 @@ export async function handleSubscriptionDeleted(params: {
286306
})
287307
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
288308

309+
if (userId) {
310+
await expireActiveBlockGrants({ userId, subscriptionId, logger })
311+
}
312+
289313
trackEvent({
290314
event: AnalyticsEvent.SUBSCRIPTION_CANCELED,
291315
userId: userId ?? 'system',

packages/billing/src/subscription.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ export async function expireActiveBlockGrants(params: {
499499

500500
const expired = await db
501501
.update(schema.creditLedger)
502-
.set({ balance: 0, expires_at: now })
502+
.set({ expires_at: now })
503503
.where(
504504
and(
505505
eq(schema.creditLedger.user_id, userId),
@@ -513,7 +513,7 @@ export async function expireActiveBlockGrants(params: {
513513
if (expired.length > 0) {
514514
logger.info(
515515
{ userId, subscriptionId, expiredCount: expired.length },
516-
'Expired active block grants for tier change',
516+
'Expired active block grants',
517517
)
518518
}
519519

@@ -539,6 +539,7 @@ export async function getActiveSubscription(params: {
539539
eq(schema.subscription.status, 'active'),
540540
),
541541
)
542+
.orderBy(desc(schema.subscription.updated_at))
542543
.limit(1)
543544

544545
return subs[0] ?? null

0 commit comments

Comments
 (0)