feat(ordering): orders priced by the server (platform-v2 step 4.4)

The waiter app (and the manager's ordering tab) send what was chosen, by id; the
server prices it with the counter's code. No app sends a price any more.

- POST /api/orders/{id}/items takes `lines` (pos_cart's line + course_id,
  price_adjustment, is_service_item): sales point checked first, refusals name the
  product and line (order.line.*, order.not_sold_here); prices or selected_options in a
  line -> order.line.client_price, the old `items` shape -> order.lines_required; a
  by-hand price change needs ordering.prices.modify or a manager's approval (audited
  PRICE_SET_BY_HAND). add_priced_items is the one writer of priced items (pos too).
- POST /api/orders/quote: the cart's total before sending; /api/pos/quote shares
  pos_cart.quote().
- pos_cart: a preference set left out takes its default; a set at its free default
  isn't stored; a default that costs is charged (the waiter never sent it: fixed);
  an extra's sub-choice is charged per extra (the counter charged it once: fixed).
- @xenia/api lines.js: lineFromOptions / wireLine / estimate / LineError.
- Waiter: lines on send; cart total from the quote ("≈ estimate" offline); summary
  shows the same prices; offline orders queued by id, priced on sync, refused lines
  shown on a "Δεν στάλθηκαν" sheet (no empty order opened); dead ItemOptionsModal gone.
