Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(api): run Signed BAA cleanup before user-not-found return
Richie McIlroy5a1ce9f679db
diff --git a/apps/web/__tests__/unit/signed-baa-webhook.test.ts b/apps/web/__tests__/unit/signed-baa-webhook.test.tsindex 8115e4253..7f793dd10 100644--- a/apps/web/__tests__/unit/signed-baa-webhook.test.ts+++ b/apps/web/__tests__/unit/signed-baa-webhook.test.ts@@ -233,6 +233,47 @@ describe("Stripe webhook — Signed BAA", () => {expect(mockStripe.subscriptions.cancel).not.toHaveBeenCalled();});+ it("cancels Signed BAA even when the customer maps to no user", async () => {+ mockStripe.webhooks.constructEvent.mockReturnValue({+ type: "customer.subscription.updated",+ data: {+ object: {+ id: "sub_pro",+ status: "unpaid",+ customer: "cus_1",+ metadata: {},+ },+ },+ });+ mockStripe.customers.retrieve.mockResolvedValue({+ id: "cus_1",+ email: "[email protected]",+ metadata: {},+ });+ mockStripe.subscriptions.list.mockResolvedValue({+ data: [+ {+ id: "sub_pro",+ status: "unpaid",+ 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(202);+ 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 8c4acc70a..c96ddf699 100644--- a/apps/web/app/api/webhooks/stripe/route.ts+++ b/apps/web/app/api/webhooks/stripe/route.ts@@ -488,6 +488,23 @@ export const POST = async (req: Request) => {foundUserId,);+ const subscriptions = await stripe().subscriptions.list({+ customer: customer.id,+ status: "all",+ limit: 100,+ });++ console.log("Retrieved all subscriptions:", {+ count: subscriptions.data.length,+ });++ // BAA cleanup depends only on Stripe state, so it must run even+ // when the customer cannot be mapped to a user; the 202 below is+ // treated as delivered and the event is never redelivered.+ if (!hasEntitledProSubscription(subscriptions.data)) {+ await cancelEntitledBaaSubscriptions(subscriptions.data);+ }+if (!dbUser) {console.log("No user found after all retries. Returning 202 to allow retry.",@@ -503,16 +520,6 @@ export const POST = async (req: Request) => {name: dbUser.name,});- const subscriptions = await stripe().subscriptions.list({- customer: customer.id,- status: "all",- limit: 100,- });-- console.log("Retrieved all subscriptions:", {- count: subscriptions.data.length,- });-// Quota follows entitlement: past_due keeps its seats during the// dunning window instead of collapsing the org to zero while// Stripe retries the card.@@ -553,10 +560,6 @@ export const POST = async (req: Request) => {"Successfully updated user in database with new invite quota:",inviteQuota,);-- if (!hasEntitledProSubscription(subscriptions.data)) {- await cancelEntitledBaaSubscriptions(subscriptions.data);- }}if (event.type === "invoice.payment_failed") {