feat: delivery areas and fee rules with "test an order" (platform-v2 3S, S.3)
- models/fees.py + Alembic 0018: delivery_areas, fee_rules; key connect_orders.fees (owner, store manager; existing ones get it). - services/fees.py: the pure engine (set / add / % / free / minimum, conditions: sales points, areas, products total, item count, days, time incl. past midnight, stop), the same file as the cloud's; tests/fixtures/fee_cases.json shared, plus a test that the cloud copy is identical. - routers/fees.py at /api/connect/fees: areas and rules CRUD (validated), order, preview. - Snapshot carries fees + the venue's time zone; a change to products, pricing or fees pushes the menu within ~5 s (middleware -> cloud_sync.request_menu_push). - Manager: Online -> Delivery fees page (@xenia/ui): rules in order with words for each, areas with postcodes, "Test an order"; en/el texts; feesLogic.js unit-tested. - Tests: test_fees.py (39), manager unit +5, manager.mjs +7 on the real page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// Delivery areas and fee rules, the manager's side (plan phase 3S, step S.3). The rules are
|
||||
// evaluated by the backend's engine (local_backend/services/fees.py, the same file the cloud
|
||||
// runs); this file only turns forms into request bodies and rules into readable lines.
|
||||
// Pure: unit-tested in Node (tests/fees.test.mjs).
|
||||
|
||||
export const ACTIONS = ['set', 'add', 'percent', 'free', 'minimum']
|
||||
export const DAYS = [0, 1, 2, 3, 4, 5, 6] // Monday = 0
|
||||
|
||||
const num = v => (v === '' || v == null ? null : Number(String(v).replace(',', '.')))
|
||||
|
||||
/** Postcodes typed with commas, semicolons or new lines → a tidy list without duplicates. */
|
||||
export function parsePostcodes(text) {
|
||||
const out = []
|
||||
for (const raw of String(text ?? '').split(/[,;\n]/)) {
|
||||
const code = raw.trim().replace(/\s+/g, ' ')
|
||||
if (code && !out.includes(code)) out.push(code)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** An empty rule form (a delivery fee by default). */
|
||||
export function emptyRule() {
|
||||
return {
|
||||
name: '', is_active: true, sales_points: ['online_delivery'], area_ids: [],
|
||||
subtotal_min: '', subtotal_max: '', items_min: '', items_max: '', days: [],
|
||||
time_from: '', time_to: '', action: 'set', value: '', stop: false,
|
||||
}
|
||||
}
|
||||
|
||||
/** A saved rule → the form (numbers as text, nulls as empty). */
|
||||
export function ruleToForm(rule) {
|
||||
const f = emptyRule()
|
||||
for (const k of Object.keys(f)) {
|
||||
const v = rule[k]
|
||||
f[k] = v == null ? f[k] : (typeof f[k] === 'string' && typeof v === 'number' ? String(v) : v)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
/** The form → the request body (POST/PUT /api/connect/fees/rules). */
|
||||
export function ruleBody(form) {
|
||||
const int = v => (num(v) == null ? null : Math.round(num(v)))
|
||||
return {
|
||||
name: form.name.trim(), is_active: !!form.is_active, sales_points: form.sales_points, area_ids: form.area_ids,
|
||||
subtotal_min: num(form.subtotal_min), subtotal_max: num(form.subtotal_max),
|
||||
items_min: int(form.items_min), items_max: int(form.items_max), days: [...form.days].sort((a, b) => a - b),
|
||||
time_from: form.time_from || null, time_to: form.time_to || null,
|
||||
action: form.action, value: form.action === 'free' ? 0 : (num(form.value) ?? 0), stop: !!form.stop,
|
||||
}
|
||||
}
|
||||
|
||||
/** What the form still needs before it can be saved: a list of message keys (empty = ok). */
|
||||
export function ruleProblems(form) {
|
||||
const out = []
|
||||
if (!form.name.trim()) out.push('name')
|
||||
const v = num(form.value)
|
||||
if (form.action !== 'free' && (v == null || Number.isNaN(v))) out.push('value')
|
||||
if (form.action !== 'add' && v != null && v < 0) out.push('negative')
|
||||
if (form.action === 'percent' && v > 100) out.push('percent')
|
||||
const lo = num(form.subtotal_min); const hi = num(form.subtotal_max)
|
||||
if (lo != null && hi != null && lo >= hi) out.push('subtotalRange')
|
||||
const il = num(form.items_min); const ih = num(form.items_max)
|
||||
if (il != null && ih != null && il > ih) out.push('itemsRange')
|
||||
if (!!form.time_from !== !!form.time_to) out.push('times')
|
||||
if (!form.sales_points.length) out.push('salesPoints')
|
||||
return out
|
||||
}
|
||||
|
||||
/** One rule as readable parts: { when: [[key, params]], action: [key, params] }, for t(). */
|
||||
export function describeRule(rule, areaNames = {}) {
|
||||
const when = []
|
||||
if (rule.area_ids?.length) when.push(['areas', { names: rule.area_ids.map(id => areaNames[id] ?? `#${id}`).join(', ') }])
|
||||
if (rule.subtotal_min != null && rule.subtotal_max != null) when.push(['subtotalBetween', { min: rule.subtotal_min, max: rule.subtotal_max }])
|
||||
else if (rule.subtotal_min != null) when.push(['subtotalFrom', { min: rule.subtotal_min }])
|
||||
else if (rule.subtotal_max != null) when.push(['subtotalBelow', { max: rule.subtotal_max }])
|
||||
if (rule.items_min != null && rule.items_max != null) when.push(['itemsBetween', { min: rule.items_min, max: rule.items_max }])
|
||||
else if (rule.items_min != null) when.push(['itemsFrom', { min: rule.items_min }])
|
||||
else if (rule.items_max != null) when.push(['itemsUpTo', { max: rule.items_max }])
|
||||
if (rule.days?.length && rule.days.length < 7) when.push(['days', { days: rule.days }])
|
||||
if (rule.time_from && rule.time_to) when.push(['time', { from: rule.time_from, to: rule.time_to }])
|
||||
return { when, action: [rule.action, { value: rule.value }] }
|
||||
}
|
||||
|
||||
/** The rules in a new order after moving one up (-1) or down (+1): their ids. */
|
||||
export function moveRule(rules, id, step) {
|
||||
const ids = rules.map(r => r.id)
|
||||
const i = ids.indexOf(id)
|
||||
const j = i + step
|
||||
if (i < 0 || j < 0 || j >= ids.length) return ids
|
||||
;[ids[i], ids[j]] = [ids[j], ids[i]]
|
||||
return ids
|
||||
}
|
||||
|
||||
/** "Test an order" form → the preview body. */
|
||||
export function previewBody(form) {
|
||||
return {
|
||||
sales_point: form.sales_point,
|
||||
area_id: form.where === 'area' && form.area_id !== '' ? Number(form.area_id) : null,
|
||||
postcode: form.where === 'postcode' ? form.postcode.trim() || null : null,
|
||||
subtotal: num(form.subtotal) ?? 0, items: Math.max(0, Math.round(num(form.items) ?? 0)),
|
||||
weekday: form.day === 'now' ? null : Number(form.day),
|
||||
time: form.day === 'now' ? null : (form.time || null),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user