feat: add portal database schema
This commit is contained in:
1 parent
763098b129
commit
7acaacb92e
17 files changed
+4410
-111
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import {
|
||||
check,
|
||||
customType,
|
||||
integer,
|
||||
jsonb,
|
||||
pgEnum,
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
uniqueIndex,
|
||||
uuid,
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
import { users } from "./auth";
|
||||
import { editorialAccounts } from "./editorials";
|
||||
import { usages } from "./usages";
|
||||
|
||||
export const reportStatuses = ["draft", "final", "cancelled"] as const;
|
||||
export const reportStatus = pgEnum("report_status", reportStatuses);
|
||||
|
||||
const bytea = customType<{ data: Buffer; driverData: Buffer }>({
|
||||
dataType() {
|
||||
return "bytea";
|
||||
},
|
||||
});
|
||||
|
||||
export const billingReports = pgTable(
|
||||
"billing_reports",
|
||||
{
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
reportNumber: integer("report_number").notNull(),
|
||||
editorialAccountId: uuid("editorial_account_id")
|
||||
.notNull()
|
||||
.references(() => editorialAccounts.id, { onDelete: "restrict" }),
|
||||
periodStartsAt: timestamp("period_starts_at", { withTimezone: true }).notNull(),
|
||||
periodEndsAt: timestamp("period_ends_at", { withTimezone: true }).notNull(),
|
||||
billingAddressSnapshot: jsonb("billing_address_snapshot").notNull(),
|
||||
invoiceNumber: text("invoice_number"),
|
||||
netTotalCents: integer("net_total_cents").notNull(),
|
||||
status: reportStatus("status").notNull().default("draft"),
|
||||
pdf: bytea("pdf"),
|
||||
pdfSha256: varchar("pdf_sha256", { length: 64 }),
|
||||
createdByUserId: uuid("created_by_user_id")
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: "restrict" }),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
finalizedAt: timestamp("finalized_at", { withTimezone: true }),
|
||||
cancelledAt: timestamp("cancelled_at", { withTimezone: true }),
|
||||
cancellationReason: text("cancellation_reason"),
|
||||
cancelledByUserId: uuid("cancelled_by_user_id").references(() => users.id, {
|
||||
onDelete: "restrict",
|
||||
}),
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex("billing_reports_report_number_unique").on(table.reportNumber),
|
||||
uniqueIndex("billing_reports_invoice_number_unique").on(table.invoiceNumber),
|
||||
check("billing_reports_pdf_sha256_hex_check", sql`${table.pdfSha256} ~ '^[0-9a-f]{64}$'`),
|
||||
check(
|
||||
"billing_reports_final_pdf_check",
|
||||
sql`${table.status} <> 'final' OR (${table.pdf} IS NOT NULL AND ${table.pdfSha256} IS NOT NULL)`,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
export const billingReportItems = pgTable(
|
||||
"billing_report_items",
|
||||
{
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
billingReportId: uuid("billing_report_id")
|
||||
.notNull()
|
||||
.references(() => billingReports.id, { onDelete: "restrict" }),
|
||||
usageId: uuid("usage_id")
|
||||
.notNull()
|
||||
.references(() => usages.id, { onDelete: "restrict" }),
|
||||
immichAssetIdSnapshot: text("immich_asset_id_snapshot").notNull(),
|
||||
filenameSnapshot: text("filename_snapshot").notNull(),
|
||||
albumNameSnapshot: text("album_name_snapshot").notNull(),
|
||||
purposeSnapshot: text("purpose_snapshot").notNull(),
|
||||
internalReferenceSnapshot: text("internal_reference_snapshot"),
|
||||
priceCents: integer("price_cents").notNull(),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex("billing_report_items_report_usage_unique").on(
|
||||
table.billingReportId,
|
||||
table.usageId,
|
||||
),
|
||||
],
|
||||
);
|
||||
Reference in new issue
Block a user