import { sql } from "drizzle-orm"; import { check, pgTable, text, timestamp } from "drizzle-orm/pg-core"; import { ekklesiaCoreUser } from "../identity/schema.js"; /** * Core-owned storage for a person's own preferences (ADR 0062). * * **One row per person, one typed column per preference.** There is deliberately no key/value * table and no JSON column: each preference has a fixed name and a closed set of values, enforced * here by a `CHECK` constraint beneath the application-layer parser * (`parseAccountPreferencesUpdate`), so nothing but a known value can be stored. A new preference is * a new nullable column in a new migration. * * **`null` means "never chosen"**, and is resolved to the platform default when read * (`resolveAccountPreferences`) rather than written as a column default. That keeps "this person * chose System" distinguishable from "this person never chose", and lets a later preference column * arrive without backfilling a value nobody chose — the same reasoning * `ekklesia_core_module_state` follows for its absent rows (ADR 0028). A person with no row at all * has chosen nothing. * * The row belongs to its person: `user_id` is the primary key and cascades on delete, so deleting * an account removes its preferences with it, and no row can exist for a person who does not. * * Personal, not church-wide. Nothing here is branding, a church theme, or an administrator's * decision for anyone else; those are Settings' (ADR 0061). */ export const ekklesiaCoreUserPreference = pgTable( "ekklesia_core_user_preference", { userId: text("user_id") .primaryKey() .references(() => ekklesiaCoreUser.id, { onDelete: "cascade" }), appearance: text("appearance"), motion: text("motion"), createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(), }, (table) => [ check( "ekklesia_core_user_preference_appearance_check", sql`${table.appearance} is null or ${table.appearance} in ('system', 'light', 'dark')`, ), check( "ekklesia_core_user_preference_motion_check", sql`${table.motion} is null or ${table.motion} in ('system', 'reduce', 'allow')`, ), ], ); /** Explicit map passed to Drizzle, mirroring `dashboardSchema` and `moduleLifecycleSchema`. The * user table is included because the account summary reads a person's own name and email from it; * nothing in this domain writes to it. */ export const accountSchema = { user: ekklesiaCoreUser, userPreference: ekklesiaCoreUserPreference, } as const;