import { NextResponse, type NextRequest } from "next/server"; import { callMobileApi } from "@/app/lib/server/mobileApi"; import { getToken } from "@/app/lib/server/session"; import { badRequest, errorResponse, unauthorized } from "@/app/lib/server/apiRoute"; // One proxy for the event guest, check-in and attendance endpoints; mobile-api owns validation and the BO_EVENTS / BO_EVENT_GUESTS guards. type Ctx = { params: Promise<{ uuid: string; path: string[] }> }; const ALLOWED = /^(guests(\/(send|\d+))?|checkin|attendance)$/; // Event uuids are strings, not numbers; word characters and hyphens cover them and nothing that can alter the upstream path. const EVENT_ID = /^[\w-]+$/; async function forward(req: NextRequest, { params }: Ctx, method?: "POST" | "DELETE") { const token = await getToken(); if (!token) return unauthorized(); const { uuid, path } = await params; if (!EVENT_ID.test(uuid) || !ALLOWED.test(path.join("/"))) return badRequest("Invalid path."); // Only the preview flag is forwarded; nothing else from the browser's query string. const preview = new URL(req.url).searchParams.getAll("preview").includes("true") ? "?preview=true" : ""; const target = `/api/admin/events/${encodeURIComponent(uuid)}/${path.map(encodeURIComponent).join("/")}${preview}`; const result = method === "POST" ? await callMobileApi(target, { method, body: await req.json().catch(() => ({})), token }) : method ? await callMobileApi(target, { method, token }) : await callMobileApi(target, { token }); if (!result.ok) return errorResponse(result); // A delete answers 204 with no body. return NextResponse.json(result.data ?? {}); } export const GET = (req: NextRequest, ctx: Ctx) => forward(req, ctx); export const POST = (req: NextRequest, ctx: Ctx) => forward(req, ctx, "POST"); export const DELETE = (req: NextRequest, ctx: Ctx) => forward(req, ctx, "DELETE");