import { NextResponse } from "next/server"; import { createSurvey, getAssessment, getCatalog, listSurveys } from "@/lib/store"; import { surveyableDomains } from "@/lib/survey"; type Ctx = { params: Promise<{ id: string }> }; export async function GET(_req: Request, { params }: Ctx) { const { id } = await params; if (!(await getAssessment(id))) return NextResponse.json({ error: "Not found" }, { status: 404 }); return NextResponse.json(await listSurveys(id)); } export async function POST(req: Request, { params }: Ctx) { const { id } = await params; if (!(await getAssessment(id))) return NextResponse.json({ error: "Not found" }, { status: 404 }); const body = (await req.json()) as { respondentName?: string; domains?: string[] }; // Keep catalog order and drop anything that is not a live category. const wanted = new Set(Array.isArray(body.domains) ? body.domains : []); const domains = surveyableDomains(await getCatalog()).map((d) => d.id).filter((d) => wanted.has(d)); if (!domains.length) return NextResponse.json({ error: "Pick at least one category." }, { status: 400 }); const name = typeof body.respondentName === "string" ? body.respondentName.trim().slice(0, 120) : ""; return NextResponse.json(await createSurvey(id, name, domains)); }