Im Wochenplan lässt sich pro Tag ein optionaler, mehrsprachiger Schließungsgrund hinterlegen. Geschlossene Tage werden auf der öffentlichen Speisekarte (Desktop + mobil) und im RSS-Feed als "Geschlossen" statt mit Essenszeiten angezeigt, im Wochenplan bleibt "Mahlzeit hinzufügen" für diesen Tag deaktiviert.
51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
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<ClosedDay[]> {
|
|
const rows = await query<ClosedDayRow>(
|
|
"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<string, string | null>
|
|
): Promise<ClosedDay> {
|
|
const row = (await queryOne<ClosedDayRow>(
|
|
`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<void> {
|
|
await query("delete from closed_days where closed_date = $1", [date]);
|
|
}
|