import { invalidateProjects, projectRef } from "@/lib/projects"; import { pieceHrefOfProject } from "@/lib/pieces/article"; import { TakeStale, pickTake } from "@/lib/report/takeMaker.mjs"; import { isTakeId } from "@/lib/report/takes.mjs"; export const dynamic = "force-dynamic"; // "USE THIS TAKE" (release 33). POST { project, take, anyway?, pin? } // // `pin` (1 Nov 2026): true pins the pick (a newer whole-cut take is not // auto-picked over it), false unpins; absent keeps a re-picked take's pin. // // The take's video.manifest.json over the project's, through the manifest // writer (lib/report/manifest.mjs replaceManifest: the auto snapshot // `auto-before-pick-take` first, so the Cut tab's "Undo last edit" and the // History tab's revisions take it back) inside the edit-notes guard; then the // pick in takes/verdicts.json, one per group. // // 409 `{ stale: true }` when the project's manifest is not the one the take // was made from -- the pick would throw away what changed since -- until the // operator says `anyway` (a second, explicit click). export async function POST(request: Request) { const body = (await request.json().catch(() => null)) as { project?: unknown; take?: unknown; anyway?: unknown; pin?: unknown } | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400 }); if (!isTakeId(body.take)) return Response.json({ error: "take must be a take id" }, { status: 400 }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); try { const r = await pickTake(project, body.take, { anyway: body.anyway === true, pinned: body.pin === true ? true : body.pin === false ? false : undefined }); invalidateProjects(); const historyHref = await pieceHrefOfProject(project.id, { tab: "history" }); return Response.json({ ok: true, ...r, historyHref }, { headers: { "cache-control": "no-store" } }); } catch (e) { if (e instanceof TakeStale) return Response.json({ error: e.message, stale: true }, { status: 409 }); const message = e instanceof Error ? e.message : String(e); return Response.json({ error: message }, { status: /^no take /.test(message) ? 404 : 400 }); } }