// POST /api/projects/[projectId]/apply — body is { ops: ModelOp[], expectedVersion?: number, reason?: string }. // Returns { applied, model, version, idMapping, errors } from the server-side applyOps. import { NextResponse } from "next/server"; import { applyOpsToProject } from "../../../../../lib/db/repo"; import type { ModelOp } from "../../../../../lib/sync/ops"; export async function POST(req: Request, { params }: { params: Promise<{ projectId: string }> }) { const { projectId } = await params; let body: { ops?: ModelOp[]; expectedVersion?: number; reason?: string }; try { body = await req.json(); } catch { return NextResponse.json({ error: "invalid json body" }, { status: 400 }); } if (!body.ops || !Array.isArray(body.ops) || body.ops.length === 0) { return NextResponse.json({ error: "body.ops must be a non-empty array" }, { status: 400 }); } try { const result = await applyOpsToProject(projectId, body.ops, body.expectedVersion, body.reason); return NextResponse.json(result); } catch (err) { return NextResponse.json({ error: (err as Error).message }, { status: 500 }); } }