// The disk half of lib/copies/file.ts: `/copies/.json` // and `.decisions.json`, read and written. SERVER-ONLY (node:fs). import path from "node:path"; import { readFile } from "node:fs/promises"; import { withJsonFileLock, writeJsonAtomic } from "../jsonFile-server"; import { SOURCE_GROUP_ID_RE } from "./settings"; import { COPIES_DECISIONS_FORMAT, COPIES_DIRNAME, COPIES_VERSION, isCopiesDecisionsFile, isCopiesFile, type CopiesDecisionsFile, type CopiesFile, type CopyDecision, type CopyDecisionBy, } from "./file"; export function copiesDirOf(transcriptsDir: string): string { return path.join(transcriptsDir, COPIES_DIRNAME); } function checkedId(group: string): string { if (!SOURCE_GROUP_ID_RE.test(group)) throw new Error(`not a source group id: ${JSON.stringify(group)}`); return group; } export function copiesPath(transcriptsDir: string, group: string): string { return path.join(copiesDirOf(transcriptsDir), `${checkedId(group)}.json`); } export function copiesDecisionsPath(transcriptsDir: string, group: string): string { return path.join(copiesDirOf(transcriptsDir), `${checkedId(group)}.decisions.json`); } // The group's scan, or null (none yet, unreadable, or another format). export async function readCopiesFile(transcriptsDir: string, group: string): Promise { try { const v = JSON.parse(await readFile(copiesPath(transcriptsDir, group), "utf8")) as unknown; return isCopiesFile(v) && v.group === group ? v : null; } catch { return null; } } export async function writeCopiesFile(transcriptsDir: string, file: CopiesFile): Promise { const p = copiesPath(transcriptsDir, file.group); await writeJsonAtomic(p, file, { mkdir: true }); return p; } export function emptyDecisions(group: string): CopiesDecisionsFile { return { format: COPIES_DECISIONS_FORMAT, version: COPIES_VERSION, group, released: {}, confirmed: {}, rejected: {} }; } // The group's decisions; none (or an unreadable file) is an empty set. export async function readCopiesDecisions(transcriptsDir: string, group: string): Promise { try { const v = JSON.parse(await readFile(copiesDecisionsPath(transcriptsDir, group), "utf8")) as unknown; if (isCopiesDecisionsFile(v) && v.group === group) return v; } catch { // none yet } return emptyDecisions(group); } export type CopiesDecisionChange = { // `/` keys to release (or, with undo, un-release). release?: readonly string[]; // Pair keys to confirm / reject (or, with undo, take back). confirm?: readonly string[]; reject?: readonly string[]; undo?: boolean; by: CopyDecisionBy; note?: string; now?: () => Date; }; const sortedMap = (m: Record) => Object.fromEntries(Object.entries(m).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0))); /** * THE ONE WRITER of a group's decisions, under the file's lock, atomically: * release videos, confirm or reject pairs, or (`undo`) take any of those back. * A pair is confirmed or rejected, never both: the later word replaces the * earlier one. Answers the file as written. */ export async function updateCopiesDecisions( transcriptsDir: string, group: string, change: CopiesDecisionChange, ): Promise { const p = copiesDecisionsPath(transcriptsDir, group); return withJsonFileLock(p, async () => { const file = await readCopiesDecisions(transcriptsDir, group); const entry: CopyDecision = { at: (change.now?.() ?? new Date()).toISOString(), by: change.by, ...(change.note ? { note: change.note } : {}), }; const released = { ...file.released }; const confirmed = { ...file.confirmed }; const rejected = { ...file.rejected }; for (const k of change.release ?? []) { if (change.undo) delete released[k]; else released[k] = entry; } for (const k of change.confirm ?? []) { if (change.undo) delete confirmed[k]; else { confirmed[k] = entry; delete rejected[k]; } } for (const k of change.reject ?? []) { if (change.undo) delete rejected[k]; else { rejected[k] = entry; delete confirmed[k]; } } const next: CopiesDecisionsFile = { ...file, released: sortedMap(released), confirmed: sortedMap(confirmed), rejected: sortedMap(rejected), }; await writeJsonAtomic(p, next, { mkdir: true }); return next; }); }