import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { isValidSiteId } from "yt-dlp-transcript-common/lib/siteSchema"; import { SiteDeleteRefusedError, deleteSiteByPlan, planSiteDelete, siteDeleteBlocked, } from "yt-dlp-transcript-common/publish/siteDelete"; import { OpsInputError, ops, opsFail, optBool, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { id: string, force?: boolean, dryRun?: boolean } // -> { ok, applied, plan, removed?, failed? } // // DELETE A SITE (release 28 H5), plan first: `sites//` (site.json, its // pool and pages, tags, charts, reports), its publish state and last bundle // (`export/.export-builds//`) and compose's staging for it // (`export/.export-index/sites//`) — through publish/siteDelete.ts, // under the site.json lock and the publish lock. Channel data is never // touched; the Pages project, what was deployed and a deployed hub are kept // (remote: a delete does not undeploy). `dryRun` answers the plan and writes // nothing. Refused (409, with the plan): a site with a deploy target // (`cloudflareProject`), a deploy in its publish state, or reports of its own // — unless `force`; no such site, a site dir that is a link, or a publish // stage running — force or not. // // There is no undo, and transcripts/ is its own git repo — the only way back // for sites// is a commit there. export async function POST(request: Request) { return ops(request, ["id", "force", "dryRun"], async (body) => { const id = reqString(body, "id"); if (!isValidSiteId(id)) throw new OpsInputError(`"${id}" is not a site id`); const force = optBool(body, "force") === true; const paths = getPaths(); try { const plan = await planSiteDelete(id, paths); if (siteDeleteBlocked(plan, force)) { const why = plan.refusals.filter((r) => !force || !r.forceable).map((r) => r.reason); return opsFail(`refused: ${why.join("; ")}`, plan.exists ? 409 : 404, { plan }); } if (optBool(body, "dryRun") === true) return NextResponse.json({ ok: true, applied: false, plan }); const r = await deleteSiteByPlan(id, paths, { force }); return NextResponse.json({ ok: r.failed.length === 0, applied: true, plan: r.plan, removed: r.removed, failed: r.failed }); } catch (err) { if (err instanceof SiteDeleteRefusedError) return opsFail(err.message, 409, { plan: err.plan }); throw err; } }); }