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:
2026-11-15 12:28:29 +02:00
co-authored by Claude Opus 5.5
parent ffeb2b45cb
commit 9bfe54554d
24 changed files with 1723 additions and 9 deletions
+38 -3
View File
@@ -292,7 +292,11 @@ def menu_snapshot(db) -> tuple[dict, list]:
if product_list:
payload_categories.append({"id": cat.id, "name": cat.name, "sort_order": cat.sort_order,
"products": product_list})
return {"categories": payload_categories}, all_products
# delivery areas and fee rules (S.3): the cloud applies them at checkout with the same
# engine (services/fees.py); the venue's time zone says what "22:00" and "Saturday" mean
from core.timeutil import venue_tz
from routers.fees import fees_payload
return {"categories": payload_categories, "fees": fees_payload(db), "timezone": venue_tz().key}, all_products
async def _push_menu_snapshot():
@@ -307,7 +311,7 @@ async def _push_menu_snapshot():
try:
snapshot, all_products = menu_snapshot(db)
payload_categories = snapshot["categories"]
snapshot_json = json.dumps({"categories": payload_categories})
snapshot_json = json.dumps(snapshot)
# Resolve numeric site_id from license state (set by heartbeat response)
site_numeric_id = license_state.get("site_numeric_id")
@@ -576,11 +580,42 @@ async def _wait_for_site_id(max_seconds: int = 30) -> None:
await asyncio.sleep(1)
# A change to what the customer apps sell (products, price rules, fees) pushes the menu soon,
# not at the next 5-minute tick (phase 3S): request_menu_push() from any thread wakes the
# worker; edits in a row within MENU_SETTLE_SECONDS become one push.
MENU_SETTLE_SECONDS = 5
MENU_PATHS = ("/api/products", "/api/pricing", "/api/connect/fees", "/api/data-transfer/import")
_menu_wake: "asyncio.Event | None" = None
_menu_loop = None
def request_menu_push() -> None:
"""Ask the menu worker to push soon. Thread-safe; nothing happens while it isn't running."""
loop, wake = _menu_loop, _menu_wake
if loop is not None and wake is not None:
try:
loop.call_soon_threadsafe(wake.set)
except RuntimeError: # the loop has closed
pass
def menu_changed(method: str, path: str, status_code: int) -> bool:
"""Whether a finished request changed what the customer apps sell."""
return method not in ("GET", "HEAD", "OPTIONS") and status_code < 400 and path.startswith(MENU_PATHS)
async def menu_push_worker():
global _menu_wake, _menu_loop
_menu_loop, _menu_wake = asyncio.get_running_loop(), asyncio.Event()
await _wait_for_site_id()
while True:
_menu_wake.clear()
await _push_menu_snapshot()
await asyncio.sleep(PUSH_INTERVAL_SECONDS)
try:
await asyncio.wait_for(_menu_wake.wait(), PUSH_INTERVAL_SECONDS)
await asyncio.sleep(MENU_SETTLE_SECONDS)
except asyncio.TimeoutError:
pass
async def stats_push_worker():