🪻 distributed transcription service thistle.dunkirk.sh

chore: skip canceled subscriptions on delete

dunkirk.sh a562052f 34ceb85a

verified
Changed files
+26 -7
src
+13 -3
src/index.ts
···
return;
}
-
// Update each subscription in the database
-
for (const subscription of subscriptions.result.items) {
+
// Filter to only active/trialing/past_due subscriptions (not canceled/expired)
+
const currentSubscriptions = subscriptions.result.items.filter(
+
(sub) => sub.status === 'active' || sub.status === 'trialing' || sub.status === 'past_due'
+
);
+
+
if (currentSubscriptions.length === 0) {
+
console.log(`[Sync] No current subscriptions found for customer ${customer.id}`);
+
return;
+
}
+
+
// Update each current subscription in the database
+
for (const subscription of currentSubscriptions) {
db.run(
`INSERT INTO subscriptions (id, user_id, customer_id, status, current_period_start, current_period_end, cancel_at_period_end, canceled_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
···
}
console.log(
-
`[Sync] Linked ${subscriptions.result.items.length} subscription(s) to user ${userId} (${email})`,
+
`[Sync] Linked ${currentSubscriptions.length} current subscription(s) to user ${userId} (${email})`,
);
} catch (error) {
console.error(
+13 -4
src/lib/auth.ts
···
// Get user's subscription if they have one
const subscription = db
-
.query<{ id: string }, [number]>(
-
"SELECT id FROM subscriptions WHERE user_id = ? ORDER BY created_at DESC LIMIT 1",
+
.query<{ id: string; status: string; cancel_at_period_end: number }, [number]>(
+
"SELECT id, status, cancel_at_period_end FROM subscriptions WHERE user_id = ? ORDER BY created_at DESC LIMIT 1",
)
.get(userId);
-
// Cancel subscription if it exists (soft cancel - keeps access until period end)
-
if (subscription) {
+
// Cancel subscription if it exists and is not already canceled or scheduled to cancel
+
if (
+
subscription &&
+
subscription.status !== 'canceled' &&
+
subscription.status !== 'expired' &&
+
!subscription.cancel_at_period_end
+
) {
try {
const { polar } = await import("./polar");
await polar.subscriptions.update({
···
);
// Continue with user deletion even if subscription cancellation fails
}
+
} else if (subscription) {
+
console.log(
+
`[User Delete] Skipping cancellation for subscription ${subscription.id} (status: ${subscription.status}, cancel_at_period_end: ${subscription.cancel_at_period_end})`,
+
);
}
// Reassign class transcriptions to ghost user (id=0)