feat(waiter): a table's earlier orders (platform-v2 step 4.7)

- GET /api/tables/{id}/history: the table's closed / cancelled orders, newest first,
  for the waiters of its zone (ordering.orders.open), never older than the history
  module's history_days (free tier 31, D-6), module.disabled when history is off;
  each order with times, total (cancelled items left out), people and items.
- Waiter: a History tab on the table (and "This table's earlier orders" on a free
  table); orders expand to their items; the last answer is kept on the phone and
  shown offline, marked as saved; the plan's day limit is shown.
- Tests: test_table_history.py (4), waiter e2e +2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 04:45:31 +02:00
co-authored by Claude Opus 5.5
parent f8d91e7fae
commit 9e1912bce0
7 changed files with 306 additions and 2 deletions
@@ -0,0 +1,90 @@
import { useEffect, useState } from 'react'
import { useFormat, useTranslation } from '@xenia/i18n'
import client from '../api/client'
import { apiError } from '../api/errors'
import db from '../db/posdb'
// A table's earlier orders (plan step 4.7): GET /api/tables/{id}/history, never older than
// the venue's `history` module allows. The last answer is kept on the phone (`kv`
// history:<table id>) and shown while offline, marked as saved.
const CACHE = id => `history:${id}`
export default function TableHistory({ tableId }) {
const { t } = useTranslation('waiter')
const { formatTime, formatDate, formatMoney } = useFormat()
const [state, setState] = useState({ loading: true, data: null, cached: false, error: '' })
const [open, setOpen] = useState(null)
useEffect(() => {
let alive = true
client.get(`/api/tables/${tableId}/history`)
.then(async r => {
try { await db.kv.put({ key: CACHE(tableId), value: { ...r.data, saved_at: new Date().toISOString() } }) } catch { /* storage full: still shown */ }
if (alive) setState({ loading: false, data: r.data, cached: false, error: '' })
})
.catch(async err => {
const saved = await db.kv.get(CACHE(tableId)).then(row => row?.value ?? null).catch(() => null)
if (alive) setState({ loading: false, data: saved, cached: !!saved, error: saved ? '' : apiError(err, t('history.failed')) })
})
return () => { alive = false }
}, [tableId]) // eslint-disable-line react-hooks/exhaustive-deps
if (state.loading) return <p style={{ textAlign: 'center', color: 'var(--muted)', padding: 24 }}>{t('common.loading')}</p>
if (state.error) return <p data-testid="history-error" style={{ textAlign: 'center', color: 'var(--danger)', padding: 24, fontSize: 13 }}>{state.error}</p>
const orders = state.data?.orders ?? []
const days = state.data?.history_days
return (
<div data-testid="table-history" style={{ display: 'flex', flexDirection: 'column', gap: 8, padding: '10px 12px 24px' }}>
{state.cached && (
<div data-testid="history-cached" style={{ fontSize: 12, color: 'var(--muted)', textAlign: 'center' }}>
{t('history.cached', { time: formatTime(state.data.saved_at) })}
</div>
)}
{orders.length === 0 && (
<p style={{ textAlign: 'center', color: 'var(--muted)', padding: 24, fontSize: 14 }}>{t('history.none')}</p>
)}
{orders.map(o => {
const expanded = open === o.id
return (
<div key={o.id} data-testid="history-order" style={{ background: 'var(--bg2)', border: '1px solid var(--border)', borderRadius: 12, overflow: 'hidden' }}>
<button onClick={() => setOpen(expanded ? null : o.id)} style={{
width: '100%', display: 'flex', alignItems: 'center', gap: 10, padding: '12px 14px',
background: 'none', border: 'none', cursor: 'pointer', textAlign: 'left', color: 'var(--text)',
}}>
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ fontSize: 14, fontWeight: 700 }}>
{formatDate(o.opened_at, 'dateShort')} · {formatTime(o.opened_at)}{o.closed_at ? `–${formatTime(o.closed_at)}` : ''}
{o.status === 'cancelled' && <span style={{ marginLeft: 8, fontSize: 11, color: 'var(--danger)' }}>{t('history.cancelled')}</span>}
</div>
<div style={{ fontSize: 12, color: 'var(--muted)', marginTop: 2 }}>
#{o.id} · {t('orders.items', { count: o.items.length })}{o.people.length ? ` · ${o.people.join(', ')}` : ''}
</div>
</div>
<span style={{ fontSize: 15, fontWeight: 800, fontVariantNumeric: 'tabular-nums' }}>{formatMoney(o.total)}</span>
</button>
{expanded && (
<div style={{ borderTop: '1px solid var(--border)', padding: '8px 14px 12px', display: 'flex', flexDirection: 'column', gap: 6 }}>
{o.items.map(i => (
<div key={i.id} style={{ display: 'flex', gap: 8, fontSize: 13, opacity: i.status === 'cancelled' ? 0.5 : 1 }}>
<span style={{ fontWeight: 700, minWidth: 28 }}>×{i.quantity}</span>
<span style={{ flex: 1, textDecoration: i.status === 'cancelled' ? 'line-through' : 'none' }}>
{i.name}
{i.options.length > 0 && <span style={{ color: 'var(--muted)' }}> · {i.options.join(', ')}</span>}
</span>
<span style={{ fontVariantNumeric: 'tabular-nums' }}>{formatMoney(i.total)}</span>
</div>
))}
</div>
)}
</div>
)
})}
{days != null && (
<div data-testid="history-limit" style={{ fontSize: 11, color: 'var(--muted)', textAlign: 'center', marginTop: 4 }}>
{t('history.limit', { count: days })}
</div>
)}
</div>
)
}
+27 -1
View File
@@ -1,7 +1,8 @@
import { useEffect, useState, useRef } from 'react'
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'
import { useAllowed, useCan } from '@xenia/access'
import { useAllowed, useCan, useModule } from '@xenia/access'
import OrderSummary from '../components/OrderSummary'
import TableHistory from '../components/TableHistory'
import useConnectionStore from '../store/connectionStore'
import client from '../api/client'
import { apiError } from '../api/errors'
@@ -1079,6 +1080,8 @@ export default function TableDetailPage() {
// Tab: 'active' | 'paid'
const [activeTab, setActiveTab] = useState('active')
const historyOn = useModule('history') // 4.7: a table's earlier orders
const [showHistory, setShowHistory] = useState(false)
// Actions sheet state
const [showActions, setShowActions] = useState(false)
@@ -1702,6 +1705,12 @@ export default function TableDetailPage() {
<button className="btn btn--primary btn--lg" onClick={openOrder}>
{t('detail.openOrder')}
</button>
{historyOn && (
<button className="btn btn--secondary" data-testid="history-open" style={{ marginTop: 12 }} onClick={() => setShowHistory(s => !s)}>
{t('history.earlier')}
</button>
)}
{historyOn && showHistory && <div style={{ width: '100%', maxHeight: '50vh', overflowY: 'auto' }}><TableHistory tableId={Number(tableId)} /></div>}
</div>
)}
@@ -1780,6 +1789,21 @@ export default function TableDetailPage() {
}}>{paidItems.length}</span>
)}
</button>
{historyOn && !isStandalone && (
<button
data-testid="tab-history"
onClick={() => setActiveTab('history')}
style={{
flex: 1, padding: '9px 0', border: 'none', cursor: 'pointer',
fontWeight: 700, fontSize: 13,
background: activeTab === 'history' ? 'var(--bg3)' : 'transparent',
color: activeTab === 'history' ? 'var(--text)' : 'var(--muted)',
transition: 'background 0.15s',
}}
>
{t('history.tab')}
</button>
)}
</div>
{/* Merge view button */}
@@ -1804,6 +1828,8 @@ export default function TableDetailPage() {
)}
</div>
{activeTab === 'history' && <TableHistory tableId={Number(tableId)} />}
{activeTab === 'active' && (
<>
<OrderSummary