Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(api): run BAA cleanup before deletion user-mapping returns
Richie McIlroy86f9c9844b2a
diff --git a/apps/web/__tests__/unit/signed-baa-webhook.test.ts b/apps/web/__tests__/unit/signed-baa-webhook.test.tsindex 7f793dd10..46684e0f3 100644--- a/apps/web/__tests__/unit/signed-baa-webhook.test.ts+++ b/apps/web/__tests__/unit/signed-baa-webhook.test.ts@@ -274,6 +274,47 @@ describe("Stripe webhook — Signed BAA", () => {expect(mockStripe.subscriptions.cancel).toHaveBeenCalledWith("sub_baa");});+ it("cancels Signed BAA on Pro deletion even without a user mapping", async () => {+ mockStripe.webhooks.constructEvent.mockReturnValue({+ type: "customer.subscription.deleted",+ data: {+ object: {+ id: "sub_pro",+ status: "canceled",+ customer: "cus_1",+ metadata: {},+ },+ },+ });+ mockStripe.customers.retrieve.mockResolvedValue({+ id: "cus_1",+ email: null,+ metadata: {},+ });+ mockStripe.subscriptions.list.mockResolvedValue({+ data: [+ {+ id: "sub_pro",+ status: "canceled",+ metadata: {},+ items: { data: [{ quantity: 1 }] },+ },+ {+ id: "sub_baa",+ status: "active",+ metadata: { type: "signed_baa" },+ items: { data: [{ quantity: 1 }] },+ },+ ],+ });+ mockStripe.subscriptions.cancel.mockResolvedValue({ id: "sub_baa" });++ const res = await POST(makeWebhookRequest());++ expect(res.status).toBe(400);+ expect(mockStripe.subscriptions.cancel).toHaveBeenCalledWith("sub_baa");+ });+it("fails the webhook when Signed BAA cancellation is rejected", async () => {mockStripe.webhooks.constructEvent.mockReturnValue({type: "customer.subscription.updated",diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.tsindex c96ddf699..93ec7ddc5 100644--- a/apps/web/app/api/webhooks/stripe/route.ts+++ b/apps/web/app/api/webhooks/stripe/route.ts@@ -658,6 +658,19 @@ export const POST = async (req: Request) => {const customer = await stripe().customers.retrieve(subscription.customer as string,);++ // BAA cleanup depends only on Stripe state; it must run before the+ // user-mapping early returns so an unmappable customer can't keep+ // an active BAA billing after their last Pro subscription ends.+ const remainingSubscriptions = await stripe().subscriptions.list({+ customer: customer.id,+ status: "all",+ limit: 100,+ });+ if (!hasEntitledProSubscription(remainingSubscriptions.data)) {+ await cancelEntitledBaaSubscriptions(remainingSubscriptions.data);+ }+let foundUserId: User.UserId | undefined;if ("metadata" in customer) {foundUserId = customer.metadata.userId@@ -716,15 +729,6 @@ export const POST = async (req: Request) => {foundUserId,inviteQuota: 1,});-- const remainingSubscriptions = await stripe().subscriptions.list({- customer: customer.id,- status: "all",- limit: 100,- });- if (!hasEntitledProSubscription(remainingSubscriptions.data)) {- await cancelEntitledBaaSubscriptions(remainingSubscriptions.data);- }}return NextResponse.json({ received: true });