Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(billing): keep webhook-recovered BAA activation over revert
Richie McIlroydc815c771d22
diff --git a/apps/web/__tests__/unit/signed-baa.test.ts b/apps/web/__tests__/unit/signed-baa.test.tsindex b02480dd5..ae7e3184d 100644--- a/apps/web/__tests__/unit/signed-baa.test.ts+++ b/apps/web/__tests__/unit/signed-baa.test.ts@@ -339,5 +339,52 @@ describe("Signed BAA Stripe recovery", () => {expect(mockDb.set).toHaveBeenCalledWith(expect.objectContaining({ status: "pending" }),);+ expect(mockDb.where).toHaveBeenCalledWith([+ { field: "signedBaaId", value: "baa-record-1" },+ { field: "signedBaaStatus", value: "processing" },+ ]);+ });++ it("clears the stale subscription id when re-claiming a canceled record", async () => {+ mockOwner({+ stripeCustomerId: "cus_1",+ stripeSubscriptionId: "sub_active",+ stripeSubscriptionStatus: "active",+ });+ mockDb.limit.mockResolvedValueOnce([+ {+ id: "baa-record-1",+ status: "canceled",+ stripeSubscriptionId: "sub_old",+ signedAt: null,+ emailSentAt: null,+ },+ ]);+ mockDb.where+ .mockReturnValueOnce(mockDb)+ .mockReturnValueOnce(mockDb)+ .mockReturnValueOnce({ affectedRows: 1 });+ vi.mocked(generateSignedBaaPdf).mockResolvedValue(new Uint8Array([1]));+ vi.mocked(sendEmail).mockResolvedValue(undefined as never);+ mockStripe.subscriptions.retrieve.mockResolvedValue({+ id: "sub_active",+ status: "active",+ default_payment_method: "pm_1",+ });+ mockStripe.subscriptions.list.mockResolvedValue({ data: [] });+ mockStripe.subscriptions.create.mockResolvedValue(recoveredSubscription);+ const { purchaseSignedBaa } = await import(+ "@/actions/organization/signed-baa"+ );++ const result = await purchaseSignedBaa("org-1" as never, VALID_INPUT);++ expect(result.success).toBe(true);+ expect(mockDb.set).toHaveBeenCalledWith(+ expect.objectContaining({+ status: "processing",+ stripeSubscriptionId: null,+ }),+ );});});diff --git a/apps/web/actions/organization/signed-baa.ts b/apps/web/actions/organization/signed-baa.tsindex 9387b882d..344c14eba 100644--- a/apps/web/actions/organization/signed-baa.ts+++ b/apps/web/actions/organization/signed-baa.ts@@ -343,7 +343,15 @@ export async function purchaseSignedBaa(status: "processing",userId: user.id,...recordFields,- ...(isEmailRetry ? {} : { signedAt: null, emailSentAt: null }),+ // A stale ID from a canceled subscription would stop the+ // subscription.created webhook from associating the replacement.+ ...(isEmailRetry+ ? {}+ : {+ signedAt: null,+ emailSentAt: null,+ stripeSubscriptionId: null,+ }),}).where(and(@@ -379,10 +387,15 @@ export async function purchaseSignedBaa(}const revertToPending = async () => {+ // The subscription.created webhook can associate and activate the record+ // mid-flight when Stripe's create response was lost, so the revert must+ // only touch rows still claimed by this purchase attempt.await db().update(signedBaas).set({ status: existing?.status === "canceled" ? "canceled" : "pending" })- .where(eq(signedBaas.id, recordId));+ .where(+ and(eq(signedBaas.id, recordId), eq(signedBaas.status, "processing")),+ );};const signedAt = (isEmailRetry ? existing?.signedAt : null) ?? new Date();