import { DeleteRefused, deleteProject, planProjectDelete } from "@/lib/report/delete.mjs"; import { projectDeleteContext } from "@/lib/report/delete-context"; import { answerDelete, type DeletePlanView } from "@/lib/delete-plan"; import { invalidateProjects, projectRef, pruneIndex } from "@/lib/projects"; import { clearLinksCache } from "@/lib/articles/links.mjs"; export const dynamic = "force-dynamic"; // DELETE A PROJECT (release 27 follow-up): the "new project" door's other // half. POST { project, dryRun?, force? } — lib/report/delete.mjs: its // directory under the reports root and, for a link umtool's move-out made, its // media copy; refused while a job works in it, when it is an article's current // video (or a running iteration's project), or reached through a link; a // project whose manifest names an article with a video attached only with // `force`. The answer is lib/delete-plan.ts's. export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as { project?: unknown; dryRun?: unknown; force?: unknown }; const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const force = body.force === true; const opts = { ...(await projectDeleteContext(project.id)), force }; try { const plan = (await planProjectDelete(project, opts)) as DeletePlanView; return await answerDelete(plan, { dryRun: body.dryRun === true, force }, async () => { const r = await deleteProject(project, opts); invalidateProjects(); clearLinksCache(); await pruneIndex().catch(() => 0); return r as { plan: DeletePlanView; removed: string[]; failed: { path: string; error: string }[] }; }); } catch (err) { if (err instanceof DeleteRefused) return Response.json({ error: err.message, plan: err.plan }, { status: 409 }); return Response.json({ error: err instanceof Error ? err.message : String(err) }, { status: 500 }); } }