import type { PreferencesView, ThemePreference } from "@batch-cooking/shared"; import { prisma } from "../../db/prisma.js"; /** * A profile's personalization preferences. `SYSTEM` (the schema default) * is returned both when a row already says so *and* when there's no row * yet at all — same "absent means the default" philosophy as * `dietId`/allergies elsewhere in `profile.service.ts`, no row is created * just to read it. */ export async function getPreferences(userProfileId: number): Promise { const preferences = await prisma.userPreference.findUnique({ where: { userProfileId } }); return { theme: preferences?.theme ?? "SYSTEM" }; } /** * Sets a profile's theme preference, creating its preferences row on first * write (an `upsert` rather than requiring a separate "create" step — a * profile never needs to explicitly initialize this row before using it). */ export async function updatePreferences( userProfileId: number, theme: ThemePreference, ): Promise { const preferences = await prisma.userPreference.upsert({ where: { userProfileId }, create: { userProfileId, theme }, update: { theme }, }); return { theme: preferences.theme }; }