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
+66
View File
@@ -578,6 +578,72 @@ try {
await page.getByTestId('test-run').click()
await page.waitForFunction(() => document.querySelector('[data-testid="test-result"]')?.dataset.refused, null, { timeout: 5000 }).catch(() => {})
r.check('fees: …a postcode outside the areas is refused', await page.getByTestId('test-result').getAttribute('data-refused') === 'area_not_served')
// ── 6R.G: the venue on the map, an area drawn on it, a circle, "test an order" by a point ──
{
await page.getByTestId('fees-tab-areas').click()
await page.getByTestId('venue-edit').click()
const vmap = page.getByTestId('venue-map')
await vmap.waitFor({ timeout: 5000 })
const vb = await vmap.boundingBox()
await vmap.click({ position: { x: vb.width / 2, y: vb.height / 2 } })
await page.getByTestId('venue-save').click()
let venuePoint = null
for (let i = 0; i < 30 && !venuePoint; i++) { venuePoint = (await api('GET', '/api/connect/fees', { token: boss })).venue_point; if (!venuePoint) await sleep(150) }
r.check('6R.G: the venue\'s place set by a tap on the map', !!venuePoint && Math.abs(venuePoint.lat) > 1, JSON.stringify(venuePoint))
// a shape: four taps, then a tap on the last corner takes it away
await page.getByTestId('area-add').click()
await page.getByTestId('area-name').fill('Drawn')
await page.getByTestId('area-kind-polygon').click()
const dmap = page.getByTestId('delivery-map')
await dmap.waitFor({ timeout: 5000 })
await sleep(500)
const db = await dmap.boundingBox()
const cx = db.width / 2, cy = db.height / 2
for (const [dx, dy] of [[-150, -100], [150, -100], [0, 120], [120, 110]]) {
await dmap.click({ position: { x: cx + dx, y: cy + dy } })
await sleep(150)
}
const four = await dmap.getAttribute('data-count')
await dmap.click({ position: { x: cx + 120, y: cy + 110 } }) // the 4th corner: removed
await sleep(200)
r.check('6R.G: corners added by taps on the map, one taken away by a tap on it', four === '4' && await dmap.getAttribute('data-count') === '3',
`${four} → ${await dmap.getAttribute('data-count')}`)
await page.screenshot({ path: path.join(OUT, 'manager-area-drawn.png') })
await page.getByTestId('area-save').click()
await page.getByTestId('area').filter({ hasText: 'Drawn' }).waitFor({ timeout: 5000 }).catch(() => {})
// a circle around the venue (its centre starts there), 1.5 km
await page.getByTestId('area-add').click()
await page.getByTestId('area-name').fill('Ring')
await page.getByTestId('area-kind-circle').click()
await page.screenshot({ path: path.join(OUT, 'manager-area-circle.png') })
await page.getByTestId('area-radius').fill('1500')
await page.getByTestId('area-save').click()
await page.getByTestId('area').filter({ hasText: 'Ring' }).waitFor({ timeout: 5000 }).catch(() => {})
const areasNow = (await api('GET', '/api/connect/fees', { token: boss })).areas
const drawnA = areasNow.find(a => a.name === 'Drawn'), ring = areasNow.find(a => a.name === 'Ring')
r.check('6R.G: saved as a shape with 3 corners and a 1.5 km circle around the venue',
drawnA?.kind === 'polygon' && drawnA.geometry.length === 3 && ring?.kind === 'circle' && ring.geometry.radius_m === 1500
&& ring.geometry.center[0] === venuePoint.lat && ring.geometry.center[1] === venuePoint.lng, JSON.stringify([drawnA, ring]))
r.check('6R.G: the list says what each area is', (await page.getByTestId('area').filter({ hasText: 'Drawn' }).textContent()).includes('3 corners')
&& (await page.getByTestId('area').filter({ hasText: 'Ring' }).textContent()).includes('1.5 km') && await page.getByTestId('areas-map').count() === 1)
// "test an order" by a point: the middle of the map is inside the drawn shape (the first shape wins)
await page.getByTestId('test-subtotal').fill('15')
await page.getByRole('combobox', { name: 'Where' }).selectOption('point')
const tmap = page.getByTestId('test-map')
await tmap.waitFor({ timeout: 5000 })
await sleep(500)
const tb = await tmap.boundingBox()
await tmap.click({ position: { x: tb.width / 2, y: tb.height / 2 } })
await page.getByTestId('test-run').click()
await page.waitForFunction(() => document.querySelector('[data-testid="test-result"]')?.textContent.includes('Drawn'), null, { timeout: 5000 }).catch(() => {})
const tr = page.getByTestId('test-result')
r.check('6R.G: "test an order" by a point on the map finds the drawn area and its fee', (await tr.textContent()).includes('Drawn')
&& await tr.getAttribute('data-refused') === '' && await tr.getAttribute('data-fee') === '2', await tr.textContent())
await page.screenshot({ path: path.join(OUT, 'manager-test-point.png') })
for (const a of [drawnA, ring]) if (a) await api('DELETE', `/api/connect/fees/areas/${a.id}`, { token: boss }).catch(() => {})
await page.reload()
await page.getByTestId('fees-page').waitFor({ timeout: 10_000 })
}
// S.5: the fee's line on the bill and the receipt (a system product, never in the catalogue)
await page.getByTestId('fees-tab-settings').click()
await page.getByTestId('fee-product-name').fill('Delivery')