Data Layer high complexity mobilebackend
0
Dependencies
2
Dependents
0
Entities
1
Integrations

Description

Data access layer for reading and writing peer mentor location data stored as PostGIS points in Supabase. Supports spatial bounding box queries for viewport-based fetching and enforces row-level security so coordinators can only read consented locations within their organization.

Feature: Geographic Peer Mentor Map View

mentor-location-repository

Summaries

The Mentor Location Repository is the foundational data layer that makes real-time geographic mentor discovery possible. It stores mentor positions as precise spatial points in Supabase PostGIS, enabling the platform to perform high-performance bounding box queries that return only the mentors visible within a coordinator's current map viewport — regardless of how large the overall mentor database grows. Critically, it enforces row-level security so coordinators can only ever access location data within their own organization, protecting data boundaries between tenants and eliminating the risk of cross-organization data leakage. The automatic deletion of location data upon consent revocation ensures the organization can honor mentor privacy commitments reliably and at scale without manual intervention.

Mentor Location Repository is a high-complexity data component that is a dependency for both the Mentor Location Service and the Location Consent Service, placing it early on the critical path. The PostGIS spatial query implementation requires either a Supabase database function (RPC) or a PostgREST extension setup — confirm the approach with the backend team before development begins, as this decision affects the Supabase project configuration and may require infrastructure provisioning time. Row-level security policy authoring and testing requires careful coordination with the database administrator and a dedicated security review. Plan for a full integration test suite covering bounding box edge cases, organization isolation, and consent-delete atomicity.

Any instability in this component will block multiple upstream features simultaneously, so prioritize its stability milestone.

Mentor Location Repository is the spatial data access layer targeting Supabase with PostGIS. Bounding box queries should be implemented as a Supabase RPC (PostgreSQL function) using ST_MakeEnvelope and ST_Within rather than client-side filtering to leverage PostGIS indexing for performance. The organizationId parameter maps directly to the row-level security context — ensure the Supabase client is initialized with the authenticated session so RLS policies evaluate correctly per request. upsertLocation uses INSERT ...

ON CONFLICT DO UPDATE with the mentorId as the conflict key. The upsertConsentRecord method is called by Location Consent Service and should be treated as an internal contract — validate that consent records and location deletes are issued in the correct order. Use Supabase's .select() with PostGIS ST_AsText or ST_X/ST_Y to deserialize coordinates into MentorLocationRecord. Add a spatial index on the location column in the migration script to ensure bounding box query performance does not degrade at scale.

Responsibilities

  • Insert or update mentor location as PostGIS point
  • Query mentors within a geographic bounding box
  • Delete location data when consent is revoked
  • Apply organization-scoped row-level security

Interfaces

upsertLocation(String mentorId, double lat, double lng) → Future<void>
deleteLocation(String mentorId) → Future<void>
getMentorsInBounds(LatLngBounds bounds, String organizationId) → Future<List<MentorLocationRecord>>
getMentorLocation(String mentorId) → Future<MentorLocationRecord?>
upsertConsentRecord(ConsentRecord record) → Future<void>
getConsentRecord(String mentorId) → Future<ConsentRecord?>

Relationships

Dependents (2)

Components that depend on this component

Used Integrations (1)

External integrations and APIs this component relies on

API Contract

View full contract →
REST /api/v1/location-records 6 endpoints
GET /api/v1/location-records
GET /api/v1/location-records/:mentor_id
POST /api/v1/location-records
PUT /api/v1/location-records/:mentor_id
DELETE /api/v1/location-records/:mentor_id
GET /api/v1/location-records/bounds