// Narrative document save endpoint. The TipTap editor PUTs the current // ProseMirror JSON here on debounce. Source of truth after the pivot. import { NextResponse } from "next/server"; import { loadDocument, saveDocument } from "../../../../../lib/db/repo"; interface RouteContext { params: Promise<{ projectId: string }>; } export async function GET(_req: Request, ctx: RouteContext) { const { projectId } = await ctx.params; const doc = await loadDocument(projectId); if (!doc) return NextResponse.json({ doc: null, version: 0 }); return NextResponse.json({ doc: doc.doc, version: doc.version, updatedAt: doc.updatedAt }); } export async function PUT(req: Request, ctx: RouteContext) { const { projectId } = await ctx.params; let body: unknown; try { body = await req.json(); } catch { return NextResponse.json({ error: "Invalid JSON" }, { status: 400 }); } if (!body || typeof body !== "object" || !("doc" in body)) { return NextResponse.json({ error: "Missing `doc` field" }, { status: 400 }); } const stored = await saveDocument(projectId, (body as { doc: unknown }).doc); return NextResponse.json({ version: stored.version, updatedAt: stored.updatedAt }); }