- Tests: test_order_lines.py (14), lines.test.mjs, waiter e2e +6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 03:35:03 +02:00
co-authored by Claude Opus 5.5
parent bcf91bf6e9
commit 8e0a786bbc
25 changed files with 1052 additions and 656 deletions
+90 -2
View File
@@ -1,6 +1,8 @@
// Waiter app suite (plan phase 4): sign-in v2 on the phone (4.2): each login mode, wrong PIN,
// one session per waiter, lock / unlock / idle sign-out, a manager's approval on the phone;
// what a waiter may do from permission keys and the venue's role defaults (4.3).
// what a waiter may do from permission keys and the venue's role defaults (4.3); orders priced
// by the server: the cart's total from a quote, lines sent by id, an offline order priced when
// the phone is back and a refused line shown to the waiter (4.4).
// Starts its own backend (:8061) and the venue-mode dev build (:5181), phone-sized Edge.
// node waiter.mjs
import path from 'node:path'
@@ -13,7 +15,7 @@ const vite = await startViteNative({ port: 5181, backendUrl: backend.url })
const APP = 'http://localhost:5181'
const TOKEN_KEY = 'xenia:dev:token'
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
const r = reporter('waiter: sign-in v2, lock, approvals (4.2), keys (4.3)')
const r = reporter('waiter: sign-in v2, lock, approvals (4.2), keys (4.3), server prices (4.4)')
const errors = []
let failures = 0
@@ -156,6 +158,92 @@ try {
await api('DELETE', `/api/orders/${o3.id}`, { token: boss }).catch(() => {})
await a.goto(`${APP}/tables`)
// ── 4.4: orders priced by the server ────────────────────────────────────────
const prods = await api('GET', '/api/products/', { token: boss })
const simple = p => p.quick_add_enabled && p.is_available && !p.is_service_item && p.lifecycle_status === 'active'
&& !(p.preference_sets || []).some(ps => ps.default_choice_id == null)
const free = tables.filter(t => ![table.id, t2.id].includes(t.id) && !t.open_order_id && !t.current_order_id)
const [t4, t5] = free
// two products the first category on screen offers with a quick "+"
await a.goto(`${APP}/tables/${t4.id}/add?new=1`)
// the picker opens on favourites (none yet): pick the first category that has a quick "+"
let category = null
for (const c of (await api('GET', '/api/products/categories', { token: boss })).filter(c => !c.parent_id)) {
await a.getByRole('button', { name: c.name, exact: true }).first().click({ timeout: 5000 }).catch(() => {})
if (await a.locator('[data-testid="product-quick-add"]').first().waitFor({ timeout: 3000 }).then(() => true).catch(() => false)) { category = c.name; break }
}
await a.locator('[data-testid="product-quick-add"]').first().waitFor({ timeout: 5000 })
.catch(async e => { await a.screenshot({ path: path.join(OUT, 'waiter-add-items.png') }); throw e })
const onScreen = await a.locator('[data-testid="product-card"]:has([data-testid="product-quick-add"])').evaluateAll(els => els.map(e => e.dataset.name))
const [plain, other] = prods.filter(p => simple(p) && onScreen.includes(p.name))
// a price the phone's menu doesn't know (a price modifier): only the server can say it
const mod = await api('POST', '/api/pricing/modifiers', { token: boss, body: {
name: 'e2e 4.4', scope: 'item', item_id: plain.id, action_type: 'set', action_value: 1.2 } })
const addOn = async (tableId, product, times = 1) => {
if (!(await pathOf(a)).endsWith('/add')) await a.goto(`${APP}/tables/${tableId}/add?new=1`)
await a.getByRole('button', { name: category, exact: true }).first().click({ timeout: 5000 }).catch(() => {})
const card = a.locator(`[data-testid="product-card"][data-name="${product.name}"]`)
await card.first().waitFor({ timeout: 10_000 })
for (let i = 0; i < times; i++) await card.getByTestId('product-quick-add').first().click()
}
const total = () => a.getByTestId('cart-total')
await addOn(t4.id, plain, 2)
await a.getByTestId('cart-open').click()
const exact = await waitFor(async () => await total().getAttribute('data-estimated') === 'false', 10_000, 300)
const shownTotal = await total().innerText().catch(() => '')
r.check('4.4: the cart total is the server\'s price (a price the phone\'s menu doesn\'t know)', !!exact && shownTotal.includes('2.40'), shownTotal.replace(/\s+/g, ' '))
await a.getByRole('button', { name: /^(ΑΠΟΣΤΟΛΗ|ΣΥΝΟΨΗ)/ }).last().click()
if (await shown(a, 'summary-total', 3000)) {
r.check('4.4: the summary shows the same total', (await a.getByTestId('summary-total').innerText()).includes('2.40'))
await a.getByRole('button', { name: /ΑΠΟΣΤΟΛΗ/ }).last().click()
}
const sent = await waitFor(async () => {
const st = await api('GET', `/api/tables/${t4.id}/status`, { token: boss })
if (!st.active_order_id) return null
const o = await api('GET', `/api/orders/${st.active_order_id}`, { token: boss })
return o.items.length ? o : null
}, 10_000, 500)
r.check('4.4: sent by id, priced by the server: the order costs what the cart said',
!!sent && sent.items.length === 1 && sent.items[0].quantity === 2 && sent.items[0].unit_price === 1.2, JSON.stringify(sent?.items?.map(i => [i.quantity, i.unit_price])))
// offline: the order is queued by id, shown as an estimate, priced when the phone is back;
// a product taken off the menu meanwhile comes back to the waiter as refused
await a.goto(`${APP}/tables/${t5.id}/add?new=1`)
await addOn(t5.id, plain)
await ctxA.setOffline(true)
await a.getByTestId('cart-open').click()
const wentOffline = await waitFor(async () => (await total().innerText().catch(() => '')).includes('χωρίς σύνδεση'), 30_000, 1000)
const est = await total().getAttribute('data-estimated').catch(() => null)
r.check('4.4: offline the cart total is an estimate (≈, the menu prices)', !!wentOffline && est === 'true'
&& (await total().innerText()).includes('≈'), `${wentOffline} ${est} ${await total().innerText().catch(() => '')}`)
await a.mouse.click(10, 400)
await sleep(500)
await addOn(t5.id, other)
await a.getByTestId('cart-open').click()
await a.getByRole('button', { name: /^(ΑΠΟΣΤΟΛΗ|ΣΥΝΟΨΗ)/ }).last().click()
if (await shown(a, 'summary-total', 3000)) await a.getByRole('button', { name: /ΑΠΟΣΤΟΛΗ/ }).last().click()
await sleep(2000)
await api('PUT', `/api/products/${other.id}`, { token: boss, body: { is_available: false } })
await ctxA.setOffline(false)
const synced = await waitFor(async () => {
const st = await api('GET', `/api/tables/${t5.id}/status`, { token: boss })
if (!st.active_order_id) return null
const o = await api('GET', `/api/orders/${st.active_order_id}`, { token: boss })
return o.items.length ? o : null
}, 40_000, 1000)
r.check('4.4: back online the queued order is priced by the server (not the phone\'s estimate)',
!!synced && synced.items.length === 1 && synced.items[0].product_id === plain.id && synced.items[0].unit_price === 1.2,
JSON.stringify(synced?.items?.map(i => [i.product_id, i.unit_price])))
const refusedShown = await shown(a, 'refused-lines', 15_000)
const refusedText = await a.getByTestId('refused-lines').innerText().catch(() => '')
r.check('4.4: the line the server refused comes back to the waiter with the reason', refusedShown && refusedText.includes(other.name), refusedText.replace(/\s+/g, ' ').slice(0, 200))
await a.screenshot({ path: path.join(OUT, 'waiter-refused-lines.png') })
await a.getByTestId('refused-lines-ok').click().catch(() => {})
await api('PUT', `/api/products/${other.id}`, { token: boss, body: { is_available: true } })
await api('DELETE', `/api/pricing/modifiers/${mod.id}`, { token: boss }).catch(() => {})
for (const o of [sent, synced]) if (o) await api('DELETE', `/api/orders/${o.id}`, { token: boss }).catch(() => {})
await a.goto(`${APP}/tables`)
// ── Signing out ends the session on the server: the second phone can sign in ─
await a.getByRole('button', { name: 'demo_maria' }).first().click({ timeout: 10_000 })
await a.getByRole('button', { name: /Αποσύνδεση/ }).last().click()