cloudaxe-opencode/packages/console/core/src/schema/billing.sql.ts

153 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

2026-04-18 20:26:58 +00:00
import {
bigint,
boolean,
index,
int,
json,
mysqlEnum,
mysqlTable,
primaryKey,
uniqueIndex,
varchar,
} from "drizzle-orm/mysql-core"
2025-10-09 02:33:20 +00:00
import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"
2025-08-08 17:22:54 +00:00
import { workspaceIndexes } from "./workspace.sql"
2026-02-24 09:45:39 +00:00
export const BlackPlans = ["20", "100", "200"] as const
2025-09-02 07:14:56 +00:00
export const BillingTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"billing",
{
...workspaceColumns,
...timestamps,
customerID: varchar("customer_id", { length: 255 }),
paymentMethodID: varchar("payment_method_id", { length: 255 }),
2025-10-16 18:59:44 +00:00
paymentMethodType: varchar("payment_method_type", { length: 32 }),
2025-08-08 17:22:54 +00:00
paymentMethodLast4: varchar("payment_method_last4", { length: 4 }),
balance: bigint("balance", { mode: "number" }).notNull(),
2025-09-15 18:48:00 +00:00
monthlyLimit: int("monthly_limit"),
monthlyUsage: bigint("monthly_usage", { mode: "number" }),
timeMonthlyUsageUpdated: utc("time_monthly_usage_updated"),
2025-08-08 17:22:54 +00:00
reload: boolean("reload"),
2025-10-31 18:42:18 +00:00
reloadTrigger: int("reload_trigger"),
reloadAmount: int("reload_amount"),
2025-09-15 18:48:00 +00:00
reloadError: varchar("reload_error", { length: 255 }),
timeReloadError: utc("time_reload_error"),
timeReloadLockedTill: utc("time_reload_locked_till"),
2026-01-16 22:46:32 +00:00
subscription: json("subscription").$type<{
status: "subscribed"
seats: number
2026-02-24 09:45:39 +00:00
plan: (typeof BlackPlans)[number]
2026-01-23 15:57:58 +00:00
useBalance?: boolean
coupon?: string
2026-01-16 22:46:32 +00:00
}>(),
2026-01-07 00:40:36 +00:00
subscriptionID: varchar("subscription_id", { length: 28 }),
2026-02-24 09:45:39 +00:00
subscriptionPlan: mysqlEnum("subscription_plan", BlackPlans),
2026-01-13 22:51:20 +00:00
timeSubscriptionBooked: utc("time_subscription_booked"),
2026-01-22 21:59:32 +00:00
timeSubscriptionSelected: utc("time_subscription_selected"),
2026-02-24 09:45:39 +00:00
liteSubscriptionID: varchar("lite_subscription_id", { length: 28 }),
lite: json("lite").$type<{
useBalance?: boolean
}>(),
2025-08-08 17:22:54 +00:00
},
2026-01-16 22:48:26 +00:00
(table) => [
...workspaceIndexes(table),
uniqueIndex("global_customer_id").on(table.customerID),
uniqueIndex("global_subscription_id").on(table.subscriptionID),
],
2025-08-08 17:22:54 +00:00
)
2026-01-09 00:24:20 +00:00
export const SubscriptionTable = mysqlTable(
"subscription",
{
...workspaceColumns,
...timestamps,
userID: ulid("user_id").notNull(),
rollingUsage: bigint("rolling_usage", { mode: "number" }),
fixedUsage: bigint("fixed_usage", { mode: "number" }),
timeRollingUpdated: utc("time_rolling_updated"),
timeFixedUpdated: utc("time_fixed_updated"),
},
(table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
)
2026-02-24 09:45:39 +00:00
export const LiteTable = mysqlTable(
"lite",
{
...workspaceColumns,
...timestamps,
userID: ulid("user_id").notNull(),
rollingUsage: bigint("rolling_usage", { mode: "number" }),
weeklyUsage: bigint("weekly_usage", { mode: "number" }),
monthlyUsage: bigint("monthly_usage", { mode: "number" }),
timeRollingUpdated: utc("time_rolling_updated"),
timeWeeklyUpdated: utc("time_weekly_updated"),
timeMonthlyUpdated: utc("time_monthly_updated"),
},
(table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
)
2025-09-02 07:14:56 +00:00
export const PaymentTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"payment",
{
...workspaceColumns,
...timestamps,
customerID: varchar("customer_id", { length: 255 }),
2025-09-24 23:09:23 +00:00
invoiceID: varchar("invoice_id", { length: 255 }),
2025-08-08 17:22:54 +00:00
paymentID: varchar("payment_id", { length: 255 }),
amount: bigint("amount", { mode: "number" }).notNull(),
2025-09-23 21:58:26 +00:00
timeRefunded: utc("time_refunded"),
2026-01-09 04:44:11 +00:00
enrichment: json("enrichment").$type<
| {
2026-02-22 23:41:34 +00:00
type: "subscription" | "lite"
2026-03-19 22:44:21 +00:00
currency?: "inr"
2026-01-09 04:44:11 +00:00
couponID?: string
}
| {
type: "credit"
}
>(),
2025-08-08 17:22:54 +00:00
},
(table) => [...workspaceIndexes(table)],
)
2025-09-02 07:14:56 +00:00
export const UsageTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"usage",
{
...workspaceColumns,
...timestamps,
model: varchar("model", { length: 255 }).notNull(),
2025-09-12 15:57:12 +00:00
provider: varchar("provider", { length: 255 }).notNull(),
2025-09-02 07:14:56 +00:00
inputTokens: int("input_tokens").notNull(),
outputTokens: int("output_tokens").notNull(),
reasoningTokens: int("reasoning_tokens"),
cacheReadTokens: int("cache_read_tokens"),
2025-09-12 15:57:12 +00:00
cacheWrite5mTokens: int("cache_write_5m_tokens"),
cacheWrite1hTokens: int("cache_write_1h_tokens"),
2025-08-08 17:22:54 +00:00
cost: bigint("cost", { mode: "number" }).notNull(),
2025-10-09 02:33:20 +00:00
keyID: ulid("key_id"),
2026-02-22 23:41:34 +00:00
sessionID: varchar("session_id", { length: 30 }),
2026-01-07 04:34:10 +00:00
enrichment: json("enrichment").$type<{
2026-02-22 23:41:34 +00:00
plan: "sub" | "byok" | "lite"
2026-01-07 00:40:36 +00:00
}>(),
2025-08-08 17:22:54 +00:00
},
2026-01-05 03:46:21 +00:00
(table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
2025-08-08 17:22:54 +00:00
)
2026-04-18 20:26:58 +00:00
2026-05-17 08:04:39 +00:00
export const CouponType = [
"BUILDATHON",
"GO1MONTH50",
"GOFREEMONTH",
"GO3MONTHS100",
"GO6MONTHS100",
"GO12MONTHS100",
] as const
2026-04-18 20:26:58 +00:00
export const CouponTable = mysqlTable(
"coupon",
{
email: varchar("email", { length: 255 }),
type: mysqlEnum("type", CouponType).notNull(),
timeRedeemed: utc("time_redeemed"),
},
(table) => [primaryKey({ columns: [table.email, table.type] })],
)