mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-21 18:27:53 +02:00
🗃️ Add new fields to subscription (#1564)
This commit is contained in:
parent
e022c4c279
commit
7cf578bedf
5 changed files with 289 additions and 198 deletions
|
@ -49,6 +49,7 @@ export async function POST(request: NextRequest) {
|
|||
);
|
||||
}
|
||||
|
||||
try {
|
||||
switch (event.type) {
|
||||
case "checkout.session.completed": {
|
||||
const checkoutSession = event.data.object as Stripe.Checkout.Session;
|
||||
|
@ -58,7 +59,9 @@ export async function POST(request: NextRequest) {
|
|||
break;
|
||||
}
|
||||
|
||||
const { userId } = checkoutMetadataSchema.parse(checkoutSession.metadata);
|
||||
const { userId } = checkoutMetadataSchema.parse(
|
||||
checkoutSession.metadata,
|
||||
);
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
|
@ -84,7 +87,7 @@ export async function POST(request: NextRequest) {
|
|||
distinctId: userId,
|
||||
event: "upgrade",
|
||||
properties: {
|
||||
interval: subscription.items.data[0].plan.interval,
|
||||
interval: subscription.items.data[0].price.recurring?.interval,
|
||||
$set: {
|
||||
tier: "pro",
|
||||
},
|
||||
|
@ -155,9 +158,20 @@ export async function POST(request: NextRequest) {
|
|||
const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
|
||||
|
||||
if (!res.success) {
|
||||
return NextResponse.json({ error: "Missing user ID" }, { status: 400 });
|
||||
return NextResponse.json(
|
||||
{ error: "Missing user ID" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const subscriptionItem = subscription.items.data[0];
|
||||
const interval = subscriptionItem.price.recurring?.interval;
|
||||
|
||||
if (!interval) {
|
||||
throw new Error(
|
||||
`Missing interval in subscription ${subscription.id}`,
|
||||
);
|
||||
}
|
||||
// create or update the subscription in the database
|
||||
await prisma.subscription.upsert({
|
||||
where: {
|
||||
|
@ -167,7 +181,10 @@ export async function POST(request: NextRequest) {
|
|||
id: subscription.id,
|
||||
active: isActive,
|
||||
priceId: price.id,
|
||||
currency: subscription.currency ?? null,
|
||||
currency: subscriptionItem.price.currency,
|
||||
interval,
|
||||
amount: subscriptionItem.price.unit_amount,
|
||||
status: subscription.status,
|
||||
createdAt: toDate(subscription.created),
|
||||
periodStart: toDate(subscription.current_period_start),
|
||||
periodEnd: toDate(subscription.current_period_end),
|
||||
|
@ -175,7 +192,10 @@ export async function POST(request: NextRequest) {
|
|||
update: {
|
||||
active: isActive,
|
||||
priceId: price.id,
|
||||
currency: subscription.currency ?? null,
|
||||
currency: subscriptionItem.price.currency,
|
||||
interval,
|
||||
amount: subscriptionItem.price.unit_amount,
|
||||
status: subscription.status,
|
||||
createdAt: toDate(subscription.created),
|
||||
periodStart: toDate(subscription.current_period_start),
|
||||
periodEnd: toDate(subscription.current_period_end),
|
||||
|
@ -219,7 +239,9 @@ export async function POST(request: NextRequest) {
|
|||
const userId = session.metadata?.userId;
|
||||
if (!userId) {
|
||||
console.info("No user ID found in Checkout Session metadata");
|
||||
Sentry.captureMessage("No user ID found in Checkout Session metadata");
|
||||
Sentry.captureMessage(
|
||||
"No user ID found in Checkout Session metadata",
|
||||
);
|
||||
break;
|
||||
}
|
||||
// Do nothing if the Checkout Session has no email or recovery URL
|
||||
|
@ -277,13 +299,21 @@ export async function POST(request: NextRequest) {
|
|||
break;
|
||||
}
|
||||
default:
|
||||
Sentry.captureException(new Error(`Unhandled event type: ${event.type}`));
|
||||
Sentry.captureException(
|
||||
new Error(`Unhandled event type: ${event.type}`),
|
||||
);
|
||||
// Unexpected event type
|
||||
return NextResponse.json(
|
||||
{ error: "Unhandled event type" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
const error =
|
||||
err instanceof Error ? err.message : "An unexpected error occurred";
|
||||
Sentry.captureException(err);
|
||||
return NextResponse.json({ error }, { status: 500 });
|
||||
}
|
||||
|
||||
waitUntil(Promise.all([posthog?.shutdown()]));
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
"scripts": {
|
||||
"normalize-subscription-metadata": "dotenv -e ../../.env -- tsx ./src/scripts/normalize-metadata.ts",
|
||||
"checkout-expiry": "dotenv -e ../../.env -- tsx ./src/scripts/checkout-expiry.ts",
|
||||
"subscription-data-sync": "dotenv -e ../../.env -- tsx ./src/scripts/subscription-data-sync.ts",
|
||||
"type-check": "tsc --pretty --noEmit",
|
||||
"lint": "eslint ./src"
|
||||
},
|
||||
|
|
55
packages/billing/src/scripts/subscription-data-sync.ts
Normal file
55
packages/billing/src/scripts/subscription-data-sync.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import { prisma } from "@rallly/database";
|
||||
|
||||
import { stripe } from "../lib/stripe";
|
||||
|
||||
(async function syncSubscriptionData() {
|
||||
const BATCH_SIZE = 10;
|
||||
let processed = 0;
|
||||
let failed = 0;
|
||||
|
||||
const userSubscriptions = await prisma.subscription.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
take: BATCH_SIZE,
|
||||
});
|
||||
|
||||
console.info(`🚀 Syncing ${userSubscriptions.length} subscriptions...`)
|
||||
|
||||
for (const userSubscription of userSubscriptions) {
|
||||
try {
|
||||
const subscription = await stripe.subscriptions.retrieve(
|
||||
userSubscription.id,
|
||||
);
|
||||
|
||||
const subscriptionItem = subscription.items.data[0];
|
||||
const interval = subscriptionItem.price.recurring?.interval;
|
||||
|
||||
if (!interval) {
|
||||
console.info(`🚨 Missing interval in subscription ${subscription.id}`);
|
||||
+ failed++;
|
||||
continue;
|
||||
}
|
||||
|
||||
await prisma.subscription.update({
|
||||
where: {
|
||||
id: subscription.id,
|
||||
},
|
||||
data: {
|
||||
amount: subscriptionItem.price.unit_amount,
|
||||
currency: subscriptionItem.price.currency,
|
||||
interval: subscriptionItem.price.recurring?.interval,
|
||||
status: subscription.status,
|
||||
},
|
||||
});
|
||||
|
||||
console.info(`✅ Subscription ${subscription.id} synced`);
|
||||
processed++;
|
||||
} catch (error) {
|
||||
console.error(`❌ Failed to sync subscription ${userSubscription.id}:`, error);
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
|
||||
console.info(`📊 Sync complete: ${processed} processed, ${failed} failed`);
|
||||
})();
|
|
@ -0,0 +1,3 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "subscriptions" ADD COLUMN "amount" INTEGER,
|
||||
ADD COLUMN "status" TEXT;
|
|
@ -88,6 +88,8 @@ model UserPaymentData {
|
|||
model Subscription {
|
||||
id String @id
|
||||
priceId String @map("price_id")
|
||||
amount Int?
|
||||
status String?
|
||||
active Boolean
|
||||
currency String?
|
||||
interval String?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue