120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import { sql } from "drizzle-orm";
|
|
import {
|
|
bigint,
|
|
boolean,
|
|
check,
|
|
index,
|
|
integer,
|
|
pgEnum,
|
|
pgTable,
|
|
text,
|
|
timestamp,
|
|
uniqueIndex,
|
|
uuid,
|
|
varchar,
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
import { editorialAccounts } from "./editorials";
|
|
import { billingReports } from "./billing";
|
|
|
|
export const usageBillingStatuses = ["open", "billed", "reopened", "voided"] as const;
|
|
export const usageBillingStatus = pgEnum("usage_billing_status", usageBillingStatuses);
|
|
|
|
export const downloadStatuses = [
|
|
"authorized",
|
|
"streaming",
|
|
"completed",
|
|
"failed_before_delivery",
|
|
"interrupted",
|
|
] as const;
|
|
export const downloadStatus = pgEnum("download_status", downloadStatuses);
|
|
|
|
export const usages = pgTable(
|
|
"usages",
|
|
{
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
editorialAccountId: uuid("editorial_account_id")
|
|
.notNull()
|
|
.references(() => editorialAccounts.id, { onDelete: "restrict" }),
|
|
immichAssetId: text("immich_asset_id").notNull(),
|
|
filenameSnapshot: text("filename_snapshot").notNull(),
|
|
immichAlbumIdSnapshot: text("immich_album_id_snapshot").notNull(),
|
|
albumNameSnapshot: text("album_name_snapshot").notNull(),
|
|
purpose: text("purpose").notNull(),
|
|
internalReference: text("internal_reference"),
|
|
priceCents: integer("price_cents").notNull(),
|
|
billingStatus: usageBillingStatus("billing_status").notNull().default("open"),
|
|
billingReportId: uuid("billing_report_id").references(() => billingReports.id, {
|
|
onDelete: "restrict",
|
|
}),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index("usages_editorial_created_id_idx").on(
|
|
table.editorialAccountId,
|
|
table.createdAt.desc(),
|
|
table.id.desc(),
|
|
),
|
|
check(
|
|
"usages_canonical_immich_asset_id_check",
|
|
sql`${table.immichAssetId} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'`,
|
|
),
|
|
check(
|
|
"usages_canonical_immich_album_id_check",
|
|
sql`${table.immichAlbumIdSnapshot} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'`,
|
|
),
|
|
],
|
|
);
|
|
|
|
export const downloadEvents = pgTable(
|
|
"download_events",
|
|
{
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
editorialAccountId: uuid("editorial_account_id")
|
|
.notNull()
|
|
.references(() => editorialAccounts.id, { onDelete: "restrict" }),
|
|
usageId: uuid("usage_id")
|
|
.notNull()
|
|
.references(() => usages.id, { onDelete: "restrict" }),
|
|
immichAssetId: text("immich_asset_id").notNull(),
|
|
authorizedAt: timestamp("authorized_at", { withTimezone: true }).notNull().defaultNow(),
|
|
startedAt: timestamp("started_at", { withTimezone: true }),
|
|
completedAt: timestamp("completed_at", { withTimezone: true }),
|
|
status: downloadStatus("status").notNull().default("authorized"),
|
|
transferredBytes: bigint("transferred_bytes", { mode: "number" }),
|
|
repeatDownloadNote: text("repeat_download_note"),
|
|
requestId: uuid("request_id").notNull(),
|
|
idempotencyKey: text("idempotency_key").notNull(),
|
|
intentKind: text("intent_kind").notNull().default("new_usage"),
|
|
requestFingerprint: varchar("request_fingerprint", { length: 64 }).notNull(),
|
|
legacyIntent: boolean("legacy_intent").notNull().default(false),
|
|
},
|
|
(table) => [
|
|
index("download_events_editorial_authorized_id_idx").on(
|
|
table.editorialAccountId,
|
|
table.authorizedAt.desc(),
|
|
table.id.desc(),
|
|
),
|
|
uniqueIndex("download_events_editorial_idempotency_key_unique").on(
|
|
table.editorialAccountId,
|
|
table.idempotencyKey,
|
|
),
|
|
check(
|
|
"download_events_intent_kind_check",
|
|
sql`${table.intentKind} in ('new_usage', 'repeat_download', 'legacy')`,
|
|
),
|
|
check(
|
|
"download_events_request_fingerprint_hex_check",
|
|
sql`${table.requestFingerprint} ~ '^[0-9a-f]{64}$'`,
|
|
),
|
|
check(
|
|
"download_events_canonical_idempotency_key_check",
|
|
sql`${table.legacyIntent} OR ${table.idempotencyKey} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'`,
|
|
),
|
|
check(
|
|
"download_events_canonical_immich_asset_id_check",
|
|
sql`${table.immichAssetId} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'`,
|
|
),
|
|
],
|
|
);
|