Detect paid plans from Autumn entitlement flags (#482)
This commit is contained in:
parent
a1ed6ece4b
commit
75aec8424f
22
src/client/features/billing/plan-detection.test.ts
Normal file
22
src/client/features/billing/plan-detection.test.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { AUTUMN_PAID_PLAN_FEATURE_ID } from "@/shared/billing";
|
||||
import { getCustomerPlanStatus } from "./plan-detection";
|
||||
|
||||
describe("getCustomerPlanStatus", () => {
|
||||
it("treats a customer without the paid entitlement as free", () => {
|
||||
expect(getCustomerPlanStatus(undefined)).toBe("free");
|
||||
expect(getCustomerPlanStatus({ flags: {} })).toBe("free");
|
||||
});
|
||||
|
||||
it("treats any plan granting the paid entitlement as paid", () => {
|
||||
expect(
|
||||
getCustomerPlanStatus({
|
||||
flags: {
|
||||
[AUTUMN_PAID_PLAN_FEATURE_ID]: {
|
||||
planId: "friends_and_family_2",
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toBe("paid");
|
||||
});
|
||||
});
|
||||
@ -1,17 +1,9 @@
|
||||
import { AUTUMN_PAID_PLAN_ID } from "@/shared/billing";
|
||||
import { AUTUMN_PAID_PLAN_FEATURE_ID } from "@/shared/billing";
|
||||
|
||||
export type PlanStatus = "free" | "paid";
|
||||
|
||||
export function getCustomerPlanStatus(
|
||||
customer:
|
||||
| { subscriptions?: Array<{ planId: string; status: string }> }
|
||||
| undefined,
|
||||
customer: { flags?: Record<string, unknown> } | undefined,
|
||||
): PlanStatus {
|
||||
if (!customer?.subscriptions) return "free";
|
||||
|
||||
const hasActivePaid = customer.subscriptions.some(
|
||||
(sub) => sub.planId === AUTUMN_PAID_PLAN_ID && sub.status === "active",
|
||||
);
|
||||
|
||||
return hasActivePaid ? "paid" : "free";
|
||||
return customer?.flags?.[AUTUMN_PAID_PLAN_FEATURE_ID] ? "paid" : "free";
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user