feat: Speiseplan-Inhalte variabel mehrsprachig (Teil 2)
Gerichtnamen, Essenszeiten und Allergen-/Zusatzstoff-Bezeichnungen sind jetzt genauso dynamisch mehrsprachig wie die Bedienoberfläche aus v1.10.0, statt fest auf Englisch/Polnisch begrenzt zu sein. Neue Übersetzungstabellen ersetzen die alten name_en/name_pl-Spalten, DeepL-Auto-Übersetzung und CSV-Import/-Export unterstützen jetzt beliebig viele aktive Sprachen.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import { replaceTranslations } from "@/lib/db/translations";
|
||||
import { listActiveLanguageCodes } from "@/lib/db/languages";
|
||||
import { authErrorResponse, requireRole } from "@/lib/auth/session";
|
||||
import { isSameOrigin } from "@/lib/origin";
|
||||
import { parseCsvWithHeader, splitCodes } from "@/lib/csv";
|
||||
@@ -7,8 +9,7 @@ import { parseCsvWithHeader, splitCodes } from "@/lib/csv";
|
||||
interface ImportRow {
|
||||
rowNum: number;
|
||||
name: string;
|
||||
name_en: string | null;
|
||||
name_pl: string | null;
|
||||
translations: Record<string, string>;
|
||||
price: number;
|
||||
allergens: string[];
|
||||
additives: string[];
|
||||
@@ -32,8 +33,10 @@ export async function POST(request: Request) {
|
||||
if (nameIdx === -1 || priceIdx === -1) {
|
||||
return NextResponse.json({ error: "missing_columns" }, { status: 400 });
|
||||
}
|
||||
const nameEnIdx = header.indexOf("name_en");
|
||||
const namePlIdx = header.indexOf("name_pl");
|
||||
const activeCodes = (await listActiveLanguageCodes()).filter((c) => c !== "de");
|
||||
const langCols = activeCodes
|
||||
.map((code) => ({ code, idx: header.indexOf(`name_${code}`) }))
|
||||
.filter((c) => c.idx !== -1);
|
||||
const allergensIdx = header.indexOf("allergens");
|
||||
const additivesIdx = header.indexOf("additives");
|
||||
|
||||
@@ -52,11 +55,15 @@ export async function POST(request: Request) {
|
||||
skipped.push({ row: rowNum, reason: `Ungültiger Preis „${c[priceIdx] ?? ""}“` });
|
||||
return;
|
||||
}
|
||||
const translations: Record<string, string> = {};
|
||||
for (const { code, idx } of langCols) {
|
||||
const value = c[idx]?.trim();
|
||||
if (value) translations[code] = value;
|
||||
}
|
||||
rows.push({
|
||||
rowNum,
|
||||
name,
|
||||
name_en: nameEnIdx >= 0 ? c[nameEnIdx]?.trim() || null : null,
|
||||
name_pl: namePlIdx >= 0 ? c[namePlIdx]?.trim() || null : null,
|
||||
translations,
|
||||
price,
|
||||
allergens: allergensIdx >= 0 ? splitCodes(c[allergensIdx]) : [],
|
||||
additives: additivesIdx >= 0 ? splitCodes(c[additivesIdx]) : [],
|
||||
@@ -65,6 +72,7 @@ export async function POST(request: Request) {
|
||||
|
||||
let created = 0;
|
||||
let updated = 0;
|
||||
const savedTranslations: { id: string; translations: Record<string, string> }[] = [];
|
||||
if (rows.length > 0) {
|
||||
await withTransaction(async (client) => {
|
||||
for (const row of rows) {
|
||||
@@ -84,21 +92,27 @@ export async function POST(request: Request) {
|
||||
}
|
||||
if (existing.rows.length === 1) {
|
||||
await client.query(
|
||||
`update dishes set name_en = $1, name_pl = $2, price = $3, allergens = $4, additives = $5
|
||||
where id = $6`,
|
||||
[row.name_en, row.name_pl, row.price, row.allergens, row.additives, existing.rows[0].id]
|
||||
`update dishes set price = $1, allergens = $2, additives = $3 where id = $4`,
|
||||
[row.price, row.allergens, row.additives, existing.rows[0].id]
|
||||
);
|
||||
savedTranslations.push({ id: existing.rows[0].id, translations: row.translations });
|
||||
updated++;
|
||||
} else {
|
||||
await client.query(
|
||||
`insert into dishes (name, name_en, name_pl, price, allergens, additives)
|
||||
values ($1, $2, $3, $4, $5, $6)`,
|
||||
[row.name, row.name_en, row.name_pl, row.price, row.allergens, row.additives]
|
||||
const inserted = await client.query<{ id: string }>(
|
||||
`insert into dishes (name, price, allergens, additives)
|
||||
values ($1, $2, $3, $4) returning id`,
|
||||
[row.name, row.price, row.allergens, row.additives]
|
||||
);
|
||||
savedTranslations.push({ id: inserted.rows[0].id, translations: row.translations });
|
||||
created++;
|
||||
}
|
||||
}
|
||||
});
|
||||
for (const { id, translations } of savedTranslations) {
|
||||
if (Object.keys(translations).length > 0) {
|
||||
await replaceTranslations("dish_translations", id, translations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ created, updated, skipped });
|
||||
|
||||
Reference in New Issue
Block a user