Files
Wabenhain/src/app/admin/actions/harvests-batches.ts
T
Nick Szaunig c7927f2883 feat: Wabenhain Honig-Shop Anwendung
Vollstaendige Next.js/Prisma-E-Commerce-Anwendung fuer eine Imkerei:
Shop, Warenkorb, Stripe-Checkout, Kundenkonto mit 2FA, Admin-Bereich
(Produkte, Ernten, Chargen, Bestellungen, Gutscheine, admin-editierbare
Website-Inhalte), i18n (DE/EN), Rate-Limiting/CSP/Audit-Log-Haertung,
PostgreSQL- und MySQL-Unterstuetzung ueber austauschbare Prisma-
Treiber-Adapter, Unit-/Komponenten-/E2E-Tests.
2026-08-17 14:30:28 +02:00

97 lines
2.9 KiB
TypeScript

"use server";
import { redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
import { prisma } from "@/lib/prisma";
import { logAdminAction } from "@/lib/audit-log";
import { requireAdmin, str, num } from "./_shared";
export async function createHarvestAction(formData: FormData): Promise<void> {
const admin = await requireAdmin();
const year = num(formData, "year");
const type = str(formData, "type");
const slug = `${type.toLowerCase().replace(/[^a-z0-9]+/g, "-")}-${year}`;
const harvest = await prisma.harvest.create({
data: {
year,
type,
slug,
harvestDate: new Date(str(formData, "harvestDate")),
location: str(formData, "location") || "Altenmünster",
description: str(formData, "description"),
},
});
await logAdminAction(admin, {
action: "harvest.create",
entityType: "Harvest",
entityId: harvest.id,
detail: `${type} ${year}`,
});
revalidatePath("/admin/ernten");
redirect("/admin/ernten");
}
export async function updateHarvestAction(formData: FormData): Promise<void> {
const admin = await requireAdmin();
const id = str(formData, "id");
await prisma.harvest.update({
where: { id },
data: {
year: num(formData, "year"),
type: str(formData, "type"),
harvestDate: new Date(str(formData, "harvestDate")),
location: str(formData, "location") || "Altenmünster",
description: str(formData, "description"),
},
});
await logAdminAction(admin, { action: "harvest.update", entityType: "Harvest", entityId: id });
revalidatePath("/admin/ernten");
redirect("/admin/ernten");
}
export async function createBatchAction(formData: FormData): Promise<void> {
const admin = await requireAdmin();
const code = str(formData, "code");
const batch = await prisma.batch.create({
data: {
code,
harvestId: str(formData, "harvestId"),
fillDate: new Date(str(formData, "fillDate")),
bestBeforeDate: new Date(str(formData, "bestBeforeDate")),
glassCount: num(formData, "glassCount"),
remainingGlasses: num(formData, "remainingGlasses"),
},
});
await logAdminAction(admin, { action: "batch.create", entityType: "Batch", entityId: batch.id, detail: code });
revalidatePath("/admin/chargen");
redirect("/admin/chargen");
}
export async function updateBatchAction(formData: FormData): Promise<void> {
const admin = await requireAdmin();
const id = str(formData, "id");
await prisma.batch.update({
where: { id },
data: {
fillDate: new Date(str(formData, "fillDate")),
bestBeforeDate: new Date(str(formData, "bestBeforeDate")),
glassCount: num(formData, "glassCount"),
remainingGlasses: num(formData, "remainingGlasses"),
},
});
await logAdminAction(admin, { action: "batch.update", entityType: "Batch", entityId: id });
revalidatePath("/admin/chargen");
redirect("/admin/chargen");
}