feat: printing problems shown and handled; printing set-up on v2 (platform-v2 step 6.10)

- a ticket that doesn't print is a problem for whoever sent it and the managers:
  GET /api/print-problems (the order, table, dishes, printer, since when, tries,
  last error, who sent it) and three ways out: print again, send to another
  printer, handled (services/print_problems.py; print_jobs.resolved_by /
  redirected_to, Alembic 0036); live event print.problems from the print paths
- phone: the red printer button opens the problems sheet, the table page warns
  (usePrintProblems replaces the 8 s poll); counter POS: a red button under the
  modes with the shared panel (@xenia/ordering PrintProblemsPanel + pure rules);
  manager: a card on the overview (top), the Tables page and the order page
- manager: Settings -> Printing (printers with reachability, test print, network
  scan; ticket layout + test ticket, beep, fonts with previews, dot leader,
  dividers) and Management -> Prep zones (cards + zone dialog with the ticket
  language, printers, copies, sorting, category order, kitchen screen) rebuilt in
  src/pages/printing; v1 PrintFontsTab, PrepZonesConfigPage, PendingPrints and
  /api/orders/pending-prints removed
- access: printer add/edit/delete/scan need core.settings.manage, prep zone
  changes catalog.menu.manage (were any dashboard user)
- tests: test_print_problems.py, @xenia/ordering printProblems, manager
  printing.test.mjs, e2e print_problems.mjs (done-when) + manager.mjs printing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 01:40:07 +02:00
