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:
@@ -5,9 +5,11 @@
|
||||
// → an axios instance: auth/device headers, err.xenia on every error, manager-override retry
|
||||
// parseError(err) / errorMessage(err, t) / isError(err, code)
|
||||
// createLiveClient({ url, cursor, onEvent, onReady, onDisconnect }) WebSocket + cursor replay
|
||||
// lineFromOptions / wireLine / estimate order lines: what was chosen, by id; the server prices it (4.4)
|
||||
//
|
||||
// React glue (bootstrap, permissions, the override dialog) lives in @xenia/access.
|
||||
|
||||
export { createApiClient, DEVICE_HEADER, OVERRIDE_HEADER } from './client.js'
|
||||
export { parseError, errorMessage, isError } from './errors.js'
|
||||
export { createLiveClient, wsUrlFor, INITIAL_RECONNECT_DELAY, MAX_RECONNECT_DELAY } from './live.js'
|
||||
export { LineError, lineFromOptions, wireLine, estimate } from './lines.js'
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
// Order lines: what POST /api/orders/{id}/items and POST /api/orders/quote take (plan step 4.4).
|
||||
//
|
||||
// An app never sends a price. A line says what was chosen, by id (sub-choices by name):
|
||||
// { product_id, quantity, notes?, course_id?, price_adjustment?, is_service_item?,
|
||||
// quick: [{id, qty?}], options: [{id, qty?, subs: [name]}],
|
||||
// prefs: [{set_id, choices: [{id, qty?, sub?}], shared?}], removed: [ingredient_id] }
|
||||
// and the server prices it (local_backend/services/pos_cart.py; the counter POS builds the
|
||||
// same lines in terminal/src/apps/pos/logic/cart.js). A preference set left out takes its
|
||||
// default on the server.
|
||||
//
|
||||
// The waiter app and the manager's ordering tab keep their cart in the shape an order item is
|
||||
// stored in (`selected_options` entries tagged quick | extra | extra_sub | pref | pref_sub, with
|
||||
// the prices of the menu they have, `removed_ingredients` by name). `lineFromOptions` turns that
|
||||
// into a line against the product as the menu lists it; the prices in the entries are only used
|
||||
// for `estimate` (a cart total while offline, shown as an estimate).
|
||||
|
||||
/** An entry that can't be matched to the product any more (an option or ingredient was
|
||||
* deleted since the item was put in the cart, or "order again" of an old item). */
|
||||
export class LineError extends Error {
|
||||
constructor(product, what) {
|
||||
super(`${product?.name ?? 'product'}: "${what}" is not on the menu any more`)
|
||||
this.name = 'LineError'
|
||||
this.product = product?.name ?? null
|
||||
this.what = what
|
||||
}
|
||||
}
|
||||
|
||||
const QTY = /^(.*) x(\d+)$/
|
||||
const splitQty = name => { const m = QTY.exec(name ?? ''); return m ? [m[1], Number(m[2])] : [name ?? '', 1] }
|
||||
const isSub = (e, type) => !!e && (e.type === type || (!e.type && e.id == null))
|
||||
const named = (list, name) => (list ?? []).some(s => s.name === name)
|
||||
|
||||
/** The line for a cart item / stored order item `{selected_options, removed_ingredients}` of
|
||||
* `product` (the menu's product: quick_options, options, preference_sets, ingredients). */
|
||||
export function lineFromOptions(product, item = {}) {
|
||||
const line = { product_id: product?.id ?? item.product_id }
|
||||
const opts = item.selected_options ?? []
|
||||
const removedNames = item.removed_ingredients ?? []
|
||||
if (!product) {
|
||||
if (opts.length || removedNames.length) throw new LineError(null, opts[0]?.name ?? removedNames[0])
|
||||
return line
|
||||
}
|
||||
const sets = product.preference_sets ?? []
|
||||
const setOf = new Map(sets.flatMap(ps => (ps.choices ?? []).map(c => [c.id, ps])))
|
||||
const optById = new Map((product.options ?? []).map(o => [o.id, o]))
|
||||
const quickById = new Map((product.quick_options ?? []).map(q => [q.id, q]))
|
||||
const quickByName = new Map((product.quick_options ?? []).map(q => [q.name, q]))
|
||||
|
||||
const quick = new Map()
|
||||
const options = []
|
||||
const prefs = new Map()
|
||||
for (let i = 0; i < opts.length; i++) {
|
||||
const o = opts[i]
|
||||
if (o.type === 'pref' || (!o.type && o.id != null && setOf.has(o.id))) {
|
||||
const ps = setOf.get(o.id)
|
||||
const c = ps?.choices.find(x => x.id === o.id)
|
||||
if (!c) throw new LineError(product, o.name)
|
||||
const entry = prefs.get(ps.id) ?? { set_id: ps.id, choices: [] }
|
||||
prefs.set(ps.id, entry)
|
||||
if (ps.allow_multi_select) {
|
||||
const [, qty] = splitQty(o.name)
|
||||
entry.choices.push(qty > 1 ? { id: c.id, qty } : { id: c.id })
|
||||
continue
|
||||
}
|
||||
const pick = { id: c.id }
|
||||
if (c.sub_choices?.length && isSub(opts[i + 1], 'pref_sub') && named(c.sub_choices, opts[i + 1].name)) pick.sub = opts[++i].name
|
||||
const shared = !c.disables_subset ? ps.shared_subset?.choices : null
|
||||
if (shared?.length && isSub(opts[i + 1], 'pref_sub') && named(shared, opts[i + 1].name)) entry.shared = opts[++i].name
|
||||
entry.choices = [pick]
|
||||
} else if (o.type === 'extra' || (!o.type && o.id != null && optById.has(o.id))) {
|
||||
const opt = optById.get(o.id)
|
||||
if (!opt) throw new LineError(product, o.name)
|
||||
const [, qty] = splitQty(o.name)
|
||||
const subs = []
|
||||
while (isSub(opts[i + 1], 'extra_sub') && named(opt.sub_choices, splitQty(opts[i + 1].name)[0])) subs.push(splitQty(opts[++i].name)[0])
|
||||
const one = { id: opt.id }
|
||||
if (qty > 1) one.qty = qty
|
||||
if (subs.length) one.subs = subs
|
||||
options.push(one)
|
||||
} else if (o.type === 'quick' || !o.type) {
|
||||
const [name, qty] = splitQty(o.name)
|
||||
const q = (o.id != null && quickById.get(o.id)) || quickByName.get(name)
|
||||
if (!q) throw new LineError(product, name)
|
||||
quick.set(q.id, (quick.get(q.id) ?? 0) + qty)
|
||||
} else {
|
||||
throw new LineError(product, o.name)
|
||||
}
|
||||
}
|
||||
const ingredients = new Map((product.ingredients ?? []).map(g => [g.name, g.id]))
|
||||
const removed = removedNames.map(name => {
|
||||
if (!ingredients.has(name)) throw new LineError(product, name)
|
||||
return ingredients.get(name)
|
||||
})
|
||||
if (quick.size) line.quick = [...quick].map(([id, qty]) => (qty > 1 ? { id, qty } : { id }))
|
||||
if (options.length) line.options = options
|
||||
if (prefs.size) line.prefs = [...prefs.values()]
|
||||
if (removed.length) line.removed = removed
|
||||
return line
|
||||
}
|
||||
|
||||
const cents = n => Math.round(n * 100) / 100
|
||||
|
||||
/** A cart item as the line the server takes: what was chosen + quantity, note, course, a
|
||||
* price changed by hand (needs ordering.prices.modify or a manager's PIN), service item. */
|
||||
export function wireLine(item, product) {
|
||||
const line = lineFromOptions(product, item)
|
||||
line.quantity = item.quantity ?? 1
|
||||
if (item.notes?.trim()) line.notes = item.notes.trim()
|
||||
if (item.course_id != null) line.course_id = item.course_id
|
||||
if (item.price_adjustment) line.price_adjustment = cents(item.price_adjustment)
|
||||
if (item.is_service_item) line.is_service_item = true
|
||||
return line
|
||||
}
|
||||
|
||||
/** What a cart item will roughly cost from the menu the app has (base price, the options'
|
||||
* prices, a preference set's default that costs, a price changed by hand). The server's
|
||||
* price can differ (happy hours, price groups, quantity deals): use POST /api/orders/quote
|
||||
* when online and show this one as an estimate. */
|
||||
export function estimate(item, product) {
|
||||
if (!product) return 0
|
||||
const opts = item.selected_options ?? []
|
||||
let unit = product.base_price ?? 0
|
||||
for (const o of opts) unit += o.price_delta ?? 0
|
||||
const listed = new Set(opts.filter(o => o.type === 'pref').map(o => o.id))
|
||||
for (const ps of product.preference_sets ?? []) {
|
||||
if ((ps.choices ?? []).some(c => listed.has(c.id))) continue
|
||||
const def = (ps.choices ?? []).find(c => c.id === ps.default_choice_id)
|
||||
if (def) unit += def.extra_cost ?? 0
|
||||
}
|
||||
unit += item.price_adjustment ?? 0
|
||||
return cents(unit * (item.quantity ?? 1))
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { LineError, estimate, lineFromOptions, wireLine } from '../src/lines.js'
|
||||
|
||||
// the menu's burger as /api/products lists it (local_backend tests/test_order_lines.py `full`)
|
||||
const burger = {
|
||||
id: 1, name: 'Burger', base_price: 10,
|
||||
quick_options: [{ id: 11, name: 'Extra cheese', price: 1, allow_multiple: true }],
|
||||
options: [
|
||||
{ id: 21, name: 'Sauce', extra_cost: 0.5, sub_choices: [{ name: 'BBQ', extra_cost: 0.2, is_default: true }, { name: 'Mayo' }] },
|
||||
{ id: 22, name: 'Pickles', extra_cost: 0.4, allow_multiple: true, sub_choices: [{ name: 'Sweet', extra_cost: 0.1 }, { name: 'Sour' }] },
|
||||
],
|
||||
ingredients: [{ id: 31, name: 'Onion' }],
|
||||
preference_sets: [
|
||||
{ id: 41, name: 'Cooking', default_choice_id: null,
|
||||
shared_subset: { name: 'Side', choices: [{ name: 'Fries', is_default: true }, { name: 'Salad', extra_cost: 1 }] },
|
||||
choices: [{ id: 411, name: 'Rare' }, { id: 412, name: 'Well done', extra_cost: 0.5, disables_subset: true }] },
|
||||
{ id: 42, name: 'Bread', default_choice_id: 421,
|
||||
choices: [{ id: 421, name: 'Brioche', sub_choices: [{ name: 'Toasted', is_default: true }, { name: 'Soft' }] }, { id: 422, name: 'Rye', extra_cost: 0.3 }] },
|
||||
{ id: 43, name: 'Size', default_choice_id: 431, choices: [{ id: 431, name: 'Double', extra_cost: 1 }, { id: 432, name: 'Single' }] },
|
||||
],
|
||||
}
|
||||
|
||||
const e = (type, id, name, price_delta = 0) => ({ id, name, price_delta, type })
|
||||
|
||||
test('the waiter drawer\'s cart item becomes a line by id', () => {
|
||||
const item = {
|
||||
product_id: 1, quantity: 2, notes: ' no salt ', course_id: 3,
|
||||
selected_options: [
|
||||
e('pref', 411, 'Rare'), e('pref_sub', null, 'Salad', 1),
|
||||
e('pref', 421, 'Brioche'), e('pref_sub', null, 'Soft'),
|
||||
e('quick', null, 'Extra cheese', 1), e('quick', null, 'Extra cheese', 1),
|
||||
e('extra', 21, 'Sauce', 0.5), e('extra_sub', null, 'BBQ', 0.2),
|
||||
e('extra', 22, 'Pickles', 0.4), e('extra_sub', null, 'Sweet', 0.1),
|
||||
e('extra', 22, 'Pickles', 0.4), e('extra_sub', null, 'Sweet', 0.1),
|
||||
],
|
||||
removed_ingredients: ['Onion'],
|
||||
}
|
||||
assert.deepEqual(wireLine(item, burger), {
|
||||
product_id: 1, quantity: 2, notes: 'no salt', course_id: 3,
|
||||
quick: [{ id: 11, qty: 2 }],
|
||||
options: [{ id: 21, subs: ['BBQ'] }, { id: 22, subs: ['Sweet'] }, { id: 22, subs: ['Sweet'] }],
|
||||
prefs: [{ set_id: 41, choices: [{ id: 411 }], shared: 'Salad' }, { set_id: 42, choices: [{ id: 421, sub: 'Soft' }] }],
|
||||
removed: [31],
|
||||
})
|
||||
// the cheaper default isn't listed; the default that costs (Size: Double +1) counts in the estimate
|
||||
assert.equal(estimate(item, burger), Math.round((10 + 1 + 0 + 2 + 0.7 + 1 + 1) * 2 * 100) / 100)
|
||||
})
|
||||
|
||||
test('an item stored by the counter (quantities in the names) — order again', () => {
|
||||
const stored = {
|
||||
product_id: 1, quantity: 1,
|
||||
selected_options: [e('quick', 11, 'Extra cheese x2', 2), e('extra', 22, 'Pickles x2', 0.8), e('extra_sub', null, 'Sweet x2', 0.2),
|
||||
e('pref', 412, 'Well done', 0.5), e('pref', 431, 'Double', 1)],
|
||||
}
|
||||
assert.deepEqual(lineFromOptions(burger, stored), {
|
||||
product_id: 1, quick: [{ id: 11, qty: 2 }], options: [{ id: 22, qty: 2, subs: ['Sweet'] }],
|
||||
prefs: [{ set_id: 41, choices: [{ id: 412 }] }, { set_id: 43, choices: [{ id: 431 }] }],
|
||||
})
|
||||
})
|
||||
|
||||
test('a price changed by hand and a service item travel with the line; nothing else priced', () => {
|
||||
const line = wireLine({ product_id: 1, quantity: 1, price_adjustment: -1.004, is_service_item: true, unit_price: 0.01, selected_options: [] }, burger)
|
||||
assert.deepEqual(line, { product_id: 1, quantity: 1, price_adjustment: -1, is_service_item: true })
|
||||
})
|
||||
|
||||
test('what is not on the menu any more is refused, not dropped', () => {
|
||||
assert.throws(() => lineFromOptions(burger, { selected_options: [e('extra', 99, 'Truffle')] }), LineError)
|
||||
assert.throws(() => lineFromOptions(burger, { selected_options: [e('quick', null, 'Bacon')] }), LineError)
|
||||
assert.throws(() => lineFromOptions(burger, { removed_ingredients: ['Tomato'] }), err => err instanceof LineError && err.what === 'Tomato' && err.product === 'Burger')
|
||||
// a product missing from the menu: fine without options
|
||||
assert.deepEqual(wireLine({ product_id: 7, quantity: 3 }, undefined), { product_id: 7, quantity: 3 })
|
||||
})
|
||||
@@ -62,7 +62,16 @@
|
||||
},
|
||||
"order": {
|
||||
"not_found": "Η παραγγελία {{order_id}} δεν βρέθηκε.",
|
||||
"not_sold_here": "Το {{name}} δεν πωλείται για αυτό το είδος παραγγελίας."
|
||||
"not_sold_here": "Το {{name}} δεν πωλείται για αυτό το είδος παραγγελίας.",
|
||||
"lines_required": "Αυτή η εφαρμογή έστειλε την παραγγελία σε παλιά μορφή. Ενημερώστε την εφαρμογή και στείλτε ξανά.",
|
||||
"line": {
|
||||
"product_unavailable": "Το «{{name}}» δεν είναι πια διαθέσιμο (γραμμή {{line}}).",
|
||||
"option_gone": "«{{name}}»: μια επιλογή δεν υπάρχει πια (γραμμή {{line}}). Αφαιρέστε το και προσθέστε το ξανά.",
|
||||
"choice_missing": "«{{name}}»: λείπει μια υποχρεωτική επιλογή (γραμμή {{line}}).",
|
||||
"quantity": "«{{name}}»: αυτή η ποσότητα δεν επιτρέπεται (γραμμή {{line}}).",
|
||||
"price": "«{{name}}»: αυτή η αλλαγή τιμής δεν επιτρέπεται (γραμμή {{line}}).",
|
||||
"client_price": "«{{name}}»: η εφαρμογή έστειλε δικές της τιμές, ενώ οι τιμές έρχονται από τον server (γραμμή {{line}}). Ενημερώστε την εφαρμογή."
|
||||
}
|
||||
},
|
||||
"notification": {
|
||||
"not_found": "Η ειδοποίηση {{notification_id}} δεν βρέθηκε."
|
||||
|
||||
@@ -62,7 +62,16 @@
|
||||
},
|
||||
"order": {
|
||||
"not_found": "Order {{order_id}} not found.",
|
||||
"not_sold_here": "{{name}} isn't sold for this kind of order."
|
||||
"not_sold_here": "{{name}} isn't sold for this kind of order.",
|
||||
"lines_required": "This app sent the order in an old format. Update the app and send it again.",
|
||||
"line": {
|
||||
"product_unavailable": "{{name}} isn't available any more (line {{line}}).",
|
||||
"option_gone": "{{name}}: a chosen option doesn't exist any more (line {{line}}). Remove it and add it again.",
|
||||
"choice_missing": "{{name}}: a required choice is missing (line {{line}}).",
|
||||
"quantity": "{{name}}: that quantity isn't allowed (line {{line}}).",
|
||||
"price": "{{name}}: that price change isn't allowed (line {{line}}).",
|
||||
"client_price": "{{name}}: the app sent its own prices, but prices come from the server (line {{line}}). Update the app."
|
||||
}
|
||||
},
|
||||
"notification": {
|
||||
"not_found": "Notification {{notification_id}} not found."
|
||||
|
||||
Reference in New Issue
Block a user