Create PostGIS bounding-box RPC function
epic-geographic-peer-mentor-map-data-infrastructure-task-004 — Write a Supabase RPC function (find_mentors_in_bounds) that accepts min_lat, min_lng, max_lat, max_lng, and organisation_id parameters, performs a PostGIS ST_MakeEnvelope bounding-box spatial query on the mentor_locations table, and returns mentor_id, latitude, longitude, display_name, and consent_level fields. Include organisation_id filter to enforce RLS at the query level as a defence-in-depth measure.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Create the migration file as supabase/migrations/
For approximate location support: JOIN organisation_location_config and apply ST_SnapToGrid(location, 0.1) to reduce precision to ~10km grid when precision = 'approximate'. Run EXPLAIN (ANALYZE, BUFFERS) after seeding test data to confirm the GiST index is used.
Testing Requirements
Unit test the SQL function directly via psql or supabase db test with pgTAP: (1) assert returns correct rows within bounds, (2) assert excludes rows outside bounds, (3) assert excludes expired consent rows, (4) assert organisation_id filter isolates multi-tenant data, (5) assert empty result for empty bounds, (6) assert error on inverted bounding box. Integration test via Supabase client RPC call from a Dart test: verify typed response deserialization. Performance test with seed data of 10,000 mentor rows — confirm EXPLAIN ANALYZE shows index scan, not seq scan.
Supabase's hosted PostGIS extension behaviour may differ from the local emulator for spatial RPC functions, causing bounding-box queries to return incorrect results or fail in production while passing locally.
Mitigation & Contingency
Mitigation: Write integration tests against the Supabase emulator from the start and run the same test suite against a staging Supabase project before merging. Use ST_DWithin and ST_MakeEnvelope in plain SQL first, validate with psql, then wrap as RPC.
Contingency: If PostGIS RPC proves unreliable, fall back to client-side bounding box filtering on a full fetch of consented mentor locations (acceptable for up to ~200 mentors per chapter) until the spatial query is stabilised.
OpenStreetMap tile usage may require attribution handling and rate limiting. Switching to Google Maps Flutter plugin mid-implementation would require significant rework of the map-provider-integration abstraction.
Mitigation & Contingency
Mitigation: Define the map-provider-integration abstraction interface before selecting the SDK so that the concrete implementation is swappable. Implement OSM first with correct attribution. Document Google Maps as the alternate with its API key setup steps.
Contingency: If OSM tiles are rejected by stakeholders or tile server limits are hit, activate the Google Maps Flutter plugin implementation behind the same interface without touching any UI or service code.
Incorrect RLS configuration could allow a coordinator to query mentor locations from a different organisation, constituting a GDPR data breach.
Mitigation & Contingency
Mitigation: Write dedicated RLS integration tests with two isolated test organisations and assert that cross-organisation queries return zero rows. Include these tests in CI. Have a second developer review all RLS policy SQL before migration is applied.
Contingency: If a cross-organisation data leak is discovered post-deployment, immediately disable the map feature via the organisation feature flag, revoke the affected Supabase RLS policy, and notify the data protection officer per the organisation's GDPR incident response procedure.