import { NextResponse } from "next/server"; import { requireBotAuth } from "@/lib/bot-auth"; import { createOrGetArea, listAreas, validateCreateAreaInput } from "@/lib/areas"; // Bot-facing Area list/create — see src/lib/areas.ts's createOrGetArea() // comment for why POST is idempotent-by-name rather than erroring on a // name the bot's own keyboard just offered as an existing option. export async function GET(request: Request): Promise { if (!requireBotAuth(request)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const areas = await listAreas(); return NextResponse.json({ areas }); } export async function POST(request: Request): Promise { if (!requireBotAuth(request)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } let body: unknown; try { body = await request.json(); } catch { return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 }); } if (typeof body !== "object" || body === null) { return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 }); } const { name, team } = body as Record; const validation = validateCreateAreaInput({ name, team }); if (!validation.ok) { return NextResponse.json({ error: validation.error }, { status: 400 }); } const created = await createOrGetArea(validation.value); if (!created.ok) { return NextResponse.json({ error: created.error }, { status: 400 }); } return NextResponse.json(created.value, { status: 201 }); }