feat: delivery areas drawn on a map: the customer pins the address (plan 6R.G)

- services/fees.py: the site's engine (shapes matched by the pin), shared cases
- checkout: price_cart(pin=), delivery_areas with kind (+ geometry for shapes),
  venue_point in the public menu (postcodes and rules stay on the cloud)
- orders: quote / create take lat / lng; online_orders.lat / lng (Alembic 0007),
  sent to the site with the pending orders
- order app: PinMap (Leaflet + OpenStreetMap tiles), 'Pin it on the map' first
  when the venue drew areas, the browser's location offered; lat / lng sent
- tests: cloud test_fees (pinned quote and order), order-app logic

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 21:56:13 +02:00
co-authored by Claude Opus 5.5
parent e55157fa86
commit d5b8492512
17 changed files with 406 additions and 41 deletions
+41 -2
View File
@@ -3,7 +3,10 @@
"areas": [
{"id": 1, "name": "Village centre", "postcodes": ["241 00"], "is_active": true},
{"id": 2, "name": "Kalamata", "postcodes": ["24100", "24131"], "is_active": true},
{"id": 3, "name": "Old area", "postcodes": ["99999"], "is_active": false}
{"id": 3, "name": "Old area", "postcodes": ["99999"], "is_active": false},
{"id": 4, "name": "Drawn square", "kind": "polygon", "postcodes": [], "is_active": true, "geometry": [[37.0, 22.0], [37.0, 22.1], [37.1, 22.1], [37.1, 22.0]]},
{"id": 5, "name": "1 km around", "kind": "circle", "postcodes": [], "is_active": true, "geometry": {"center": [37.5, 22.5], "radius_m": 1000}},
{"id": 6, "name": "Old shape", "kind": "polygon", "postcodes": [], "is_active": false, "geometry": [[38.0, 23.0], [38.0, 23.1], [38.1, 23.1]]}
],
"cases": [
{"name": "no rules: free",
@@ -103,6 +106,42 @@
{"name": "a takeaway packaging fee (areas only matter for delivery)",
"rules": [{"id": 1, "name": "Packaging", "sales_points": ["online_takeaway"], "action": "set", "value": 0.3}],
"ctx": {"sales_point": "online_takeaway", "subtotal": 9, "items": 3, "postcode": "nowhere"},
"expect": {"fee": 0.3, "refused": null, "area_id": null}}
"expect": {"fee": 0.3, "refused": null, "area_id": null}},
{"name": "6R.G: a pin inside a drawn shape",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.05, 22.05]},
"expect": {"refused": null, "area_id": 4}},
{"name": "6R.G: a pin on the shape's edge counts as inside",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.0, 22.05]},
"expect": {"refused": null, "area_id": 4}},
{"name": "6R.G: a pin on a corner counts as inside",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.1, 22.1]},
"expect": {"refused": null, "area_id": 4}},
{"name": "6R.G: a pin outside every area is refused",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.2, 22.05]},
"expect": {"refused": "area_not_served", "area_id": null}},
{"name": "6R.G: inside the circle (990 m)",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.5089, 22.5]},
"expect": {"refused": null, "area_id": 5}},
{"name": "6R.G: past the circle's rim (1.1 km)",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.5099, 22.5]},
"expect": {"refused": "area_not_served"}},
{"name": "6R.G: a shape picked by id needs a pin inside it",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "area_id": 4},
"expect": {"refused": "area_not_served"}},
{"name": "6R.G: a shape picked by id with its pin",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "area_id": 4, "point": [37.05, 22.05]},
"expect": {"area_id": 4}},
{"name": "6R.G: a shape picked with a pin outside it falls back to the postcode",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "area_id": 4, "point": [37.2, 22.0], "postcode": "24131"},
"expect": {"area_id": 2}},
{"name": "6R.G: an inactive shape holds nothing",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [38.02, 23.05]},
"expect": {"refused": "area_not_served"}},
{"name": "6R.G: a nonsense pin is no pin",
"rules": [], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": ["x", 200], "postcode": "241 00"},
"expect": {"area_id": 1}},
{"name": "6R.G: rules by area work for shapes",
"rules": [{"id": 1, "name": "Square fee", "area_ids": [4], "action": "set", "value": 2.5, "sales_points": ["online_delivery"]}, {"id": 2, "name": "Circle fee", "area_ids": [5], "action": "set", "value": 1, "sales_points": ["online_delivery"]}], "ctx": {"sales_point": "online_delivery", "subtotal": 20, "items": 2, "point": [37.05, 22.05]},
"expect": {"fee": 2.5, "area_id": 4}}
]
}
+38 -1
View File
@@ -75,7 +75,7 @@ def test_minimum_and_area_are_refused(client, admin_h):
def test_the_public_menu_lists_areas_but_not_the_rules(client, admin_h):
site, _ = _venue_with_fees(client, admin_h)
menu = client.get(f"/api/menu/{site['site_id']}").json()
assert menu["delivery_areas"] == [{"id": 1, "name": "Centre"}, {"id": 2, "name": "Far"}]
assert menu["delivery_areas"] == [{"id": 1, "name": "Centre", "kind": "postcodes"}, {"id": 2, "name": "Far", "kind": "postcodes"}]
assert "fees" not in menu
@@ -84,3 +84,40 @@ def test_the_venue_clock():
at = datetime(2026, 11, 15, 21, 30, tzinfo=timezone.utc) # Sunday 23:30 in Athens (UTC+2)
assert venue_clock({"timezone": "Europe/Athens"}, at) == (6, "23:30")
assert venue_clock({"timezone": "Nowhere/Bad"}, at) == (6, "23:30")
SHAPED = {"areas": [{"id": 1, "name": "Centre", "postcodes": ["24100"], "is_active": True},
{"id": 7, "name": "Drawn", "kind": "polygon", "postcodes": [], "is_active": True,
"geometry": [[37.0, 22.0], [37.0, 22.1], [37.1, 22.1], [37.1, 22.0]]},
{"id": 8, "name": "2 km", "kind": "circle", "postcodes": [], "is_active": True,
"geometry": {"center": [37.5, 22.5], "radius_m": 2000}}],
"rules": [{"id": 1, "name": "Drawn fee", "area_ids": [7], "action": "set", "value": 1.5, "sales_points": ["online_delivery"]},
{"id": 2, "name": "Circle fee", "area_ids": [8], "action": "set", "value": 3, "sales_points": ["online_delivery"]}],
"venue_point": {"lat": 37.05, "lng": 22.05}}
def test_a_pin_on_the_map_finds_a_drawn_area_and_reaches_the_site(client, admin_h):
"""6R.G: the customer pins the address; the cloud prices it by the area holding the pin and
keeps the pin for the site (which checks the fee with the same engine)."""
site = _new_site(client, admin_h)
numeric = _heartbeat(client, site)["site_numeric_id"]
_menu(client, site, numeric)
menu = client.get(f"/api/menu/{site['site_id']}").json()
snap = {"categories": menu["categories"], "fees": SHAPED, "ordering": ORDERING, "timezone": "Europe/Athens"}
assert client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(snap)}).status_code == 200
menu = client.get(f"/api/menu/{site['site_id']}").json()
assert menu["venue_point"] == {"lat": 37.05, "lng": 22.05}
assert menu["delivery_areas"][1] == {"id": 7, "name": "Drawn", "kind": "polygon", "geometry": SHAPED["areas"][1]["geometry"]}
assert "24100" not in json.dumps(menu["delivery_areas"]) # the postcodes stay on the cloud
items = [{"product_id": COLA, "quantity": 2}]
q = _quote(client, site, items=items, lat=37.05, lng=22.05).json()
assert (q["delivery_fee"], q["area_name"]) == (1.5, "Drawn")
q = _quote(client, site, items=items, lat=37.51, lng=22.5).json()
assert (q["delivery_fee"], q["area_name"]) == (3.0, "2 km")
assert _code(_quote(client, site, items=items, lat=36.0, lng=22.5)) == "order.area_not_served"
assert _code(_quote(client, site, items=items, area_id=7)) == "order.area_not_served" # a shape needs the pin
assert _quote(client, site, items=items, lat=37.05).json()["detail"]["code"] == "order.area_not_served" # half a pin is none
ok = _order(client, site, "delivery", customer_address="Main sq. 3", lat=37.05, lng=22.05, total=round(5.6 + 1.5, 2))
assert ok.status_code == 201, ok.text
stored = next(o for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json() if o["public_ref"] == ok.json()["public_ref"])
assert (stored["delivery_area"], stored["delivery_area_id"], stored["lat"], stored["lng"]) == ("Drawn", 7, 37.05, 22.05)