feat: delivery areas drawn on a map (plan 6R.G)

An area is a list of postcodes, a shape drawn on a map or a circle around a
point. The fee engine (both copies, byte-identical) matches a shape only by
the customer's pin, then postcodes; point-in-polygon (edges inside) and
haversine circles; shared cases in fee_cases.json.

- Alembic 0053: delivery_areas.kind / geometry, orders.online_lat / lng
- /api/connect/fees: areas with kind + geometry (validated: 3-200 corners,
  50 m - 50 km), the venue's place (PUT /venue-point, setting venue.location,
  sent to the cloud as fees.venue_point), preview by lat / lng
- manager: DeliveryMap (Leaflet + OpenStreetMap tiles): draw / move / remove
  corners, circles, the venue on the map, an overview, test an order by a point
- pulled online orders keep the pin; the site re-checks the fee with it;
  the counter links the pin to a map
- tests: backend (shapes, venue point, pinned orders), manager logic,
  e2e manager.mjs and order_app.mjs 6R.G blocks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 21:55:36 +02:00
co-authored by Claude Opus 5.5
parent 0840247768
commit 6f42e00684
25 changed files with 844 additions and 40 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}}
]
}
+53
View File
@@ -151,3 +151,56 @@ def test_online_ordering_settings(client, db, owner_h):
for bad in ({"services": ["teleport"]}, {"hours": {"7": [["10:00", "11:00"]]}}, {"hours": {"1": [["10:00", "10:00"]]}},
{"hours": {"1": [["25:00", "11:00"]]}}, {"slot_minutes": 1}, {"days_ahead": 30}, {"prep_minutes": -5}):
assert client.put("/api/connect/fees/ordering", headers=owner_h, json=bad).status_code == 422, bad
# ── 6R.G: areas drawn on a map ────────────────────────────────────────────────
SQUARE = [[37.0, 22.0], [37.0, 22.1], [37.1, 22.1], [37.1, 22.0]]
def test_areas_drawn_on_the_map_and_tested_by_a_point(client, owner_h):
h = owner_h
centre = _area(client, h, "Village centre", ["24100"])
drawn = _area(client, h, "Drawn", kind="polygon", geometry=SQUARE, postcodes=["ignored"])
ring = _area(client, h, "2 km", kind="circle", geometry={"center": [37.5, 22.5], "radius_m": 2000.4})
assert (drawn["kind"], drawn["geometry"], drawn["postcodes"]) == ("polygon", SQUARE, [])
assert ring["geometry"] == {"center": [37.5, 22.5], "radius_m": 2000}
assert centre["kind"] == "postcodes" and centre["geometry"] is None
_rule(client, h, name="Drawn", area_ids=[drawn["id"]], action="set", value=1.5)
_rule(client, h, name="Ring", area_ids=[ring["id"]], action="set", value=3)
p = _preview(client, h, lat=37.05, lng=22.05, subtotal=12, items=2)
assert (p["area_name"], p["fee"]) == ("Drawn", 1.5)
assert _preview(client, h, lat=37.51, lng=22.5, subtotal=12, items=2)["area_name"] == "2 km"
assert _preview(client, h, lat=36.0, lng=22.0, subtotal=12, items=2)["refused"] == "area_not_served"
assert _preview(client, h, area_id=drawn["id"], subtotal=12, items=2)["refused"] == "area_not_served" # a shape needs a point
assert _preview(client, h, postcode="24100", subtotal=12, items=2)["area_name"] == "Village centre"
# an area can change kind: back to postcodes drops the shape
r = client.put(f"/api/connect/fees/areas/{drawn['id']}", headers=h, json={"name": "Drawn", "postcodes": ["24131"]})
assert r.status_code == 200 and (r.json()["kind"], r.json()["geometry"], r.json()["postcodes"]) == ("postcodes", None, ["24131"])
@pytest.mark.parametrize("body", [
{"kind": "polygon", "geometry": [[37, 22], [37, 22.1]]}, # two corners
{"kind": "polygon", "geometry": [[37, 22], [37, 22.1], [91, 22]]}, # latitude past 90
{"kind": "polygon", "geometry": "a square"},
{"kind": "circle", "geometry": {"center": [37, 22], "radius_m": 10}}, # under 50 m
{"kind": "circle", "geometry": {"center": [37, 22], "radius_m": 60000}}, # over 50 km
{"kind": "circle", "geometry": {"center": [37], "radius_m": 500}},
{"kind": "triangle", "geometry": None},
])
def test_bad_shapes_are_refused(client, owner_h, body):
assert client.post("/api/connect/fees/areas", headers=owner_h, json={"name": "x", **body}).status_code == 422
def test_the_venues_place_on_the_map(db, client, owner_h):
from services.cloud_sync import menu_snapshot
assert client.get("/api/connect/fees", headers=owner_h).json()["venue_point"] is None
r = client.put("/api/connect/fees/venue-point", headers=owner_h, json={"lat": 37.0365123456, "lng": 22.1142})
assert r.json()["venue_point"] == {"lat": 37.0365123, "lng": 22.1142}
assert client.get("/api/connect/fees", headers=owner_h).json()["venue_point"] == {"lat": 37.0365123, "lng": 22.1142}
_area(client, owner_h, "Drawn", kind="polygon", geometry=SQUARE)
snap, _ = menu_snapshot(db)
assert snap["fees"]["venue_point"] == {"lat": 37.0365123, "lng": 22.1142}
assert snap["fees"]["areas"][0]["geometry"] == SQUARE and snap["fees"]["areas"][0]["kind"] == "polygon"
assert client.put("/api/connect/fees/venue-point", headers=owner_h, json={}).json()["venue_point"] is None
assert client.put("/api/connect/fees/venue-point", headers=owner_h, json={"lat": 95, "lng": 1}).status_code == 422
@@ -176,3 +176,25 @@ def test_the_venues_ordering_link(client, owner_h, monkeypatch):
monkeypatch.setattr(cloud_sync.settings, "SITE_ID", "taverna-1")
links = client.get("/api/connect/fees", headers=owner_h).json()["links"]
assert links == {"order": "https://menu.example.com/order/taverna-1", "menu": "https://menu.example.com/menu/taverna-1"}
def test_a_pinned_delivery_keeps_its_pin_and_is_checked_by_it(client, db, counter, menu):
"""6R.G: an order priced by a drawn area: the site checks the fee with the pin (a shape is
matched only by a point inside it), keeps the pin for a map link on the counter."""
from models.fees import DeliveryArea, FeeRule
from models.order import Order
drawn = DeliveryArea(name="Drawn", kind="polygon", postcodes="[]", is_active=1, sort_order=0,
geometry=json.dumps([[37.0, 22.0], [37.0, 22.1], [37.1, 22.1], [37.1, 22.0]]))
db.add(drawn)
db.flush()
db.add(FeeRule(name="Drawn", is_active=1, sort_order=0, sales_points=json.dumps(["online_delivery"]),
area_ids=json.dumps([drawn.id]), action="set", value=2.5, stop=1))
db.commit()
oid = pull(db, menu, cloud_id=611, area_id=drawn.id, area="Drawn", lat=37.05, lng=22.05)
order = db.get(Order, oid)
assert order.online_price_diff is None and (order.online_lat, order.online_lng) == (37.05, 22.05)
assert next(x for x in board(client, counter)["incoming"] if x["id"] == oid)["pin"] == {"lat": 37.05, "lng": 22.05}
# no pin: the site can't place it in the drawn area, and says so (the customer keeps the cloud's fee)
oid = pull(db, menu, cloud_id=612, area_id=drawn.id, area="Drawn")
assert json.loads(db.get(Order, oid).online_price_diff)["fee"]["reason"] == "area"
assert next(x for x in board(client, counter)["incoming"] if x["id"] == oid)["pin"] is None