import { NextRequest } from "next/server"; import { z } from "zod"; import { clientIp, gateOrUser } from "@/auth/api"; import { rateLimit } from "@/lib/rate-limit"; import { IntakeError, createCoverageIntake, declineCoverageIntake, scheduleCoverageIntake, } from "@/db/intake"; const JSON_HEADERS = { "content-type": "application/json" }; const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: JSON_HEADERS }); const intakeSchema = z.object({ requesterName: z.string().min(2).max(120), requesterEmail: z.string().email().max(200), requesterPhone: z.string().max(40).nullable().optional(), ministry: z.string().max(120).nullable().optional(), title: z.string().min(3).max(200), eventType: z.string().max(60).optional(), date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), startTime: z.string().regex(/^\d{2}:\d{2}$/), endTime: z.string().regex(/^\d{2}:\d{2}$/).nullable().optional(), location: z.string().max(200).nullable().optional(), description: z.string().max(2000).nullable().optional(), }); const reviewSchema = z.object({ id: z.string().min(1), action: z.enum(["schedule", "decline"]), }); async function body(req: NextRequest): Promise { try { return await req.json(); } catch { return null; } } /** * Public submission endpoint. Rate-limited per IP; the response never reveals * whether an identical request already exists. */ export const POST = async (req: NextRequest) => { const limit = rateLimit(`coverage-intake:${clientIp(req)}`, 5, 60 * 60 * 1000); if (!limit.ok) { return new Response( JSON.stringify({ error: "Too many requests. Please try again later." }), { status: 429, headers: { ...JSON_HEADERS, "retry-after": String(limit.retryAfterSeconds), }, }, ); } const parsed = intakeSchema.safeParse(await body(req)); if (!parsed.success) { return json({ error: parsed.error.issues[0]?.message ?? "Invalid request" }, 400); } try { await createCoverageIntake(parsed.data); return json({ ok: true }, 201); } catch (err) { if (err instanceof IntakeError) return json({ error: err.message }, err.status); console.error("[coverage-intake] submit failed", err); return json({ error: "Could not submit the request." }, 500); } }; /** Coordinator review: turn an accepted request into an event, or decline it. */ export const PATCH = async (req: NextRequest) => { const gate = await gateOrUser("schedule.publish"); if (gate instanceof Response) return gate; const parsed = reviewSchema.safeParse(await body(req)); if (!parsed.success) return json({ error: parsed.error.message }, 400); try { if (parsed.data.action === "schedule") { const row = await scheduleCoverageIntake({ id: parsed.data.id, actorId: gate.id, }); return json({ ok: true, ...row }); } await declineCoverageIntake({ id: parsed.data.id, actorId: gate.id }); return json({ ok: true }); } catch (err) { if (err instanceof IntakeError) return json({ error: err.message }, err.status); console.error("[coverage-intake] review failed", err); return json({ error: "Could not review the request." }, 500); } };