core PK: id 10 required 1 unique

Description

Stores geographic location data for peer mentors, enabling map-based visualization of active mentors and geographic proximity matching for assignment dispatch. Supports both GPS-captured and manually entered locations with privacy controls.

16
Attributes
4
Indexes
5
Validation Rules
11
CRUD Operations

Data Structure

Name Type Description Constraints
id uuid Primary key, globally unique location record identifier
PKrequiredunique
user_id uuid Foreign key to users table — the peer mentor this location belongs to
required
latitude decimal WGS84 latitude coordinate, precision 8 decimal places (~1mm accuracy)
required
longitude decimal WGS84 longitude coordinate, precision 8 decimal places
required
accuracy_meters decimal GPS horizontal accuracy radius in metres as reported by the device. NULL when location source is manual or geocoded.
-
location_source enum How the location was obtained — affects display precision and trust level
required
municipality_code string Norwegian municipality code (kommunenummer, 4 digits) reverse-geocoded from coordinates. Used for regional filtering and Bufdir-area matching.
-
municipality_name string Human-readable municipality name, cached at record creation to avoid repeated reverse-geocoding
-
county_name string Norwegian county (fylke) name derived from municipality code at write time
-
postal_code string Norwegian postal code (postnummer, 4 digits) if available from geocoding or manual entry
-
is_active boolean True for the single current location record for this user. Previous records are set to false when a new location is recorded. Only the active record is used for map rendering and matching.
required
is_visible_on_map boolean Privacy flag — peer mentor can opt out of appearing on the coordinator map while retaining their location for assignment matching. Defaults to true.
required
recorded_at datetime Timestamp when the location was captured or manually entered, in UTC
required
expires_at datetime Optional expiry time after which this location record is treated as stale and excluded from matching and map display. NULL means no expiry.
-
created_at datetime Record creation timestamp
required
updated_at datetime Record last update timestamp
required

Database Indexes

idx_peer_mentor_locations_user_active
btree

Columns: user_id, is_active

idx_peer_mentor_locations_map_query
btree

Columns: is_active, is_visible_on_map, latitude, longitude

idx_peer_mentor_locations_municipality
btree

Columns: municipality_code, is_active

idx_peer_mentor_locations_recorded_at
btree

Columns: user_id, recorded_at

Validation Rules

valid_norwegian_coordinates error

Validation failed

accuracy_required_for_gps_source error

Validation failed

recorded_at_not_future error

Validation failed

user_must_be_peer_mentor_or_coordinator error

Validation failed

municipality_code_format warning

Validation failed

Business Rules

single_active_location_per_user
on_create

A peer mentor may have at most one active location record (is_active = true) at any time. When a new location is written, all prior records for that user_id are set to is_active = false before the new record is inserted.

map_visibility_requires_active_mentor_status
always

A location record is only surfaced on the map when the owning user has an active peer mentor status (peer_mentor_statuses.is_active = true). Paused mentors are excluded from map queries regardless of is_visible_on_map.

stale_location_excluded_from_matching
always

Location records where expires_at < now() are treated as stale and excluded from assignment matching results and map rendering. The record is not deleted but is effectively inactive for operational purposes.

location_scoped_to_organization
always

Map queries and matching results are scoped to the requesting coordinator's organization. Peer mentors from other organizations are never surfaced, enforcing tenant isolation.

privacy_opt_out_respected
always

When is_visible_on_map = false the mentor is excluded from map rendering but still eligible for geographic matching used internally by coordinators for assignment suggestions. Mentors are informed of this distinction during opt-out.

Storage Configuration

Storage Type
primary_table
Location
main_db
Partitioning
by_user
Retention
archive_after_1year