Plan phase 3S, step S.6.
- New order types "reservation" (a table only, no items) and "preorder" (a table with food,
priced at the online_preorder sales point); online_orders.party_size (migration 0005).
- services/ordering.py: the venue's booking settings (ordering.reservations in the snapshot):
hours (none = any time), time grid, notice, days ahead (≤ 90), party size; res_slots per
venue day, res_days, res_check (order.service_unavailable, order.party_invalid,
order.time_invalid). The public menu's ordering summary gains `reservations`
{enabled, with_food, party_max, days}; GET /api/orders/{site}/reservation-slots?day=.
- Bookings need a phone; a plain booking refuses items (order.items_not_allowed).
- Status steps for bookings: pending_acceptance → accepted | rejected; accepted → delivered
(arrived) | cancelled (by the venue); ordinary orders unchanged.
- Order app: "Book a table" (people, day, time, optional food at the pre-order prices),
booking summary at checkout, a booking tracking page (waiting, confirmed, welcome;
declined / cancelled with the reason). en/el.
- Tests: tests/test_reservations.py (3), order-app logic test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
5.6 KiB
JavaScript
84 lines
5.6 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', {}])
|
|
})
|
|
|
|
test('booking a table, with or without food (S.6)', async () => {
|
|
const { bookingBody, bookingProblems } = await import('../src/logic.js')
|
|
const form = { name: 'Eleni', phone: '6977777777', notes: '' }
|
|
const booking = { party: 4, when: '2026-11-16T18:00:00+00:00' }
|
|
assert.deepEqual(bookingProblems(form, booking, 8), [])
|
|
assert.deepEqual(bookingProblems({ ...form, name: ' ' }, { party: 9, when: null }, 8), ['name', 'party', 'when'])
|
|
assert.deepEqual(bookingBody(form, booking, [], null), {
|
|
order_type: 'reservation', customer_name: 'Eleni', customer_phone: '6977777777', customer_notes: null,
|
|
party_size: 4, requested_for: booking.when, items: [], total: undefined,
|
|
})
|
|
const food = bookingBody(form, booking, [{ productId: 1, qty: 2, options: [], unit: 2.6 }], { total: 5.2 })
|
|
assert.equal(food.order_type, 'preorder')
|
|
assert.deepEqual(food.items, [{ product_id: 1, quantity: 2, options: [], unit_price: 2.6 }])
|
|
assert.equal(food.total, 5.2)
|
|
assert.deepEqual(trackSteps('reservation', 'accepted'), { steps: ['pending_acceptance', 'accepted', 'delivered'], at: 1, done: false })
|
|
assert.equal(trackSteps('preorder', 'cancelled').done, true)
|
|
assert.equal(trackSteps('preorder', 'cancelled').at, -1)
|
|
assert.equal(priceFor({ sold_at: ['online_preorder'], prices: { online_preorder: 2.6 } }, 'preorder'), 2.6)
|
|
})
|