- Next.js 16 App Router site with the ANOUMA design system - Payload CMS (PostgreSQL) for offers, events, posts and page content - WebRTC video-call system with custom signaling server - SMTP email reminders and booking-request notifications - Customer accounts, calendar-based availability, and booking workflow
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getPayload } from "payload";
|
|
import config from "@payload-config";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type Args = { params: Promise<{ slug: string }> };
|
|
|
|
export async function POST(request: Request, { params }: Args) {
|
|
const { slug } = await params;
|
|
|
|
let body: { name?: unknown; email?: unknown };
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Ungültige Anfrage." }, { status: 400 });
|
|
}
|
|
|
|
const name = typeof body.name === "string" ? body.name.trim() : "";
|
|
const email = typeof body.email === "string" ? body.email.trim() : "";
|
|
if (!name || !email || !email.includes("@")) {
|
|
return NextResponse.json({ error: "Bitte gib deinen Namen und eine gültige E-Mail-Adresse an." }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const payload = await getPayload({ config });
|
|
|
|
const { docs } = await payload.find({
|
|
collection: "events",
|
|
where: { slug: { equals: slug } },
|
|
limit: 1,
|
|
});
|
|
const event = docs[0];
|
|
if (!event) {
|
|
return NextResponse.json({ error: "Dieser Termin wurde nicht gefunden." }, { status: 404 });
|
|
}
|
|
|
|
const { docs: existingForEmail } = await payload.find({
|
|
collection: "event-registrations",
|
|
where: {
|
|
and: [
|
|
{ event: { equals: event.id } },
|
|
{ email: { equals: email } },
|
|
{ status: { equals: "registered" } },
|
|
],
|
|
},
|
|
limit: 1,
|
|
});
|
|
if (existingForEmail[0]) {
|
|
return NextResponse.json({ ok: true, alreadyRegistered: true });
|
|
}
|
|
|
|
if (event.maxParticipants) {
|
|
const { totalDocs } = await payload.count({
|
|
collection: "event-registrations",
|
|
where: { and: [{ event: { equals: event.id } }, { status: { equals: "registered" } }] },
|
|
});
|
|
if (totalDocs >= event.maxParticipants) {
|
|
return NextResponse.json({ error: "Dieser Termin ist bereits ausgebucht." }, { status: 409 });
|
|
}
|
|
}
|
|
|
|
await payload.create({
|
|
collection: "event-registrations",
|
|
data: { event: event.id, name, email, status: "registered" },
|
|
});
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error) {
|
|
console.error("event registration failed", error);
|
|
return NextResponse.json({ error: "Die Anmeldung ist gerade nicht möglich. Bitte versuche es später erneut." }, { status: 500 });
|
|
}
|
|
}
|