Skip to content

Commit dd71ccf

Browse files
committed
fix: add warning log when subscription not found in handleSubscriptionDeleted
1 parent 85a0022 commit dd71ccf

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,30 @@ export async function handleSubscriptionDeleted(params: {
347347
const user = await getUserByStripeCustomerId(customerId)
348348
const userId = user?.id ?? null
349349

350-
await db
350+
const result = await db
351351
.update(schema.subscription)
352352
.set({
353353
status: 'canceled',
354354
scheduled_tier: null,
355355
canceled_at: new Date(),
356356
})
357357
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
358+
.returning({ id: schema.subscription.stripe_subscription_id })
359+
360+
if (result.length === 0) {
361+
logger.warn(
362+
{ subscriptionId, customerId },
363+
'No subscription found to cancel — may not exist in our database',
364+
)
365+
// Still track the event for observability
366+
trackEvent({
367+
event: AnalyticsEvent.SUBSCRIPTION_CANCELED,
368+
userId: userId ?? 'system',
369+
properties: { subscriptionId, notFoundInDb: true },
370+
logger,
371+
})
372+
return
373+
}
358374

359375
if (userId) {
360376
await expireActiveBlockGrants({ userId, subscriptionId, logger })

0 commit comments

Comments
 (0)