Skip to content

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

πŸ› Treat subscription webhooks for deleted spaces as no-ops (#3001)

Luke Vellac2229e1445dc
diff --git a/apps/web/src/features/billing/webhook/mutations.ts b/apps/web/src/features/billing/webhook/mutations.ts
index 8fce25cb6..cf7266def 100644
--- a/apps/web/src/features/billing/webhook/mutations.ts
+++ b/apps/web/src/features/billing/webhook/mutations.ts
@@ -21,6 +21,7 @@ import {
} from "@/features/billing/webhook/utils";
import { licenseManager } from "@/features/licensing/mutations";
import { licenseCheckoutMetadataSchema } from "@/features/licensing/schema";
+import { spaceExists } from "@/features/space/data";
import { identifyGroup, posthog } from "@/lib/posthog";
async function getExpandedSubscription(subscriptionId: string) {
@@ -237,6 +238,12 @@ async function onCustomerSubscriptionCreated(event: Stripe.Event) {
const { userId, spaceId } = res.data;
+ // Cascade-deleted with its owner β€” nothing to sync, and failing would
+ // make Stripe retry the event
+ if (!(await spaceExists(spaceId))) {
+ return;
+ }
+
const subscriptionItem = subscription.items.data[0];
if (!subscriptionItem) {
@@ -369,6 +376,12 @@ async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
const { userId, spaceId } = res.data;
+ // Cascade-deleted with its owner β€” nothing to sync, and failing would
+ // make Stripe retry the event
+ if (!(await spaceExists(spaceId))) {
+ return;
+ }
+
const tier = await prisma.$transaction(async (tx) => {
// updateMany so a missing row (e.g. already cascade-deleted with the user
// or space) doesn't abort the transaction before the tier sync runs
@@ -434,6 +447,12 @@ async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
const { userId, spaceId } = res.data;
+ // Cascade-deleted with its owner β€” nothing to sync, and failing would
+ // make Stripe retry the event
+ if (!(await spaceExists(spaceId))) {
+ return;
+ }
+
const subscriptionItem = subscription.items.data[0];
if (!subscriptionItem) {
diff --git a/apps/web/src/features/space/data.ts b/apps/web/src/features/space/data.ts
index 726066e04..de1083427 100644
--- a/apps/web/src/features/space/data.ts
+++ b/apps/web/src/features/space/data.ts
@@ -46,6 +46,14 @@ function createMemberDTO(member: {
} satisfies MemberDTO;
}
+export async function spaceExists(spaceId: string) {
+ const space = await prisma.space.findUnique({
+ where: { id: spaceId },
+ select: { id: true },
+ });
+ return space !== null;
+}
+
export async function getSpaceSeatCount(spaceId: string) {
return await prisma.spaceMember.count({
where: {