import { NextResponse } from "next/server"; import { enqueueAsks, lastJobOf, refusalsOf } from "../_publish"; import { ops, optPreviewBranch, opsFail, reqSiteIds } from "../_lib"; export const dynamic = "force-dynamic"; // AN ALIAS (release 18): each site's BUILT bundle, deployed — never a build. The body and the answer are the // route's old ones; the work is publish stages on the publish queue // (../_publish.ts), as `POST /api/ops/publish` {"verb": "deploy"} runs them. // // A site never built is refused before any job: "no build of in — // archilyzer publish build ". → { ok, jobs: [{siteId, jobId, // previewUrl?}], skipped } (+ `jobId` when one site); none started → 400. export async function POST(request: Request) { return ops(request, ["siteId", "siteIds", "preview"], async (body) => { const preview = optPreviewBranch(body); const ids = reqSiteIds(body); const where = preview ? ("preview" as const) : ("production" as const); const run = await enqueueAsks( ids.map((target) => ({ target, deploy: { where, ...(preview ? { preview } : {}), force: true } })), ); const jobs = ids.flatMap((siteId) => { const j = lastJobOf(run, siteId); return j ? [{ siteId, jobId: j.jobId, ...(j.previewUrl ? { previewUrl: j.previewUrl } : {}) }] : []; }); const skipped = refusalsOf(run).map((r) => ({ siteId: r.target, reason: r.error })); if (jobs.length === 0) { return opsFail(skipped.length === 1 ? skipped[0].reason : skipped.map((s) => `${s.siteId}: ${s.reason}`).join("; ")); } return NextResponse.json({ ok: true, runId: run.runId, jobs, skipped, ...(jobs.length === 1 ? { jobId: jobs[0].jobId, ...(jobs[0].previewUrl ? { previewUrl: jobs[0].previewUrl } : {}) } : {}), }); }); } export function GET() { return NextResponse.json({ ok: false, error: "POST only" }, { status: 405 }); }