// Printing problems (plan step 6.10), as plain data for every app: GET /api/print-problems // gives { problems, printers }; a problem is one ticket (an order's lines for one printer) // that didn't print. Node-tested (tests/printProblems.test.mjs). export const PRINT_EVENT = 'print.problems' /** The problems of one order (the order and table pages show only theirs). */ export const problemsOf = (problems = [], orderId) => problems.filter(p => p.order_id === orderId) /** The printers this ticket can be sent to instead (any but its own). */ export const otherPrinters = (problem, printers = []) => printers.filter(p => p.id !== problem.printer.id) /** "2× Burger, Freddo": what the ticket holds. */ export function itemsText(problem) { return problem.items.map(i => (i.quantity !== 1 || i.unit_type !== 'piece' ? `${i.quantity}× ${i.name}` : i.name)).join(', ') } /** Where the ticket belongs: the table's name, else the order number. */ export const placeOf = problem => problem.table_name || `#${problem.order_id}` /** Minutes since the first try (whole minutes, never negative). */ export function minutesSince(problem, now = Date.now()) { const t = problem.since ? new Date(/[zZ]|[+-]\d\d:?\d\d$/.test(problem.since) ? problem.since : `${problem.since}Z`).getTime() : now return Math.max(0, Math.floor((now - t) / 60_000)) } /** One line per printer with how many tickets wait on it (the counter's and the phones' summary). */ export function byPrinter(problems = []) { const out = new Map() for (const p of problems) { const e = out.get(p.printer.id) ?? { printer: p.printer, count: 0 } e.count += 1 out.set(p.printer.id, e) } return [...out.values()] } /** The request for an action: 'retry' | 'redirect' (with a printer) | 'handled'. → [url, body] */ export function actionRequest(kind, problem, printerId) { const base = `/api/print-problems/${problem.job_id}` if (kind === 'redirect') return [`${base}/redirect`, { printer_id: printerId }] return [`${base}/${kind}`, {}] }