📈 Fix prevPlan retrieve logic in Stripe webhooks

This commit is contained in:
Baptiste Arnaud 2025-01-30 20:58:47 +01:00
parent 806158541f
commit 09bbe1df41
No known key found for this signature in database

View File

@ -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,
},
})),