co-authored by Claude Opus 5.5
parent 070c9cf625
commit f31bdf42aa
57 changed files with 2557 additions and 2221 deletions
+4 -2
View File
@@ -7,13 +7,15 @@
"main": "src/index.js",
"exports": {
".": "./src/index.js",
"./cart": "./src/cart.js"
"./cart": "./src/cart.js",
"./printProblems": "./src/printProblems.js"
},
"dependencies": {
"@xenia/access": "^0.0.0",
"@xenia/i18n": "^0.0.0",
"@xenia/ui": "^0.0.0",
"lucide-react": "^1.73.0"
"lucide-react": "^1.73.0",
"@xenia/api": "^0.0.0"
},
"scripts": {
"test": "node --test \"tests/*.test.mjs\""
@@ -0,0 +1,76 @@
// The tickets that didn't print (plan step 6.10), for the manager and the counter POS: per
// ticket where it belongs, which printer, since when, how many tries, what's on it and who sent
// it; then Print again, Send to another printer, or Handled (told the kitchen another way).
// `run(kind, problem, printerId)` does the request and resolves (or throws the API error);
// the list itself comes back through the live event, so this keeps no copy of it.
import { useState } from 'react'
import { CheckCheck, Printer, RotateCw, Send } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Select } from '@xenia/ui'
import { itemsText, otherPrinters, placeOf } from './printProblems.js'
export function PrintProblemsPanel({ problems, printers = [], run, onOpen, showPlace = true }) {
return (
<ul className="divide-y divide-line" data-testid="print-problems">
{problems.map(p => (
<Problem key={p.job_id} problem={p} printers={printers} run={run} onOpen={onOpen} showPlace={showPlace} />
))}
</ul>
)
}
function Problem({ problem: p, printers, run, onOpen, showPlace }) {
const { t } = useTranslation('common')
const { formatTime } = useFormat()
const others = otherPrinters(p, printers)
const [to, setTo] = useState(others[0] ? String(others[0].id) : '')
const [busy, setBusy] = useState(null)
const [error, setError] = useState(null)
async function act(kind, printerId) {
setBusy(kind)
setError(null)
try { await run(kind, p, printerId) } catch (err) { setError(errorMessage(err, t)) } finally { setBusy(null) }
}
return (
<li className="px-4 py-3 grid gap-2" data-testid="print-problem" data-job={p.job_id} data-order={p.order_id}>
<div className="flex flex-wrap items-center gap-x-3 gap-y-1">
{showPlace && (
<button type="button" className="text-body-lg font-bold text-ink hover:underline disabled:no-underline" disabled={!onOpen} onClick={() => onOpen?.(p)}>
{placeOf(p)}
</button>
)}
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full bg-danger-soft text-danger-ink text-caption font-semibold">
<Printer size={12} aria-hidden="true" />{t('printProblems.notOn', { printer: p.printer.name })}
</span>
<span className="text-caption text-ink-2">
{t('printProblems.since', { time: formatTime(p.since) })} · {t('printProblems.tries', { count: p.attempts })}
{p.sent_by?.length > 0 && ` · ${t('printProblems.sentBy', { names: p.sent_by.join(', ') })}`}
</span>
</div>
<div className="text-body text-ink">{itemsText(p)}</div>
{p.error && <div className="text-caption text-ink-3 truncate" title={p.error}>{p.error}</div>}
<div className="flex flex-wrap items-center gap-2">
<Button size="sm" variant="secondary" icon={<RotateCw size={14} />} loading={busy === 'retry'} disabled={!!busy} onClick={() => act('retry')} data-testid="problem-retry">
{t('printProblems.retry')}
</Button>
{others.length > 0 && (
<span className="flex items-center gap-1">
<Select size="sm" value={to} onChange={e => setTo(e.target.value)} aria-label={t('printProblems.sendTo')} data-testid="problem-printer"
options={others.map(o => ({ value: String(o.id), label: o.name }))} />
<Button size="sm" icon={<Send size={14} />} loading={busy === 'redirect'} disabled={!!busy || !to} onClick={() => act('redirect', Number(to))} data-testid="problem-redirect">
{t('printProblems.send')}
</Button>
</span>
)}
<Button size="sm" variant="ghost" icon={<CheckCheck size={14} />} loading={busy === 'handled'} disabled={!!busy} onClick={() => act('handled')} data-testid="problem-handled"
title={t('printProblems.handledHint')}>
{t('printProblems.handled')}
</Button>
</div>
{error && <p role="alert" className="text-label text-danger-ink">{error}</p>}
</li>
)
}
+4 -1
View File
@@ -1,6 +1,9 @@
// @xenia/ordering (plan step 6.9): what a person chooses for a product and how it becomes a line
// the server prices (line ids, 4.4), one implementation for the counter POS, the menu tablet and
// the manager's ordering. Pure rules in ./cart (Node-testable); the dialog and the quote hook.
// the manager's ordering. Pure rules in ./cart (Node-testable); the dialog and the quote hook;
// printing problems (6.10): their pure rules and the panel the manager and the counter show.
export * from './cart.js'
export { ProductDialog } from './ProductDialog.jsx'
export { useQuote } from './useQuote.js'
export * from './printProblems.js'
export { PrintProblemsPanel } from './PrintProblemsPanel.jsx'
+43
View File
@@ -0,0 +1,43 @@
// 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}`, {}]
}
@@ -0,0 +1,29 @@
// Printing problems as plain data (plan step 6.10).
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { actionRequest, byPrinter, itemsText, minutesSince, otherPrinters, placeOf, problemsOf } from '../src/printProblems.js'
const p = (job, order, printer, extra = {}) => ({
job_id: job, order_id: order, table_name: null, printer: { id: printer, name: `P${printer}` },
items: [{ name: 'Burger', quantity: 2, unit_type: 'piece' }, { name: 'Freddo', quantity: 1, unit_type: 'piece' }],
since: '2026-11-19T10:00:00', ...extra,
})
test('what a ticket holds, where it belongs, how long it waits', () => {
const a = p(1, 7, 1, { table_name: 'T5' })
assert.equal(itemsText(a), '2× Burger, Freddo')
assert.equal(placeOf(a), 'T5')
assert.equal(placeOf(p(2, 8, 1)), '#8')
assert.equal(minutesSince(a, Date.parse('2026-11-19T10:07:30Z')), 7) // a time without a zone is UTC
assert.equal(minutesSince({ ...a, since: '2026-11-19T10:00:00+02:00' }, Date.parse('2026-11-19T08:03:00Z')), 3)
})
test('per order, per printer, the printers to send to, the requests', () => {
const list = [p(1, 7, 1), p(2, 7, 2), p(3, 8, 1)]
assert.deepEqual(problemsOf(list, 7).map(x => x.job_id), [1, 2])
assert.deepEqual(byPrinter(list).map(x => [x.printer.id, x.count]), [[1, 2], [2, 1]])
assert.deepEqual(otherPrinters(list[0], [{ id: 1 }, { id: 2 }, { id: 3 }]).map(x => x.id), [2, 3])
assert.deepEqual(actionRequest('redirect', list[0], 3), ['/api/print-problems/1/redirect', { printer_id: 3 }])
assert.deepEqual(actionRequest('handled', list[0]), ['/api/print-problems/1/handled', {}])
assert.deepEqual(actionRequest('retry', list[0]), ['/api/print-problems/1/retry', {}])
})