|
| 1 | +import { z } from "zod" |
| 2 | +import { and, Database, eq, isNull } from "./drizzle" |
| 3 | +import { LiteTable, SubscriptionTable } from "./schema/billing.sql" |
| 4 | +import { Identifier } from "./identifier" |
| 5 | +import { fn } from "./util/fn" |
| 6 | + |
| 7 | +export namespace Quota { |
| 8 | + // Zero all usage counters of one plan for a workspace, using the same |
| 9 | + // workspace-scoped addressing as unsubscribeLite/unsubscribeBlack and |
| 10 | + // Billing.subtractLiteUsage. Timestamps are left untouched on purpose: a |
| 11 | + // zeroed counter with a current-window stamp reads as empty and the next |
| 12 | + // write accumulates on top of zero, so the active windows stay undisturbed. |
| 13 | + export const reset = fn( |
| 14 | + z.object({ |
| 15 | + workspaceID: Identifier.schema("workspace"), |
| 16 | + plan: z.enum(["lite", "subscription"]), |
| 17 | + }), |
| 18 | + async (input) => { |
| 19 | + if (input.plan === "lite") { |
| 20 | + return Database.transaction(async (db) => { |
| 21 | + const where = and(eq(LiteTable.workspaceID, input.workspaceID), isNull(LiteTable.timeDeleted)) |
| 22 | + const rows = await db |
| 23 | + .select({ |
| 24 | + rollingUsage: LiteTable.rollingUsage, |
| 25 | + weeklyUsage: LiteTable.weeklyUsage, |
| 26 | + monthlyUsage: LiteTable.monthlyUsage, |
| 27 | + }) |
| 28 | + .from(LiteTable) |
| 29 | + .where(where) |
| 30 | + if (rows.length === 0) throw new Error("No lite usage counters found for workspace") |
| 31 | + |
| 32 | + await db.update(LiteTable).set({ rollingUsage: 0, weeklyUsage: 0, monthlyUsage: 0 }).where(where) |
| 33 | + return { |
| 34 | + plan: "lite" as const, |
| 35 | + before: { |
| 36 | + rollingUsage: rows.reduce((sum, row) => sum + (row.rollingUsage ?? 0), 0), |
| 37 | + weeklyUsage: rows.reduce((sum, row) => sum + (row.weeklyUsage ?? 0), 0), |
| 38 | + monthlyUsage: rows.reduce((sum, row) => sum + (row.monthlyUsage ?? 0), 0), |
| 39 | + }, |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + if (input.plan === "subscription") { |
| 44 | + return Database.transaction(async (db) => { |
| 45 | + const where = and(eq(SubscriptionTable.workspaceID, input.workspaceID), isNull(SubscriptionTable.timeDeleted)) |
| 46 | + const rows = await db |
| 47 | + .select({ rollingUsage: SubscriptionTable.rollingUsage, fixedUsage: SubscriptionTable.fixedUsage }) |
| 48 | + .from(SubscriptionTable) |
| 49 | + .where(where) |
| 50 | + if (rows.length === 0) throw new Error("No subscription usage counters found for workspace") |
| 51 | + |
| 52 | + await db.update(SubscriptionTable).set({ rollingUsage: 0, fixedUsage: 0 }).where(where) |
| 53 | + return { |
| 54 | + plan: "subscription" as const, |
| 55 | + before: { |
| 56 | + rollingUsage: rows.reduce((sum, row) => sum + (row.rollingUsage ?? 0), 0), |
| 57 | + fixedUsage: rows.reduce((sum, row) => sum + (row.fixedUsage ?? 0), 0), |
| 58 | + }, |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | + throw new Error(`Unknown plan: ${input.plan}`) |
| 63 | + }, |
| 64 | + ) |
| 65 | +} |