import "server-only"; import { query, queryOne } from "@/lib/db"; import { loadTranslationsFor, replaceTranslations } from "./translations"; import type { ClosedDay } from "@/lib/types"; interface ClosedDayRow { closed_date: string; reason: string | null; } /** Geschlossene Tage in einem Zeitraum (für Wochenplan/öffentliche Speisekarte/RSS). */ export async function listClosedDays(fromISO: string, toISO: string): Promise { const rows = await query( "select closed_date, reason from closed_days where closed_date between $1 and $2", [fromISO, toISO] ); if (rows.length === 0) return []; const translations = await loadTranslationsFor( "closed_day_translations", rows.map((r) => r.closed_date) ); return rows.map((r) => ({ ...r, translations: translations.get(r.closed_date) ?? {}, })); } /** Tag als geschlossen markieren (oder bestehenden Grund aktualisieren). */ export async function setClosedDay( date: string, reason: string | null, translations?: Record ): Promise { const row = (await queryOne( `insert into closed_days (closed_date, reason) values ($1, $2) on conflict (closed_date) do update set reason = excluded.reason returning closed_date, reason`, [date, reason] ))!; if (translations) { await replaceTranslations("closed_day_translations", row.closed_date, translations); } const loaded = await loadTranslationsFor("closed_day_translations", [row.closed_date]); return { ...row, translations: loaded.get(row.closed_date) ?? {} }; } /** Tag wieder öffnen (löscht auch alle Übersetzungen des Grunds per Cascade). */ export async function removeClosedDay(date: string): Promise { await query("delete from closed_days where closed_date = $1", [date]); }