Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Upsert subscriptions

Luke Vella3991315541b1
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
index 6d8fe5f2b..d48d4d436 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
@@ -1,7 +1,6 @@
import type { Stripe } from "@rallly/billing";
import { prisma } from "@rallly/database";
-import { getSpace } from "@/features/spaces/queries";
import { subscriptionMetadataSchema } from "@/features/subscription/schema";
import {
getExpandedSubscription,
@@ -26,71 +25,41 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
}
const userId = res.data.userId;
+ const spaceId = res.data.spaceId;
- // Check if user already has a subscription
- const existingUser = await prisma.user.findUnique({
- where: { id: userId },
- select: {
- subscription: true,
+ // If user already has a subscription, update it or replace it
+ // Update the existing subscription with new data
+ await prisma.subscription.upsert({
+ where: { id: subscription.id },
+ create: {
+ id: subscription.id,
+ userId,
+ active: isActive,
+ priceId,
+ currency,
+ interval,
+ amount,
+ status: subscription.status,
+ createdAt: toDate(subscription.created),
+ periodStart: toDate(subscription.current_period_start),
+ periodEnd: toDate(subscription.current_period_end),
+ cancelAtPeriodEnd: subscription.cancel_at_period_end,
+ spaceId,
+ },
+ update: {
+ id: subscription.id,
+ userId,
+ active: isActive,
+ priceId,
+ currency,
+ interval,
+ amount,
+ status: subscription.status,
+ createdAt: toDate(subscription.created),
+ periodStart: toDate(subscription.current_period_start),
+ periodEnd: toDate(subscription.current_period_end),
+ cancelAtPeriodEnd: subscription.cancel_at_period_end,
+ spaceId,
},
});
-
- if (!existingUser) {
- throw new Error(`User with ID ${userId} not found`);
- }
-
- const space = await getSpace({ id: res.data.spaceId });
-
- if (!space) {
- throw new Error(`Space with ID ${res.data.spaceId} not found`);
- }
-
- if (space.ownerId !== userId) {
- throw new Error(
- `Space with ID ${res.data.spaceId} does not belong to user ${userId}`,
- );
- }
-
- const spaceId = space.id;
-
- // If user already has a subscription, update it or replace it
- if (existingUser.subscription) {
- // Update the existing subscription with new data
- await prisma.subscription.update({
- where: { id: existingUser.subscription.id },
- data: {
- id: subscription.id,
- active: isActive,
- priceId,
- currency,
- interval,
- amount,
- status: subscription.status,
- createdAt: toDate(subscription.created),
- periodStart: toDate(subscription.current_period_start),
- periodEnd: toDate(subscription.current_period_end),
- cancelAtPeriodEnd: subscription.cancel_at_period_end,
- spaceId,
- },
- });
- } else {
- // Create a new subscription for the user
- await prisma.subscription.create({
- data: {
- id: subscription.id,
- userId,
- active: isActive,
- priceId,
- currency,
- interval,
- amount,
- status: subscription.status,
- createdAt: toDate(subscription.created),
- periodStart: toDate(subscription.current_period_start),
- periodEnd: toDate(subscription.current_period_end),
- cancelAtPeriodEnd: subscription.cancel_at_period_end,
- spaceId,
- },
- });
- }
}