Skip to content

Commit

Permalink
fix: delete subscription only if the event is a delete event (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle authored Sep 3, 2024
1 parent 6565c37 commit c194a2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions ee/apps/billing/routes/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ stripeApi.post('/webhooks', async (c) => {
orgId,
price,
active: stripeEvent.data.object.status === 'active',
deleteEvent: stripeEvent.type === 'customer.subscription.deleted',
stripeCustomerId: stripeEvent.data.object.customer as string,
stripeSubscriptionId: stripeEvent.data.object.id
});
Expand Down Expand Up @@ -77,26 +78,30 @@ type BillingRecordParams = {
stripeCustomerId: string;
stripeSubscriptionId: string;
active: boolean;
deleteEvent: boolean;
};

export const createOrUpdateBillingRecords = async ({
active,
deleteEvent,
stripeCustomerId,
orgId,
price,
stripeSubscriptionId
}: BillingRecordParams) => {
// If the subscription is canceled and the event is a delete event, we need to delete the orgBilling record
if (!active) {
if (deleteEvent) {
await db.delete(orgBilling).where(eq(orgBilling.orgId, orgId));
}
return;
}

const existingRecord = await db.query.orgBilling.findFirst({
where: eq(orgBilling.orgId, orgId),
columns: { id: true }
});

// If the subscription is canceled, we need to delete the orgBilling record
if (!active) {
await db.delete(orgBilling).where(eq(orgBilling.orgId, orgId));
return;
}

const [plan, period] = price;
const values = {
orgId,
Expand Down
3 changes: 2 additions & 1 deletion ee/apps/billing/scripts/sync-stripe-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ for (const entry of orgBillingEntries) {
price,
stripeCustomerId: subscription.customer as string,
stripeSubscriptionId: subscription.id,
active: subscription.status === 'active'
active: subscription.status === 'active',
deleteEvent: false
});
}
}
Expand Down

0 comments on commit c194a2c

Please sign in to comment.