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 { try { const preferences = await prisma.userPreference.findUnique({ where: { userProfileId }, }); return { theme: preferences?.theme ?? "SYSTEM" }; } catch (err) { // Rethrown as-is — `wrapAsyncHandler`/the error middleware (which // already logs it, see `error-logger.ts`) is what actually handles it, // this service layer just isn't allowed a bare `await` per the repo's // async/try-catch convention. throw err; } } /** * 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 { try { const preferences = await prisma.userPreference.upsert({ where: { userProfileId }, create: { userProfileId, theme }, update: { theme }, }); return { theme: preferences.theme }; } catch (err) { throw err; // see getPreferences()'s catch comment above } }