Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete subscription only if the event is a delete event #771

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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