⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
feat(billing): reconcile drifted seat counts hourly
Andrej55f1d387f25b
diff --git a/apps/api/src/scheduler/index.ts b/apps/api/src/scheduler/index.tsindex d7afb4f8e..a53001610 100644--- a/apps/api/src/scheduler/index.ts+++ b/apps/api/src/scheduler/index.ts@@ -1,14 +1,16 @@import { Cron } from "croner";import { checkDueDateReminders } from "./due-date-reminders";import { checkProjectWebhookReminders } from "./project-webhook-reminders";+import { reconcileWorkspaceSeats } from "./seat-reconciliation";const jobs: Cron[] = [];export function initializeScheduler(): void {jobs.push(new Cron("*/5 * * * *", checkDueDateReminders));jobs.push(new Cron("*/5 * * * *", checkProjectWebhookReminders));+ jobs.push(new Cron("17 * * * *", reconcileWorkspaceSeats));console.log(- "⏰ Scheduler started (due date and project webhook reminders every 5 minutes)",+ "⏰ Scheduler started (reminders every 5 minutes, seat reconciliation hourly)",);}diff --git a/apps/api/src/scheduler/seat-reconciliation.ts b/apps/api/src/scheduler/seat-reconciliation.tsnew file mode 100644index 000000000..2f087e306--- /dev/null+++ b/apps/api/src/scheduler/seat-reconciliation.ts@@ -0,0 +1,65 @@+import { and, count, eq, inArray, isNotNull, sql } from "drizzle-orm";+import { isBillingEnabled } from "../billing/config";+import { syncWorkspaceSeats } from "../billing/controllers/sync-seats";+import db from "../database";+import { workspaceBillingTable, workspaceUserTable } from "../database/schema";++const BATCH_SIZE = 100;++async function findDriftedWorkspaces() {+ return db+ .select({ workspaceId: workspaceBillingTable.workspaceId })+ .from(workspaceBillingTable)+ .innerJoin(+ workspaceUserTable,+ eq(workspaceUserTable.workspaceId, workspaceBillingTable.workspaceId),+ )+ .where(+ and(+ eq(workspaceBillingTable.plan, "team"),+ inArray(workspaceBillingTable.status, ["active", "trialing"]),+ isNotNull(workspaceBillingTable.creemSubscriptionId),+ isNotNull(workspaceBillingTable.creemProductId),+ ),+ )+ .groupBy(workspaceBillingTable.workspaceId, workspaceBillingTable.seats)+ .having(+ sql`${count(workspaceUserTable.userId)} <> ${workspaceBillingTable.seats}`,+ )+ .limit(BATCH_SIZE);+}++export async function reconcileWorkspaceSeats(): Promise<void> {+ if (!isBillingEnabled()) {+ return;+ }++ let drifted: Awaited<ReturnType<typeof findDriftedWorkspaces>>;+ try {+ drifted = await findDriftedWorkspaces();+ } catch (error) {+ console.error("billing: seat reconciliation query failed", error);+ return;+ }++ if (drifted.length === 0) {+ return;+ }++ let repaired = 0;+ for (const { workspaceId } of drifted) {+ try {+ await syncWorkspaceSeats(workspaceId);+ repaired += 1;+ } catch (error) {+ console.error(+ `billing: seat reconciliation failed for workspace ${workspaceId}`,+ error,+ );+ }+ }++ console.log(+ `billing: seat reconciliation resynced ${repaired}/${drifted.length} workspaces`,+ );+}diff --git a/tests/api-integration/billing-seat-reconciliation.test.ts b/tests/api-integration/billing-seat-reconciliation.test.tsnew file mode 100644index 000000000..abdcaaeb2--- /dev/null+++ b/tests/api-integration/billing-seat-reconciliation.test.ts@@ -0,0 +1,155 @@+import { eq } from "drizzle-orm";+import {+ afterAll,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from "vitest";++const { updateSubscriptionSeats } = vi.hoisted(() => ({+ updateSubscriptionSeats: vi.fn(async () => ({ ok: true as const })),+}));+vi.mock("../../apps/api/src/billing/creem-client", () => ({+ updateSubscriptionSeats,+ createCheckoutSession: vi.fn(),+ createCustomerPortalLink: vi.fn(),+}));++import db, { schema } from "../../apps/api/src/database";+import { reconcileWorkspaceSeats } from "../../apps/api/src/scheduler/seat-reconciliation";+import { resetTestDatabase } from "./helpers/database";+import { createWorkspaceMember } from "./helpers/fixtures";++const CLOUD_ENV = {+ KANEO_CLOUD: "true",+ CREEM_API_KEY: "creem_test_dummy",+ CREEM_WEBHOOK_SECRET: "whsec_dummy",+};+const saved: Record<string, string | undefined> = {};++async function addMember(workspaceId: string) {+ const member = await createWorkspaceMember();+ await db.insert(schema.workspaceUserTable).values({+ workspaceId,+ userId: member.user.id,+ role: "member",+ joinedAt: new Date(),+ });+}++async function billing(+ workspaceId: string,+ overrides: Partial<typeof schema.workspaceBillingTable.$inferInsert> = {},+) {+ await db.insert(schema.workspaceBillingTable).values({+ workspaceId,+ plan: "team",+ status: "active",+ seats: 1,+ creemSubscriptionId: `sub-${workspaceId}`,+ creemProductId: "prod_team_monthly",+ ...overrides,+ });+}++async function seatsOf(workspaceId: string) {+ const [row] = await db+ .select()+ .from(schema.workspaceBillingTable)+ .where(eq(schema.workspaceBillingTable.workspaceId, workspaceId));+ return row.seats;+}++describe("API integration: seat reconciliation", () => {+ beforeAll(() => {+ for (const [k, v] of Object.entries(CLOUD_ENV)) {+ saved[k] = process.env[k];+ process.env[k] = v;+ }+ });+ afterAll(() => {+ for (const k of Object.keys(CLOUD_ENV)) {+ if (saved[k] === undefined) delete process.env[k];+ else process.env[k] = saved[k];+ }+ });+ beforeEach(async () => {+ await resetTestDatabase();+ updateSubscriptionSeats.mockClear();+ updateSubscriptionSeats.mockImplementation(async () => ({+ ok: true as const,+ }));+ });++ it("repairs a workspace whose seat count drifted from its membership", async () => {+ const owner = await createWorkspaceMember({ role: "owner" });+ await billing(owner.workspace.id, { seats: 1 });+ await addMember(owner.workspace.id);+ await addMember(owner.workspace.id); // 3 members, still billed for 1++ await reconcileWorkspaceSeats();++ expect(updateSubscriptionSeats).toHaveBeenCalledWith({+ subscriptionId: `sub-${owner.workspace.id}`,+ productId: "prod_team_monthly",+ units: 3,+ });+ expect(await seatsOf(owner.workspace.id)).toBe(3);+ });++ it("repairs drift downwards after members leave", async () => {+ const owner = await createWorkspaceMember({ role: "owner" });+ await billing(owner.workspace.id, { seats: 5 });++ await reconcileWorkspaceSeats();++ expect(updateSubscriptionSeats).toHaveBeenCalledWith(+ expect.objectContaining({ units: 1 }),+ );+ expect(await seatsOf(owner.workspace.id)).toBe(1);+ });++ it("touches nothing when every workspace is already in sync", async () => {+ const owner = await createWorkspaceMember({ role: "owner" });+ await billing(owner.workspace.id, { seats: 1 });++ await reconcileWorkspaceSeats();++ expect(updateSubscriptionSeats).not.toHaveBeenCalled();+ });++ it("ignores personal plans and inactive subscriptions", async () => {+ const personal = await createWorkspaceMember({ role: "owner" });+ await billing(personal.workspace.id, { plan: "personal", seats: 9 });++ const canceled = await createWorkspaceMember({ role: "owner" });+ await billing(canceled.workspace.id, { status: "canceled", seats: 9 });++ await reconcileWorkspaceSeats();++ expect(updateSubscriptionSeats).not.toHaveBeenCalled();+ });++ it("keeps going when one workspace fails to sync", async () => {+ const first = await createWorkspaceMember({ role: "owner" });+ await billing(first.workspace.id, { seats: 4 });+ const second = await createWorkspaceMember({ role: "owner" });+ await billing(second.workspace.id, { seats: 4 });++ updateSubscriptionSeats.mockImplementationOnce(async () => {+ throw new Error("provider unavailable");+ });++ await expect(reconcileWorkspaceSeats()).resolves.toBeUndefined();++ expect(updateSubscriptionSeats).toHaveBeenCalledTimes(2);+ const seats = [+ await seatsOf(first.workspace.id),+ await seatsOf(second.workspace.id),+ ].sort();+ expect(seats).toEqual([1, 4]);+ });+});