Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(billing): ignore third-party IDs for Signed BAA

Richie McIlroydbc7dd03c493
diff --git a/apps/web/__tests__/unit/signed-baa.test.ts b/apps/web/__tests__/unit/signed-baa.test.ts
index 3e45798d5..7044bdf67 100644
--- a/apps/web/__tests__/unit/signed-baa.test.ts
+++ b/apps/web/__tests__/unit/signed-baa.test.ts
@@ -179,6 +179,41 @@ describe("Signed BAA entitlement", () => {
).rejects.toThrow("active Cap Pro subscription");
expect(mockStripe.subscriptions.create).not.toHaveBeenCalled();
});
+
+ it("does not treat a leftover third-party subscription id as Cap Pro", async () => {
+ mockOwner({
+ stripeCustomerId: "cus_1",
+ stripeSubscriptionId: "sub_canceled",
+ stripeSubscriptionStatus: "canceled",
+ thirdPartyStripeSubscriptionId: "sub_other_org",
+ });
+ mockDb.limit.mockResolvedValueOnce([]);
+ const { getSignedBaaStatus, purchaseSignedBaa } = await import(
+ "@/actions/organization/signed-baa"
+ );
+
+ const status = await getSignedBaaStatus("org-1" as never);
+ expect(status.canPurchase).toBe(false);
+
+ mockOwner({
+ stripeCustomerId: "cus_1",
+ stripeSubscriptionId: "sub_canceled",
+ stripeSubscriptionStatus: "canceled",
+ thirdPartyStripeSubscriptionId: "sub_other_org",
+ });
+ await expect(
+ purchaseSignedBaa("org-1" as never, {
+ entityName: "Acme Health, Inc.",
+ entityType: "Delaware corporation",
+ entityAddress: "123 Main St, San Francisco, CA 94105",
+ signerName: "Jane Smith",
+ signerTitle: "CEO",
+ noticesEmail: "[email protected]",
+ signatureDataUrl: `data:image/png;base64,${"A".repeat(200)}`,
+ }),
+ ).rejects.toThrow("active Cap Pro subscription");
+ expect(mockStripe.subscriptions.create).not.toHaveBeenCalled();
+ });
});
const VALID_INPUT = {
diff --git a/apps/web/actions/organization/signed-baa.ts b/apps/web/actions/organization/signed-baa.ts
index 29b5c4178..50b0dc9e4 100644
--- a/apps/web/actions/organization/signed-baa.ts
+++ b/apps/web/actions/organization/signed-baa.ts
@@ -7,7 +7,7 @@ import { SignedBaa } from "@cap/database/emails/signed-baa";
import { nanoId } from "@cap/database/helpers";
import { organizations, signedBaas } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
-import { STRIPE_SIGNED_BAA_PRICE_IDS, stripe, userIsPro } from "@cap/utils";
+import { STRIPE_SIGNED_BAA_PRICE_IDS, stripe } from "@cap/utils";
import type { Organisation } from "@cap/web-domain";
import { and, eq, lt, ne, or } from "drizzle-orm";
import { revalidatePath } from "next/cache";
@@ -20,6 +20,24 @@ import {
const BAA_NOTICE_EMAIL = "[email protected]";
const PROCESSING_STALE_MS = 2 * 60 * 1000;
const ENTITLED_STATUSES = new Set(["active", "trialing", "past_due"]);
+const CAP_PRO_STATUSES = new Set([
+ "active",
+ "trialing",
+ "complete",
+ "paid",
+ "past_due",
+]);
+
+function ownerCanPurchaseSignedBaa(user: {
+ stripeCustomerId?: string | null;
+ stripeSubscriptionStatus?: string | null;
+}) {
+ return Boolean(
+ user.stripeCustomerId &&
+ user.stripeSubscriptionStatus &&
+ CAP_PRO_STATUSES.has(user.stripeSubscriptionStatus),
+ );
+}
const getAffectedRows = (result: unknown) => {
if (Array.isArray(result)) {
@@ -106,7 +124,7 @@ export async function getSignedBaaStatus(
status === "active" && record?.emailSentAt
? record.emailSentAt.toISOString()
: null,
- canPurchase: Boolean(user.stripeCustomerId && userIsPro(user)),
+ canPurchase: ownerCanPurchaseSignedBaa(user),
};
}
@@ -274,7 +292,7 @@ export async function purchaseSignedBaa(
const { user } = await getOwnerContext(organizationId);
const priceId = getBaaPriceId();
- if (!user.stripeCustomerId || !userIsPro(user)) {
+ if (!ownerCanPurchaseSignedBaa(user)) {
throw new Error(
"Your organization needs an active Cap Pro subscription before adding the Signed BAA add-on.",
);