import { relations } from "drizzle-orm"; import { type AnyPgColumn, integer, pgEnum, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core"; import { users } from "./auth"; export const accountStatuses = [ "pending_email", "pending_approval", "active", "suspended", "rejected", ] as const; export const accountStatus = pgEnum("account_status", accountStatuses); export const editorialAccounts = pgTable("editorial_accounts", { id: uuid("id").defaultRandom().primaryKey(), userId: uuid("user_id") .notNull() .unique() .references(() => users.id, { onDelete: "restrict" }), name: text("name").notNull(), contactName: text("contact_name").notNull(), loginEmail: text("login_email").notNull().unique(), status: accountStatus("status").notNull().default("pending_email"), currentPriceCents: integer("current_price_cents"), currentBillingAddressVersionId: uuid("current_billing_address_version_id").references( (): AnyPgColumn => billingAddressVersions.id, { onDelete: "restrict" }, ), emailVerifiedAt: timestamp("email_verified_at", { withTimezone: true }), acceptedTermsVersion: text("accepted_terms_version"), acceptedTermsAt: timestamp("accepted_terms_at", { withTimezone: true }), privacyAcknowledgedAt: timestamp("privacy_acknowledged_at", { withTimezone: true }), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), }); export const billingAddressVersions = pgTable("billing_address_versions", { id: uuid("id").defaultRandom().primaryKey(), editorialAccountId: uuid("editorial_account_id") .notNull() .references(() => editorialAccounts.id, { onDelete: "restrict" }), companyName: text("company_name").notNull(), contactName: text("contact_name").notNull(), addressLine1: text("address_line_1").notNull(), addressLine2: text("address_line_2"), postalCode: text("postal_code").notNull(), city: text("city").notNull(), countryCode: text("country_code").notNull(), validFrom: timestamp("valid_from", { withTimezone: true }).notNull().defaultNow(), supersededById: uuid("superseded_by_id").references((): AnyPgColumn => billingAddressVersions.id, { onDelete: "restrict", }), createdByUserId: uuid("created_by_user_id").references(() => users.id, { onDelete: "restrict", }), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }); export const editorialAccountsRelations = relations(editorialAccounts, ({ one, many }) => ({ user: one(users, { fields: [editorialAccounts.userId], references: [users.id], }), billingAddressVersions: many(billingAddressVersions), })); export const billingAddressVersionsRelations = relations(billingAddressVersions, ({ one }) => ({ editorialAccount: one(editorialAccounts, { fields: [billingAddressVersions.editorialAccountId], references: [editorialAccounts.id], }), supersededBy: one(billingAddressVersions, { fields: [billingAddressVersions.supersededById], references: [billingAddressVersions.id], relationName: "supersededBillingAddressVersion", }), }));