import { MuteError, setMute } from "@/lib/mutes"; import { parsePieceId, readPieceGraph } from "@/lib/pieces"; import { clearRailCounts } from "@/lib/rail"; export const dynamic = "force-dynamic"; // Mute or unmute one kind of call on one piece (lib/mutes.ts): the Inbox stops // (or starts again) showing it. Not a ruling: the rows stay in the decisions // API, `hidden` with `hiddenBy: "muted"`. // // POST { piece: "a:/" | "p:", kind: "", muted: true | false } // → { piece, kind, muted, at } // // 400 for a body that is not a piece id, a kind and a boolean; 404 for a piece // nothing on disk is (a research case `c:` has no inbox); 409 when mutes.json // does not parse (it is never overwritten). const NO_STORE = { "cache-control": "no-store" }; const fail = (status: number, error: string) => Response.json({ error }, { status, headers: NO_STORE }); export async function POST(request: Request) { const body = (await request.json().catch(() => null)) as { piece?: unknown; kind?: unknown; muted?: unknown } | null; if (!body || typeof body !== "object") return fail(400, "the body is { piece, kind, muted }"); const parsed = parsePieceId(body.piece); if (!parsed) return fail(400, `not a piece id: ${JSON.stringify(body.piece ?? null)} (a:/ or p:)`); if (typeof body.kind !== "string") return fail(400, `kind is a decision kind, not ${JSON.stringify(body.kind ?? null)}`); if (typeof body.muted !== "boolean") return fail(400, `muted is true or false, not ${JSON.stringify(body.muted ?? null)}`); if (parsed.kind === "case") return fail(404, "research cases have no inbox"); const graph = await readPieceGraph(); if (!graph.byId.has(body.piece as string)) return fail(404, `no piece ${body.piece}`); try { const e = await setMute(body.piece as string, body.kind, body.muted); clearRailCounts(); return Response.json(e, { headers: NO_STORE }); } catch (err) { if (err instanceof MuteError) return fail((err as MuteError & { status: number }).status, err.message); return fail(500, (err as Error).message); } }