From db2823504f2386a198998d75e51ffa654143fe8c Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 27 Oct 2025 09:55:25 +0100 Subject: [PATCH] fix code rabbit issue --- .../api/lib/checkout-session-completed.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/web/modules/ee/billing/api/lib/checkout-session-completed.ts b/apps/web/modules/ee/billing/api/lib/checkout-session-completed.ts index 3be7d466d7..b7e29fc377 100644 --- a/apps/web/modules/ee/billing/api/lib/checkout-session-completed.ts +++ b/apps/web/modules/ee/billing/api/lib/checkout-session-completed.ts @@ -17,10 +17,19 @@ export const handleCheckoutSessionCompleted = async (event: Stripe.Event) => { if (!organization) throw new ResourceNotFoundError("Organization not found", checkoutSession.metadata.organizationId); - // Retrieve subscription to get billing interval - const subscription = await stripe.subscriptions.retrieve(checkoutSession.subscription as string); - const interval = subscription.items.data[0].price.recurring?.interval; - const period = interval === "year" ? "yearly" : "monthly"; + // Retrieve subscription to get billing interval with expanded price data + const subscription = await stripe.subscriptions.retrieve(checkoutSession.subscription as string, { + expand: ["items.data.price"], + }); + + // Defensively check subscription items and default to monthly if missing + let period: "monthly" | "yearly" = "monthly"; + + if (subscription.items?.data && subscription.items.data.length > 0) { + const firstItem = subscription.items.data[0]; + const interval = firstItem.price?.recurring?.interval; + period = interval === "year" ? "yearly" : "monthly"; + } // Update organization with Startup plan and hardcoded limits await updateOrganization(checkoutSession.metadata.organizationId, {