import { invalidateProjects } from "@/lib/projects"; import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, StructureRefused, manifestToken, writeDraftedField } from "@/lib/report/manifest.mjs"; import { alternativeById, draftsView } from "@/lib/report/drafts.mjs"; import { readFieldProvenance } from "@/lib/report/draft-provenance.mjs"; import { currentFieldValue, valueFromEdit } from "@/lib/report/draftEdit.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { draftsReviewRows, readDraftsReview, setDraftReview } from "umtool-report-to-video/drafts-review"; import { refuseCaller } from "@/lib/report/caller"; export const dynamic = "force-dynamic"; // The drafts review (release 28, M0): every drafted field of a cut, its // review, and the alternatives the draft tasks kept. // // GET ?project= the view (lib/report/drafts.mjs) and the token // POST { project, action, ... } one action: // approve { address, note? } approved on the field's CURRENT value // reject { address, note } rejected; the note says what is wrong (required) // clear { address } no verdict (the note is kept) // note { address, note } the note alone // use { alternative, token? } "Use this": the alternative (`#`) written // through the manifest writer as the operator's edit // (snapshot, validators, the edit-notes guard) // edit { address, text, note?, token? } the audition deck's EDIT (release 32 R1): the // field rewritten as text (lib/report/draftEdit.mjs), // written the same way as `use` -- the operator's // now -- and approved on its new words when the // field still has a review row // // Verdicts live in /drafts.review.json (report-to-video's // drafts-review.mjs); a final build refuses a drafted field not approved. // // Every answer carries `provenance` (lib/report/draft-provenance.mjs) beside // the rows: a field's draft file, its attempts and the rules they broke. A // Media tab that re-reads its rows after a draft then has the whole row at // once, instead of a row now and its provenance after the page refreshes. type Body = Record; const noStore = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const [view, provenance, token] = await Promise.all([ draftsView(r.project.dir, r.manifest), readFieldProvenance(r.project.dir, r.manifest), manifestToken(r.project.dir), ]); return Response.json({ ...view, provenance, token }, { headers: noStore }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/drafts"); if (refused) return refused; let body: Body; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const dir = r.project.dir; const note = typeof body.note === "string" ? body.note : undefined; let editNotes = null; try { if (body.action === "use") { const alt = await alternativeById(dir, String(body.alternative ?? "")); if (!alt) return Response.json({ error: `no alternative ${String(body.alternative ?? "")}` }, { status: 404 }); const token = typeof body.token === "string" ? body.token : null; const w = await withEditNotes(r.project, () => writeDraftedField(dir, { entry: alt.entry, field: alt.field, value: alt.value }, { token })); editNotes = w.editNotes; invalidateProjects(); } else { const address = String(body.address ?? ""); const row = draftsReviewRows(r.manifest, await readDraftsReview(dir)).find((x: { address: string }) => x.address === address); if (!row) return Response.json({ error: `${address || "(no address)"} is not a field this cut has to review` }, { status: 404 }); switch (body.action) { case "edit": { const value = valueFromEdit(row.field, currentFieldValue(r.manifest, row), String(body.text ?? "")); const token = typeof body.token === "string" ? body.token : null; const w = await withEditNotes(r.project, () => writeDraftedField(dir, { entry: row.entry, field: row.field, value }, { token })); editNotes = w.editNotes; invalidateProjects(); // A field the edit leaves under review (a narration) is approved on its new words. const after = draftsReviewRows((await readManifest(dir)) ?? r.manifest, await readDraftsReview(dir)).find((x: { address: string }) => x.address === address); if (after) await setDraftReview(dir, address, { verdict: "approved", key: after.key, text: after.text, ...(note !== undefined ? { note } : {}) }); break; } case "approve": await setDraftReview(dir, address, { verdict: "approved", key: row.key, text: row.text, ...(note !== undefined ? { note } : {}) }); break; case "reject": await setDraftReview(dir, address, { verdict: "rejected", key: row.key, text: row.text, note: note ?? "" }); break; case "clear": await setDraftReview(dir, address, { verdict: null }); break; case "note": await setDraftReview(dir, address, { note: note ?? "" }); break; default: return Response.json({ error: "action must be approve, reject, clear, note, edit or use" }, { status: 400 }); } } } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 }); if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } const fresh = await resolveReport(String(body.project)); const manifest = "error" in fresh ? r.manifest : fresh.manifest; const [view, provenance, token] = await Promise.all([draftsView(dir, manifest), readFieldProvenance(dir, manifest), manifestToken(dir)]); return Response.json({ ok: true, ...view, provenance, token, editNotes }, { headers: noStore }); }