feat: online ordering settings (services, hours, preparation, slots) and the order app e2e (platform-v2 3S, S.4)

- connect_orders.ordering setting; GET/PUT /api/connect/fees/ordering (validated:
  services, hours per day incl. past midnight, preparation, slot minutes, days ahead,
  ASAP); pushed to the cloud with the menu (snapshot `ordering`).
- Manager: the Online ordering page gains "Services and hours" (OrderingSettings.jsx; logic
  in feesLogic.js, unit-tested); nav label "Online ordering".
- tests/e2e/order_app.mjs: a real cloud + the built order app in a phone-sized browser:
  takeaway and delivery placed, priced by the cloud (fee by area, minimum refused), the
  site moves the delivery to delivered and the page follows; a rejection with its reason;
  Greek. In run-all, with the order app's unit tests.
- Tests: backend +1 (406), manager unit +1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 13:39:13 +02:00
co-authored by Claude Opus 5.5
parent df27133af9
commit 8ea44d71aa
12 changed files with 507 additions and 11 deletions
@@ -1,4 +1,5 @@
// Delivery areas and fees (plan phase 3S, step S.3). Backend: /api/connect/fees
// Online ordering (plan phase 3S, steps S.3–S.4): the ordering settings (OrderingSettings),
// delivery areas and fees. Backend: /api/connect/fees
// (connect_orders.fees). The venue lists the areas it delivers to and the fee rules, looked
// at from the top: a rule whose conditions hold changes the fee (set, add, % of the
// products, free) or refuses the order below a minimum; "stop here" makes the first match
@@ -12,6 +13,7 @@ import { useFormat, useTranslation } from '@xenia/i18n'
import { SALES_POINTS } from '@xenia/access'
import { Button, Dialog, EmptyState, Field, IconButton, Input, Select, Spinner, Switch } from '@xenia/ui'
import client from '../../api/client'
import OrderingSettings from './OrderingSettings'
import { ACTIONS, DAYS, describeRule, emptyRule, moveRule, parsePostcodes, previewBody, ruleBody, ruleProblems, ruleToForm } from './feesLogic'
const chip = on => `px-3 min-h-[var(--x-control-sm)] rounded-full text-label font-medium transition-colors duration-fast
@@ -51,10 +53,16 @@ export default function FeesPage() {
return (
<div className="p-4 md:p-6 grid gap-6 max-w-6xl" data-testid="fees-page">
<header className="grid gap-1">
<h1 className="text-heading font-semibold text-ink">{t('fees.title')}</h1>
<p className="text-body text-ink-2 max-w-3xl">{t('fees.intro')}</p>
<h1 className="text-heading font-semibold text-ink">{t('ordering.pageTitle')}</h1>
</header>
<OrderingSettings key={JSON.stringify(data.ordering)} ordering={data.ordering} />
<div className="grid gap-1">
<h2 className="text-title font-semibold text-ink">{t('fees.title')}</h2>
<p className="text-body text-ink-2 max-w-3xl">{t('fees.intro')}</p>
</div>
<div className="grid gap-6 lg:grid-cols-[1fr_22rem] items-start">
<section className="grid gap-3" aria-labelledby="fee-rules">
<div className="flex items-center gap-2">
@@ -0,0 +1,98 @@
// Online ordering settings (plan phase 3S, step S.4): which services the online ordering app
// offers (takeaway, delivery), when (ordering hours per day, or any time), how long before
// an order can be ready (preparation), the times offered (every N minutes, days ahead) and
// "as soon as possible". The cloud enforces them at checkout. PUT /api/connect/fees/ordering.
import { useState } from 'react'
import toast from 'react-hot-toast'
import { Plus, X } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, Field, IconButton, Input, Select, Switch } from '@xenia/ui'
import client from '../../api/client'
import { DAYS, orderingBody, orderingToForm } from './feesLogic'
export default function OrderingSettings({ ordering, onSaved }) {
const { t } = useTranslation('manager')
const [form, setForm] = useState(() => orderingToForm(ordering))
const [busy, setBusy] = useState(false)
const set = (k, v) => setForm(f => ({ ...f, [k]: v }))
const setDay = (d, ranges) => setForm(f => ({ ...f, days: f.days.map((r, i) => (i === d ? ranges : r)) }))
const toggleService = s => set('services', form.services.includes(s) ? form.services.filter(x => x !== s) : ['takeaway', 'delivery'].filter(x => x === s || form.services.includes(x)))
const save = async () => {
setBusy(true)
try {
const { data } = await client.put('/api/connect/fees/ordering', orderingBody(form))
setForm(orderingToForm(data))
toast.success(t('fees.saved'))
onSaved?.()
} catch (err) { toast.error(errorMessage(err, t)) }
setBusy(false)
}
return (
<section className="p-4 rounded-card border border-line bg-surface grid gap-4" aria-labelledby="ordering-h" data-testid="ordering-settings">
<div className="grid gap-1">
<h2 id="ordering-h" className="text-title font-semibold text-ink">{t('ordering.title')}</h2>
<p className="text-label text-ink-2">{t('ordering.intro')}</p>
</div>
<div className="grid gap-2 sm:grid-cols-2">
{['takeaway', 'delivery'].map(s => (
<Switch key={s} checked={form.services.includes(s)} onChange={() => toggleService(s)}
label={t(`ordering.service.${s}`)} description={t(`ordering.serviceHint.${s}`)} data-testid={`ordering-${s}`} />
))}
</div>
<div className="grid gap-2">
<Switch checked={form.anyTime} onChange={v => set('anyTime', v)} label={t('ordering.anyTime')} description={t('ordering.anyTimeHint')} />
{!form.anyTime && (
<ul className="grid gap-1.5" data-testid="ordering-hours">
{DAYS.map(d => (
<li key={d} className="grid grid-cols-[6rem_1fr] gap-2 items-start">
<span className="text-body text-ink pt-2">{t(`fees.dayLong.${d}`)}</span>
<div className="grid gap-1.5">
{form.days[d].length === 0 && <span className="text-label text-ink-3 pt-2">{t('ordering.closedDay')}</span>}
{form.days[d].map((r, i) => (
<div key={i} className="flex items-center gap-2">
<Input type="time" size="sm" value={r.from} aria-label={t('fees.timeFrom')}
onChange={e => setDay(d, form.days[d].map((x, j) => (j === i ? { ...x, from: e.target.value } : x)))} />
<span className="text-ink-3">–</span>
<Input type="time" size="sm" value={r.to} aria-label={t('fees.timeTo')}
onChange={e => setDay(d, form.days[d].map((x, j) => (j === i ? { ...x, to: e.target.value } : x)))} />
<IconButton size="sm" label={t('fees.delete')} onClick={() => setDay(d, form.days[d].filter((_, j) => j !== i))}><X size={16} /></IconButton>
</div>
))}
{form.days[d].length < 4 && (
<button type="button" className="justify-self-start text-label font-medium text-accent-ink flex items-center gap-1"
onClick={() => setDay(d, [...form.days[d], { from: '12:00', to: '23:00' }])}>
<Plus size={14} aria-hidden="true" />{t('ordering.addHours')}
</button>
)}
</div>
</li>
))}
</ul>
)}
</div>
<div className="grid gap-3 sm:grid-cols-3">
<Field label={t('ordering.prep')} hint={t('ordering.prepHint')}>
<Input inputMode="numeric" value={form.prep_minutes} onChange={e => set('prep_minutes', e.target.value)} />
</Field>
<Field label={t('ordering.slot')} hint={t('ordering.slotHint')}>
<Select value={form.slot_minutes} onChange={e => set('slot_minutes', e.target.value)}>
{['10', '15', '20', '30', '60'].map(m => <option key={m} value={m}>{t('ordering.minutes', { n: m })}</option>)}
</Select>
</Field>
<Field label={t('ordering.ahead')}>
<Select value={form.days_ahead} onChange={e => set('days_ahead', e.target.value)}>
{['0', '1', '2', '3', '7'].map(n => <option key={n} value={n}>{t(`ordering.aheadN.${n}`)}</option>)}
</Select>
</Field>
</div>
<Switch checked={form.asap} onChange={v => set('asap', v)} label={t('ordering.asap')} description={t('ordering.asapHint')} />
<div><Button onClick={save} loading={busy} data-testid="ordering-save">{t('fees.save')}</Button></div>
</section>
)
}
@@ -102,3 +102,30 @@ export function previewBody(form) {
time: form.day === 'now' ? null : (form.time || null),
}
}
/** Online ordering settings (S.4) → the form: hours as editable rows per day. */
export function orderingToForm(o) {
const hours = o?.hours ?? {}
return {
services: o?.services ?? [], anyTime: Object.keys(hours).length === 0,
days: DAYS.map(d => (hours[String(d)] ?? []).map(([from, to]) => ({ from, to }))),
prep_minutes: String(o?.prep_minutes ?? 20), slot_minutes: String(o?.slot_minutes ?? 15),
days_ahead: String(o?.days_ahead ?? 0), asap: o?.asap ?? true,
}
}
/** The form → PUT /api/connect/fees/ordering. A day without ranges is closed (unless any time). */
export function orderingBody(form) {
const hours = {}
if (!form.anyTime) {
form.days.forEach((ranges, d) => {
const clean = ranges.filter(r => r.from && r.to && r.from !== r.to).map(r => [r.from, r.to])
if (clean.length) hours[String(d)] = clean
})
}
const int = (v, dflt) => (Number.isFinite(Number(v)) && String(v).trim() !== '' ? Math.round(Number(v)) : dflt)
return {
services: form.services, hours, prep_minutes: int(form.prep_minutes, 20), slot_minutes: int(form.slot_minutes, 15),
days_ahead: int(form.days_ahead, 0), asap: !!form.asap,
}
}