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 type PlanStatus = "free" | "paid";
|
||||||
|
|
||||||
export function getCustomerPlanStatus(
|
export function getCustomerPlanStatus(
|
||||||
customer:
|
customer: { flags?: Record<string, unknown> } | undefined,
|
||||||
| { subscriptions?: Array<{ planId: string; status: string }> }
|
|
||||||
| undefined,
|
|
||||||
): PlanStatus {
|
): PlanStatus {
|
||||||
if (!customer?.subscriptions) return "free";
|
return customer?.flags?.[AUTUMN_PAID_PLAN_FEATURE_ID] ? "paid" : "free";
|
||||||
|
|
||||||
const hasActivePaid = customer.subscriptions.some(
|
|
||||||
(sub) => sub.planId === AUTUMN_PAID_PLAN_ID && sub.status === "active",
|
|
||||||
);
|
|
||||||
|
|
||||||
return hasActivePaid ? "paid" : "free";
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user