import { caseView, pinQuote, type PinBody } from "@/lib/research/boards"; import { CaseError } from "@/lib/research/cases.mjs"; import { fail, ok } from "../../../_respond"; export const dynamic = "force-dynamic"; // PIN a cite to the case as a snapshot quote (the API is documented in // ../../route.ts): { source, channel, video, t, until?, track?, kind?, claim?, // note?, section?, search?, scope?, at? } -> { quote, duplicate, case: CaseView } export async function POST(request: Request, ctx: { params: Promise<{ id: string }> }) { try { const { id } = await ctx.params; const b = (await request.json().catch(() => null)) as Record | null; if (!b || typeof b !== "object" || Array.isArray(b)) throw new CaseError("expected a JSON object: { source, channel, video, t, … }"); const s = (k: string) => (typeof b[k] === "string" && (b[k] as string).trim() ? (b[k] as string).trim() : undefined); const source = s("source"); const channel = s("channel"); const video = s("video"); if (!source || !channel || !video) throw new CaseError("a pin names its source, channel and video"); const kind = b.kind === "post" ? "post" : "video"; const t = Number(b.t ?? 0); if (!Number.isFinite(t) || t < 0) throw new CaseError("t is a second, 0 or more"); const until = b.until === undefined || b.until === null ? undefined : Number(b.until); if (until !== undefined && (!Number.isFinite(until) || until < t)) throw new CaseError("until is a second at or after t"); const obj = (k: string) => (b[k] && typeof b[k] === "object" && !Array.isArray(b[k]) ? (b[k] as Record) : undefined); const body: PinBody = { source, channel, video, t, kind, ...(until !== undefined ? { until } : {}), ...(s("track") ? { track: s("track") } : {}), ...(typeof b.claim === "string" ? { claim: b.claim } : {}), ...(typeof b.note === "string" ? { note: b.note } : {}), ...(typeof b.section === "string" || b.section === null ? { section: b.section as string | null } : {}), ...(obj("search") ? { search: obj("search") as PinBody["search"] } : {}), ...(obj("scope") ? { scope: obj("scope") as PinBody["scope"] } : {}), ...(Number.isInteger(b.at) ? { at: b.at as number } : {}), }; const r = await pinQuote(id, body); return ok({ quote: r.quote, duplicate: r.duplicate, case: await caseView(id) }); } catch (e) { return fail(e); } }