From 09bbe1df4167b34a7865be01e4fffffc7d322a91 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 30 Jan 2025 20:58:47 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=88=20Fix=20prevPlan=20retrieve=20logi?= =?UTF-8?q?c=20in=20Stripe=20webhooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/billing/src/api/webhookHandler.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/billing/src/api/webhookHandler.ts b/packages/billing/src/api/webhookHandler.ts index 077dc3e53..b553a3e87 100644 --- a/packages/billing/src/api/webhookHandler.ts +++ b/packages/billing/src/api/webhookHandler.ts @@ -73,7 +73,7 @@ export const webhookHandler = async ( workspaceId, userId: m.userId, data: { - prevPlan: workspace.plan, + prevPlan: Plan.FREE, plan, }, })), @@ -229,14 +229,16 @@ export const webhookHandler = async ( (invoice) => invoice.amount_due > prices["PRO"] * 100, ); - const workspaceExist = - (await prisma.workspace.count({ - where: { - stripeId: subscription.customer as string, - }, - })) > 0; + const existingWorkspace = await prisma.workspace.findFirst({ + where: { + stripeId: subscription.customer as string, + }, + select: { + plan: true, + }, + }); - if (!workspaceExist) + if (!existingWorkspace) return res.send({ message: "Workspace not found, skipping..." }); const workspace = await prisma.workspace.update({ @@ -266,7 +268,7 @@ export const webhookHandler = async ( workspaceId: workspace.id, userId: m.userId, data: { - prevPlan: workspace.plan, + prevPlan: existingWorkspace.plan, plan: Plan.FREE, }, })),