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))
|
||||
}
|
||||
Reference in New Issue
Block a user