feat(connect): the QR menu at the table: one code per table, orders straight onto the table

Plan phase 3S, step S.7.
- tables.qr_token (migration 0021): a random token per active table; services/table_qr.py
  (tokens, rotate, the snapshot's `qr` with the tokens' sha256 only, the code's link from the
  cloud's apps_url); setting connect_menu.table_ordering (read-only by default).
- /api/connect/qr (connect_menu.manage): the codes, ordering on/off, a new code per table or
  for all; table and QR changes push the menu at once (MENU_PATHS).
- Pull: a QR order with a table id goes onto the table's open order or a new one
  (services/qr_orders.py): items at the cloud's prices, kitchen tickets, KDS, order_updated
  (the waiters' phones and the counter), a `qr_order` message to the order's waiters or the
  table's zone waiters; audit QR_ORDER (a repeated pull adds nothing); the cloud is told
  "accepted" (the guest's page: in the kitchen). No usable table or no open day → the counter
  gets it as before. Waiter app: the message's look.
- Manager: Online → Table QR codes (/online/qr): ordering switch, the menu link, a QR card
  per table (print all), new code per table / for all.
- Heartbeat: apps_url kept in the license state.
- Tests: tests/test_qr_table.py (5); e2e qr_table.mjs (a real cloud and venue: table 5's
  code → two dishes on table 5's order, KDS, live event, the guest's page; a forged code
  refused; a new code kills the old one), in run-all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 01:26:55 +02:00
co-authored by Claude Opus 5.5
parent 70c8f2bb57
commit 652e6a26af
19 changed files with 773 additions and 13 deletions
@@ -0,0 +1,86 @@
// The tables' QR codes (plan phase 3S, step S.7): one code per table that opens the QR menu
// at that table. The venue chooses whether guests can order from it (straight onto the
// table's order, to the kitchen, with a message to the table's waiters) or only read it. A
// lost or copied code is replaced with "New code" (the printed one stops working within
// seconds). Print puts every table's code on paper, one card each.
// Backend: /api/connect/qr (connect_menu.manage), services/table_qr.py.
import { useCallback, useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import { QRCodeSVG } from 'qrcode.react'
import { Printer, RefreshCw } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, Dialog, EmptyState, Spinner, Switch } from '@xenia/ui'
import client from '../../api/client'
export default function QrCodesPage() {
const { t } = useTranslation('manager')
const [data, setData] = useState(null)
const [error, setError] = useState(null)
const [confirmAll, setConfirmAll] = useState(false)
const [busy, setBusy] = useState(false)
const load = useCallback(async () => {
try { setData((await client.get('/api/connect/qr')).data); setError(null) } catch (err) { setError(errorMessage(err, t)) }
}, [t])
useEffect(() => { load() }, [load])
const act = async (fn, done) => {
setBusy(true)
try { setData((await fn()).data); if (done) toast.success(done) } catch (err) { toast.error(errorMessage(err, t)) }
setBusy(false)
}
if (error) return <div className="p-6"><EmptyState title={error} /></div>
if (!data) return <div className="p-6 flex justify-center"><Spinner /></div>
return (
<div className="p-4 md:p-6 grid gap-6 max-w-6xl" data-testid="qr-page">
<header className="grid gap-1 print:hidden">
<h1 className="text-heading font-semibold text-ink">{t('qr.title')}</h1>
<p className="text-body text-ink-2 max-w-3xl">{t('qr.intro')}</p>
</header>
<section className="p-4 rounded-card border border-line bg-surface grid gap-3 print:hidden">
<Switch checked={data.ordering} disabled={busy} data-testid="qr-ordering"
onChange={on => act(() => client.put('/api/connect/qr/ordering', { on }), t('fees.saved'))}
label={t('qr.ordering')} description={t(data.ordering ? 'qr.orderingOn' : 'qr.orderingOff')} />
<p className="text-label text-ink-2">{t('qr.menuLink')} <a className="text-accent-ink font-medium break-all" href={data.menu_url} target="_blank" rel="noreferrer">{data.menu_url}</a></p>
<div className="flex flex-wrap gap-2">
<Button icon={<Printer size={16} />} onClick={() => window.print()} disabled={!data.tables.length}>{t('qr.print')}</Button>
<Button variant="secondary" icon={<RefreshCw size={16} />} onClick={() => setConfirmAll(true)} disabled={!data.tables.length}>{t('qr.rotateAll')}</Button>
</div>
</section>
{data.tables.length === 0 ? (
<EmptyState title={t('qr.noTables')} />
) : (
<ul className="grid gap-4 print:gap-6" style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(13rem, 1fr))' }} data-testid="qr-tables">
{data.tables.map(tb => (
<li key={tb.id} className="p-4 rounded-card border border-line bg-surface grid justify-items-center gap-2 break-inside-avoid" data-testid="qr-table" data-table={tb.id} data-url={tb.url}>
<span className="text-title font-semibold text-ink">{t('qr.table', { name: tb.name })}</span>
{tb.group && <span className="text-label text-ink-2">{tb.group}</span>}
<QRCodeSVG value={tb.url} size={168} includeMargin={false} />
<span className="text-caption text-ink-3 text-center">{t(data.ordering ? 'qr.scanOrder' : 'qr.scanRead')}</span>
<Button size="sm" variant="ghost" icon={<RefreshCw size={14} />} disabled={busy} className="print:hidden"
onClick={() => act(() => client.post(`/api/connect/qr/tables/${tb.id}/rotate`), t('qr.rotated', { name: tb.name }))}
data-testid="qr-rotate">{t('qr.rotate')}</Button>
</li>
))}
</ul>
)}
{confirmAll && (
<Dialog open size="sm" title={t('qr.rotateAllTitle')} onClose={() => setConfirmAll(false)}
footer={<>
<Button variant="secondary" onClick={() => setConfirmAll(false)}>{t('fees.cancel')}</Button>
<Button variant="danger" loading={busy} onClick={() => act(() => client.post('/api/connect/qr/rotate'), t('qr.rotatedAll')).then(() => setConfirmAll(false))}>
{t('qr.rotateAll')}
</Button>
</>}>
<p className="text-body text-ink-2">{t('qr.rotateAllBody')}</p>
</Dialog>
)}
</div>
)
}