- 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
52 lines
2.0 KiB
TypeScript
52 lines
2.0 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<{ id: string }> };
|
|
|
|
export async function POST(request: Request, { params }: Args) {
|
|
const { id } = await params;
|
|
|
|
try {
|
|
const payload = await getPayload({ config });
|
|
const { user } = await payload.auth({ headers: request.headers });
|
|
if (!user || user.collection !== "customers") {
|
|
return NextResponse.json({ error: "Bitte melde dich an." }, { status: 401 });
|
|
}
|
|
|
|
const booking = await payload.findByID({ collection: "booking-requests", id, depth: 0 }).catch(() => null);
|
|
if (!booking || Number(booking.user) !== Number(user.id)) {
|
|
return NextResponse.json({ error: "Buchung nicht gefunden." }, { status: 404 });
|
|
}
|
|
const alternative = booking.proposedAlternative;
|
|
if (booking.status !== "pending" || !alternative?.date || !alternative.startTime || !alternative.endTime) {
|
|
return NextResponse.json({ error: "Für diese Buchung liegt kein alternativer Termin vor." }, { status: 409 });
|
|
}
|
|
|
|
// The beforeChange hook on booking-requests re-checks for conflicts
|
|
// (with an advisory lock) before allowing this to become "confirmed" —
|
|
// the alternative slot may have been taken by someone else since it was
|
|
// proposed.
|
|
await payload.update({
|
|
collection: "booking-requests",
|
|
id,
|
|
data: {
|
|
status: "confirmed",
|
|
date: alternative.date,
|
|
startTime: alternative.startTime,
|
|
endTime: alternative.endTime,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error) {
|
|
if (error instanceof Error && "status" in error && (error as { status: number }).status === 409) {
|
|
return NextResponse.json({ error: error.message }, { status: 409 });
|
|
}
|
|
console.error("accept-alternative failed", error);
|
|
return NextResponse.json({ error: "Der Termin konnte nicht angenommen werden." }, { status: 500 });
|
|
}
|
|
}
|