import { prisma } from "../database"; import { normalizeBackgroundUrl } from "@/lib/avatars"; import type { SitePreferences } from "@/prisma/generated/prisma/client"; // Type étendu pour les préférences avec les nouveaux champs de points des maisons type SitePreferencesWithHousePoints = SitePreferences & { houseJoinPoints?: number; houseLeavePoints?: number; houseCreatePoints?: number; }; export interface UpdateSitePreferencesInput { homeBackground?: string | null; eventsBackground?: string | null; leaderboardBackground?: string | null; challengesBackground?: string | null; eventRegistrationPoints?: number; eventFeedbackPoints?: number; houseJoinPoints?: number; houseLeavePoints?: number; houseCreatePoints?: number; } /** * Service de gestion des préférences globales du site */ export class SitePreferencesService { /** * Récupère les préférences du site */ async getSitePreferences(): Promise { return prisma.sitePreferences.findUnique({ where: { id: "global" }, }); } /** * Récupère les préférences du site ou crée une entrée par défaut */ async getOrCreateSitePreferences(): Promise { let sitePreferences = await prisma.sitePreferences.findUnique({ where: { id: "global" }, }); if (!sitePreferences) { sitePreferences = await prisma.sitePreferences.create({ data: { id: "global", homeBackground: null, eventsBackground: null, leaderboardBackground: null, challengesBackground: null, eventRegistrationPoints: 100, eventFeedbackPoints: 100, houseJoinPoints: 100, houseLeavePoints: 100, houseCreatePoints: 100, }, }); } // S'assurer que les valeurs par défaut sont présentes même si les colonnes n'existent pas encore const prefs = sitePreferences as SitePreferencesWithHousePoints; if (prefs.houseJoinPoints == null) prefs.houseJoinPoints = 100; if (prefs.houseLeavePoints == null) prefs.houseLeavePoints = 100; if (prefs.houseCreatePoints == null) prefs.houseCreatePoints = 100; return sitePreferences; } /** * Met à jour les préférences du site */ async updateSitePreferences( data: UpdateSitePreferencesInput ): Promise { return prisma.sitePreferences.upsert({ where: { id: "global" }, update: { homeBackground: data.homeBackground === "" ? null : (data.homeBackground ?? undefined), eventsBackground: data.eventsBackground === "" ? null : (data.eventsBackground ?? undefined), leaderboardBackground: data.leaderboardBackground === "" ? null : (data.leaderboardBackground ?? undefined), challengesBackground: data.challengesBackground === "" ? null : (data.challengesBackground ?? undefined), eventRegistrationPoints: data.eventRegistrationPoints !== undefined ? data.eventRegistrationPoints : undefined, eventFeedbackPoints: data.eventFeedbackPoints !== undefined ? data.eventFeedbackPoints : undefined, houseJoinPoints: data.houseJoinPoints !== undefined ? data.houseJoinPoints : undefined, houseLeavePoints: data.houseLeavePoints !== undefined ? data.houseLeavePoints : undefined, houseCreatePoints: data.houseCreatePoints !== undefined ? data.houseCreatePoints : undefined, }, create: { id: "global", homeBackground: data.homeBackground === "" ? null : (data.homeBackground ?? null), eventsBackground: data.eventsBackground === "" ? null : (data.eventsBackground ?? null), leaderboardBackground: data.leaderboardBackground === "" ? null : (data.leaderboardBackground ?? null), challengesBackground: data.challengesBackground === "" ? null : (data.challengesBackground ?? null), eventRegistrationPoints: data.eventRegistrationPoints ?? 100, eventFeedbackPoints: data.eventFeedbackPoints ?? 100, houseJoinPoints: data.houseJoinPoints ?? 100, houseLeavePoints: data.houseLeavePoints ?? 100, houseCreatePoints: data.houseCreatePoints ?? 100, }, }); } /** * Récupère l'image de fond pour une page donnée */ async getBackgroundImage( page: "home" | "events" | "leaderboard" | "challenges", defaultImage: string ): Promise { try { const sitePreferences = await prisma.sitePreferences.findUnique({ where: { id: "global" }, }); if (!sitePreferences) { return defaultImage; } const imageKey = `${page}Background` as keyof typeof sitePreferences; const customImage = sitePreferences[imageKey]; const imageUrl = (customImage as string | null) || defaultImage; // Normaliser l'URL pour utiliser l'API si nécessaire return normalizeBackgroundUrl(imageUrl) || defaultImage; } catch (error) { console.error("Error fetching background image:", error); return defaultImage; } } } export const sitePreferencesService = new SitePreferencesService();