Files
immich-redaktions-plugin/src/db/schema/billing.ts
T

112 lines
4.2 KiB
TypeScript

import { sql } from "drizzle-orm";
import {
boolean,
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(),
flatrateCentsSnapshot: integer("flatrate_cents_snapshot"),
status: reportStatus("status").notNull().default("draft"),
pdf: bytea("pdf"),
pdfSha256: varchar("pdf_sha256", { length: 64 }),
// The actual VAT invoice is uploaded by the admin AFTER finalization.
// Uploading it triggers the bundled email (invoice + Nutzungsnachweis).
invoicePdf: bytea("invoice_pdf"),
invoicePdfSha256: varchar("invoice_pdf_sha256", { length: 64 }),
invoicePdfFilename: text("invoice_pdf_filename"),
invoiceUploadedAt: timestamp("invoice_uploaded_at", { withTimezone: true }),
invoiceEmailStatus: text("invoice_email_status").notNull().default("not_sent"),
invoiceEmailSentAt: timestamp("invoice_email_sent_at", { withTimezone: true }),
invoiceEmailError: text("invoice_email_error"),
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_invoice_pdf_sha256_hex_check",
sql`${table.invoicePdfSha256} IS NULL OR ${table.invoicePdfSha256} ~ '^[0-9a-f]{64}$'`,
),
check(
"billing_reports_invoice_email_status_check",
sql`${table.invoiceEmailStatus} in ('not_sent', 'sent', 'failed')`,
),
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(),
collab: boolean("collab").notNull().default(false),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
},
(table) => [
uniqueIndex("billing_report_items_report_usage_unique").on(
table.billingReportId,
table.usageId,
),
],
);