batchCooking/apps/api/src/modules/preferences/preferences.service.ts
Nicolas 3acde696f5 API: module preferences — GET/PATCH /preferences (step 2/4)
- getPreferences: SYSTEM par défaut si aucune ligne (même logique que
  dietId/allergies : absent = valeur par défaut, pas une omission)
- updatePreferences: upsert (crée la ligne au premier PATCH)
- Tests Mocha + Cucumber : 401, valeur invalide, défaut, création à la
  volée, cloisonnement entre profils
2026-08-17 15:55:45 +02:00

31 lines
1.2 KiB
TypeScript

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<PreferencesView> {
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<PreferencesView> {
const preferences = await prisma.userPreference.upsert({
where: { userProfileId },
create: { userProfileId, theme },
update: { theme },
});
return { theme: preferences.theme };
}