mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
🐛 Fix subscription id being set before subscription creation (#1475)
This commit is contained in:
parent
340215da3f
commit
27e635267e
1 changed files with 18 additions and 11 deletions
|
@ -70,7 +70,6 @@ export async function POST(request: NextRequest) {
|
|||
},
|
||||
data: {
|
||||
customerId: checkoutSession.customer as string,
|
||||
subscriptionId: checkoutSession.subscription as string,
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -150,6 +149,13 @@ export async function POST(request: NextRequest) {
|
|||
// update/create the subscription in the database
|
||||
const { price } = lineItem;
|
||||
|
||||
const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
|
||||
|
||||
if (!res.success) {
|
||||
return NextResponse.json({ error: "Missing user ID" }, { status: 400 });
|
||||
}
|
||||
|
||||
// create or update the subscription in the database
|
||||
await prisma.subscription.upsert({
|
||||
where: {
|
||||
id: subscription.id,
|
||||
|
@ -173,19 +179,20 @@ export async function POST(request: NextRequest) {
|
|||
},
|
||||
});
|
||||
|
||||
// update the user with the subscription id
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
id: res.data.userId,
|
||||
},
|
||||
data: {
|
||||
subscriptionId: subscription.id,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
|
||||
|
||||
if (!res.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing user ID" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
posthog?.capture({
|
||||
event: "subscription change",
|
||||
distinctId: res.data.userId,
|
||||
event: "subscription change",
|
||||
properties: {
|
||||
type: event.type,
|
||||
$set: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue