Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(billing): keep cancelled subscriptions entitled until the paid period ends

Andrej Acevski7e0a86b6b082
diff --git a/apps/api/src/billing/controllers/get-workspace-billing.ts b/apps/api/src/billing/controllers/get-workspace-billing.ts
index 63cba1ace..530d193eb 100644
--- a/apps/api/src/billing/controllers/get-workspace-billing.ts
+++ b/apps/api/src/billing/controllers/get-workspace-billing.ts
@@ -18,6 +18,8 @@ const ACTIVE_STATUSES = new Set([
"scheduled_cancel",
]);
+const PAID_THROUGH_PERIOD_STATUSES = new Set(["canceled"]);
+
export async function resolveTrialEndsAt(
workspaceId: string,
): Promise<Date | null> {
@@ -146,6 +148,14 @@ export function computeEntitlement(
if (billing.status && ACTIVE_STATUSES.has(billing.status)) {
return { active: true, reason: "subscription" as const };
}
+ if (
+ billing.status &&
+ PAID_THROUGH_PERIOD_STATUSES.has(billing.status) &&
+ billing.currentPeriodEnd &&
+ billing.currentPeriodEnd.getTime() > Date.now()
+ ) {
+ return { active: true, reason: "paid_period" as const };
+ }
if (billing.trialEndsAt && billing.trialEndsAt.getTime() > Date.now()) {
return { active: true, reason: "trial" as const };
}
diff --git a/apps/api/src/billing/response.ts b/apps/api/src/billing/response.ts
index e4e8c61ee..a4135b1d0 100644
--- a/apps/api/src/billing/response.ts
+++ b/apps/api/src/billing/response.ts
@@ -10,12 +10,13 @@ export const billingEntitlementSchema = z
"billing_disabled",
"founding_free",
"subscription",
+ "paid_period",
"trial",
"expired",
])
.openapi({
description:
- "Why access is granted or denied. `billing_disabled` means the instance is not running billing at all, so everything is unlocked.",
+ "Why access is granted or denied. `billing_disabled` means the instance is not running billing at all, so everything is unlocked. `paid_period` means the subscription is cancelled but its paid period has not ended yet.",
}),
})
.openapi("BillingEntitlement");
diff --git a/tests/api/billing/entitlement.test.ts b/tests/api/billing/entitlement.test.ts
index 5362d8abd..ee324231f 100644
--- a/tests/api/billing/entitlement.test.ts
+++ b/tests/api/billing/entitlement.test.ts
@@ -81,7 +81,7 @@ describe("computeEntitlement", () => {
expect(result).toEqual({ active: false, reason: "expired" });
});
- it("denies access for canceled/expired subscriptions", () => {
+ it("denies access for canceled/expired subscriptions with no paid period left", () => {
expect(computeEntitlement(billing({ status: "canceled" })).active).toBe(
false,
);
@@ -89,4 +89,37 @@ describe("computeEntitlement", () => {
false,
);
});
+
+ it("keeps a canceled subscription entitled until its paid period ends", () => {
+ const currentPeriodEnd = new Date(Date.now() + 60_000);
+ expect(
+ computeEntitlement(billing({ status: "canceled", currentPeriodEnd })),
+ ).toEqual({ active: true, reason: "paid_period" });
+ });
+
+ it("denies a canceled subscription once the paid period has passed", () => {
+ const currentPeriodEnd = new Date(Date.now() - 60_000);
+ expect(
+ computeEntitlement(billing({ status: "canceled", currentPeriodEnd })),
+ ).toEqual({ active: false, reason: "expired" });
+ });
+
+ it("does not extend the paid period to expired subscriptions", () => {
+ const currentPeriodEnd = new Date(Date.now() + 60_000);
+ expect(
+ computeEntitlement(billing({ status: "expired", currentPeriodEnd }))
+ .active,
+ ).toBe(false);
+ });
+
+ it("keeps a canceled subscription entitled even after its trial expired", () => {
+ const result = computeEntitlement(
+ billing({
+ status: "canceled",
+ currentPeriodEnd: new Date(Date.now() + 60_000),
+ trialEndsAt: new Date(Date.now() - 60_000),
+ }),
+ );
+ expect(result).toEqual({ active: true, reason: "paid_period" });
+ });
});