Skip to content

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

🐛 Derive space tier from active subscriptions in Stripe webhooks (#2452)

Luke Vella17445ea9df69
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 dbe643771..5cf157614 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
@@ -9,6 +9,7 @@ import {
getExpandedSubscription,
getSubscriptionDetails,
isSubscriptionActive,
+ syncSpaceTier,
toDate,
} from "../utils";
@@ -18,7 +19,6 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
);
const isActive = isSubscriptionActive(subscription);
- const tier = isActive ? "pro" : "hobby";
const { priceId, currency, interval, amount } =
getSubscriptionDetails(subscription);
@@ -41,7 +41,7 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
const subscriptionItemId = subscriptionItem.id;
const quantity = subscriptionItem.quantity ?? 1;
- await prisma.$transaction(async (tx) => {
+ const tier = await prisma.$transaction(async (tx) => {
await tx.subscription.upsert({
where: { id: subscription.id },
create: {
@@ -83,14 +83,7 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
},
});
- await tx.space.update({
- where: {
- id: spaceId,
- },
- data: {
- tier,
- },
- });
+ return syncSpaceTier(tx, spaceId);
});
posthog()?.groupIdentify({
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
index 2b720725a..2557170ac 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
@@ -3,6 +3,7 @@ import { stripe } from "@rallly/billing";
import { prisma } from "@rallly/database";
import { posthog } from "@/features/analytics/posthog";
import { subscriptionMetadataSchema } from "@/features/subscription/schema";
+import { syncSpaceTier } from "../utils";
export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
const subscription = await stripe.subscriptions.retrieve(
@@ -27,7 +28,7 @@ export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
const { userId, spaceId } = res.data;
- await prisma.$transaction(async (tx) => {
+ const tier = await prisma.$transaction(async (tx) => {
await tx.subscription.update({
where: {
id: subscription.id,
@@ -38,22 +39,14 @@ export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
},
});
- await tx.space.update({
- where: {
- id: spaceId,
- },
- data: {
- tier: "hobby",
- showBranding: false,
- },
- });
+ return syncSpaceTier(tx, spaceId);
});
posthog()?.groupIdentify({
groupType: "space",
groupKey: spaceId,
properties: {
- tier: "hobby",
+ tier,
},
});
@@ -62,7 +55,7 @@ export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
event: "subscription_cancel",
properties: {
$set: {
- tier: "hobby",
+ tier,
},
},
groups: {
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
index 13dbda01e..156c090ee 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
@@ -6,6 +6,7 @@ import {
getExpandedSubscription,
getSubscriptionDetails,
isSubscriptionActive,
+ syncSpaceTier,
toDate,
} from "../utils";
@@ -30,8 +31,6 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
const { userId, spaceId } = res.data;
- const tier = isActive ? "pro" : "hobby";
-
const subscriptionItem = subscription.items.data[0];
if (!subscriptionItem) {
@@ -42,7 +41,7 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
const quantity = subscriptionItem.quantity ?? 1;
// Update the subscription in the database
- await prisma.$transaction(async (tx) => {
+ const tier = await prisma.$transaction(async (tx) => {
await tx.subscription.upsert({
where: {
id: subscription.id,
@@ -78,15 +77,7 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
},
});
- await tx.space.update({
- where: {
- id: spaceId,
- },
- data: {
- tier,
- ...(tier === "hobby" ? { showBranding: false } : {}),
- },
- });
+ return syncSpaceTier(tx, spaceId);
});
posthog()?.groupIdentify({
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/utils.ts b/apps/web/src/app/api/stripe/webhook/handlers/utils.ts
index 861874de7..d63d59030 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/utils.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/utils.ts
@@ -22,6 +22,34 @@ export function isSubscriptionActive(subscription: Stripe.Subscription) {
);
}
+/**
+ * Recomputes a space's tier from its subscriptions so the tier reflects whether
+ * *any* subscription is active, rather than the state of a single webhook event.
+ * Must run after the triggering subscription's `active` flag has been written in
+ * the same transaction. Returns the resolved tier.
+ */
+export async function syncSpaceTier(
+ tx: Prisma.TransactionClient,
+ spaceId: string,
+) {
+ const activeSubscription = await tx.subscription.findFirst({
+ where: { spaceId, active: true },
+ select: { id: true },
+ });
+
+ const tier = activeSubscription ? "pro" : "hobby";
+
+ await tx.space.update({
+ where: { id: spaceId },
+ data: {
+ tier,
+ ...(tier === "hobby" ? { showBranding: false } : {}),
+ },
+ });
+
+ return tier;
+}
+
export function getSubscriptionDetails(subscription: Stripe.Subscription) {
const subscriptionItem = subscription.items.data[0];
const interval = subscriptionItem.price.recurring?.interval;