import { NextResponse } from "next/server"; import { getCatalog, saveCatalog } from "@/lib/store"; import type { Catalog } from "@/lib/types"; export async function GET() { return NextResponse.json(await getCatalog()); } export async function PUT(req: Request) { const body = (await req.json()) as Catalog; if (!Array.isArray(body?.products) || !Array.isArray(body?.businessUnits)) { return NextResponse.json({ error: "Malformed catalog" }, { status: 400 }); } const ids = new Set(); for (const p of body.products) { if (!p.id || !p.name) return NextResponse.json({ error: "Product needs an id and a name" }, { status: 400 }); if (ids.has(p.id)) return NextResponse.json({ error: `Duplicate product id: ${p.id}` }, { status: 400 }); ids.add(p.id); } return NextResponse.json(await saveCatalog(body)); }