- connect_frontend/order-app at /order/{site}: menu per service at that service's prices,
extras, basket (kept per tab), checkout priced by the cloud (fee, area by list or
postcode, minimum), name/phone/address/notes (remembered), as soon as possible or a time
slot, tracking page with steps, lines and totals; en/el; tokens in index.css. Logic in
src/logic.js, unit-tested; built and served by the connect image (npm test in the build).
- services/ordering.py: the venue's services, hours (past midnight), preparation, slot grid,
days ahead, ASAP; takeaway/delivery orders checked (order.service_unavailable,
order.closed, order.time_invalid) and need a phone; GET /api/orders/{site}/slots; the
public menu carries the ordering summary.
- services/ratelimit.py: per-caller limits on quote, order and track (429 rate.limited).
- online_orders.requested_for (0003, unreleased) stored as UTC; every datetime sent with its
offset (the database keeps no zone: a 12:00 Athens slot came back as 12:00 unzoned).
- Tracking returns what was ordered, the totals, the type and the time.
- Tests: test_ordering.py (5), checkout/fees updated; order-app unit (4).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
4.3 KiB
JavaScript
64 lines
4.3 KiB
JavaScript
// The online ordering app's logic (plan phase 3S, step S.4).
|
|
import assert from 'node:assert/strict'
|
|
import { test } from 'node:test'
|
|
import {
|
|
addToCart, cartCount, cartLines, checkoutProblems, menuFor, orderBody, priceFor, refusal, setQty, slotDays, trackSteps,
|
|
} from '../src/logic.js'
|
|
|
|
const cola = { id: 1, name: 'Cola', sold_at: ['qr_table', 'online_takeaway', 'online_delivery'], prices: { qr_table: 3.5, online_takeaway: 2.5, online_delivery: 2.8 },
|
|
quick_options: [{ id: 7, name: 'Ice', price: 0.2 }] }
|
|
const pie = { id: 2, name: 'Pie', digital_name: 'Spinach pie', sold_at: ['online_takeaway'], prices: { online_takeaway: 4 }, digital_available: false }
|
|
const old = { id: 3, name: 'Old', digital_price: 5 }
|
|
const cats = [{ id: 10, name: 'Drinks', products: [cola] }, { id: 11, name: 'Food', products: [pie, old] }, { id: 12, name: 'Also', products: [cola] }]
|
|
|
|
test('the menu and prices per service', () => {
|
|
assert.equal(priceFor(cola, 'takeaway'), 2.5)
|
|
assert.equal(priceFor(cola, 'delivery'), 2.8)
|
|
assert.equal(priceFor(pie, 'delivery'), null)
|
|
assert.equal(priceFor(old, 'delivery'), 5)
|
|
const m = menuFor(cats, 'delivery')
|
|
assert.deepEqual(m.map(c => [c.name, c.products.map(p => [p.id, p.price, p.repeat])]), [['Drinks', [[1, 2.8, false]]], ['Food', [[3, 5, false]]], ['Also', [[1, 2.8, true]]]])
|
|
assert.equal(menuFor(cats, 'takeaway')[1].products[0].available, false)
|
|
})
|
|
|
|
test('the cart', () => {
|
|
let cart = addToCart([], cola)
|
|
cart = addToCart(cart, cola)
|
|
cart = addToCart(cart, cola, [7])
|
|
assert.deepEqual(cart.map(l => [l.key, l.qty]), [['1', 2], ['1:7', 1]])
|
|
assert.equal(cartCount(cart), 3)
|
|
const lines = cartLines(cart, [cola, pie], 'takeaway')
|
|
assert.deepEqual(lines.map(l => [l.unit, l.total, l.optionNames]), [[2.5, 5, []], [2.7, 2.7, ['Ice']]])
|
|
assert.deepEqual(cartLines(addToCart(cart, pie), [cola, pie], 'delivery').length, 2) // the pie isn't delivered
|
|
assert.deepEqual(setQty(cart, '1', 0).map(l => l.key), ['1:7'])
|
|
assert.equal(setQty(cart, '1', 500)[0].qty, 99)
|
|
})
|
|
|
|
test('checkout checks and the order body', () => {
|
|
const form = { name: ' Maria ', phone: '690 000 0000', address: '', notes: '', where: 'area', areaId: '', postcode: '', when: 'asap' }
|
|
assert.deepEqual(checkoutProblems(form, 'takeaway'), [])
|
|
assert.deepEqual(checkoutProblems(form, 'delivery', { areas: [{ id: 1 }] }), ['address', 'area'])
|
|
assert.deepEqual(checkoutProblems({ ...form, name: '', phone: 'abc' }, 'takeaway'), ['name', 'phone'])
|
|
assert.deepEqual(checkoutProblems(form, 'takeaway', { asap: false }), ['when'])
|
|
assert.deepEqual(checkoutProblems({ ...form, when: 'X' }, 'takeaway', { slots: ['X'] }), [])
|
|
const lines = cartLines(addToCart([], cola), [cola], 'delivery')
|
|
const body = orderBody({ ...form, address: 'Main sq 3', areaId: '2', notes: ' bell ' }, 'delivery', lines, { total: 4.8 })
|
|
assert.deepEqual(body, {
|
|
order_type: 'delivery', customer_name: 'Maria', customer_phone: '690 000 0000', customer_address: 'Main sq 3', customer_notes: 'bell',
|
|
area_id: 2, postcode: null, requested_for: null, items: [{ product_id: 1, quantity: 1, options: [], unit_price: 2.8 }], total: 4.8,
|
|
})
|
|
assert.equal(orderBody({ ...form, when: '2026-11-16T10:00:00+00:00' }, 'takeaway', lines, null).requested_for, '2026-11-16T10:00:00+00:00')
|
|
})
|
|
|
|
test('slots by day in the venue time zone, tracking steps, refusals', () => {
|
|
const days = slotDays(['2026-11-15T19:45:00+00:00', '2026-11-15T22:00:00+00:00'], 'Europe/Athens')
|
|
assert.deepEqual(days, [{ day: '2026-11-15', times: [{ iso: '2026-11-15T19:45:00+00:00', label: '21:45' }] },
|
|
{ day: '2026-11-16', times: [{ iso: '2026-11-15T22:00:00+00:00', label: '00:00' }] }])
|
|
assert.deepEqual(trackSteps('delivery', 'preparing'), { steps: ['pending_acceptance', 'accepted', 'preparing', 'out_for_delivery', 'delivered'], at: 2, done: false })
|
|
assert.equal(trackSteps('takeaway', 'ready').at, 3)
|
|
assert.deepEqual(trackSteps('takeaway', 'rejected'), { ...trackSteps('takeaway', 'rejected'), at: -1, done: true })
|
|
assert.deepEqual(refusal({ response: { status: 409, data: { detail: { code: 'order.minimum_not_met', params: { minimum: 10 } } } } }), ['minimum_not_met', { minimum: 10 }])
|
|
assert.deepEqual(refusal({ response: { status: 429, data: {} } }), ['rate', {}])
|
|
assert.deepEqual(refusal(new Error('x')), ['failed', {}])
|
|
})
|