Skip to content

Live Tracking

This page defines the Slice 5 contract for Driver location publishing, Passenger and Manager tracking reads, ETA, persistence, and Reverb delivery.

1. Scope and authorization

  • A Driver publishes only for a Trip assigned to their active Driver Membership and only while the Trip is in_transit.
  • A Passenger reads tracking only with an approved Booking on the Trip and an active Passenger Membership in the Trip's Company.
  • A Manager reads any Trip in their Company and lists Company Trips that are in_transit.
  • company_id is derived from authenticated membership and is never accepted from a client.
  • Cross-tenant Trip access returns 404. An inaccessible Driver assignment returns 404. An authenticated Passenger without an approved Booking on a same-Company Trip receives 403.
  • A suspended Company returns 403 with {"message":"Company is suspended."}.
  • Tracking resources and events expose only Trip, location, ETA, Driver {id,name}, and Vehicle {id,name} data. They never expose phone, email, license, documents, other Passengers, or Manager notes.

2. Trip ETA coordinates

Slice 5 adds optional coordinate pairs to Trip planning so ETA has an explicit target:

Field Type Rule
origin_lat, origin_lng number or null Must be supplied together; latitude -90..90, longitude -180..180. The origin is the route-stop pickup target.
destination_lat, destination_lng number or null Must be supplied together; same ranges. The destination is the Manager ETA target.

TripResource returns origin_location and destination_location as {lat,lng} or null. Existing Trips without coordinates remain valid; their ETA is null until coordinates are supplied while the Trip is scheduled.

3. Driver location publishing

POST /api/driver/trips/{id}/location requires auth:sanctum, company, and an active Driver Membership.

Request:

{
  "lat": 15.3694,
  "lng": 44.1910,
  "heading": 90,
  "speed": 32.5,
  "recorded_at": "2026-11-27T12:00:00Z"
}
Field Type Required Rule
lat number yes -90..90.
lng number yes -180..180.
heading number no 0 <= heading < 360; degrees clockwise from north.
speed number no Non-negative kilometres per hour.
recorded_at ISO-8601 datetime no Defaults to server receipt time; cannot be later than server receipt time. Responses use UTC.

The API stores every accepted point in trip_tracking_points and maintains one current row in trip_latest_locations. A point becomes current only when its recorded_at is newer than the current row. Equal or older points remain in history, return is_latest: false, and do not broadcast or replace the current marker.

200 response:

{
  "trip_id": 12,
  "company_id": 1,
  "location": {
    "lat": 15.3694,
    "lng": 44.1910,
    "heading": 90,
    "speed": 32.5,
    "recorded_at": "2026-11-27T12:00:00Z"
  },
  "is_latest": true
}

Errors:

Case Status Envelope
Invalid location fields or future recorded_at 422 message + field-level errors
Trip is not in_transit 422 message + errors.status
Wrong role/inactive Driver Membership 403 message only
Missing, cross-Company, or unassigned Trip 404 message only
Suspended Company 403 message only

4. Passenger tracking

GET /api/passenger/trips/{id}/tracking requires auth:sanctum, company, and an active Passenger Membership with an approved Booking on the Trip.

200 response:

{
  "trip_id": 12,
  "company_id": 1,
  "status": "in_transit",
  "latest_location": {
    "lat": 15.3694,
    "lng": 44.1910,
    "heading": 90,
    "speed": 32.5,
    "recorded_at": "2026-11-27T12:00:00Z"
  },
  "eta": {
    "target": "pickup",
    "eta_seconds": 600,
    "distance_meters": 5000,
    "speed_kmh": 30,
    "calculated_at": "2026-11-27T12:00:05Z"
  },
  "driver": { "id": 8, "name": "Driver Name" },
  "vehicle": { "id": 2, "name": "Van 1" }
}

latest_location is null before the first point. eta is null unless the Trip is in_transit, a latest point exists, and the relevant coordinate pair exists. For Slice 5 the Passenger pickup target is the Trip origin; D2D pickup points remain a later slice. Reads are allowed for an approved Booking in any Trip status so clients can reconcile after completion.

5. Manager tracking and live list

GET /api/company/trips/{id}/tracking requires auth:sanctum, company, and Manager authorization. It returns the same resource shape as Passenger tracking, with eta.target = "destination". A missing or cross-Company Trip returns 404.

GET /api/company/live requires the same middleware and returns a paginated collection of that resource for Company Trips whose status is in_transit. Query parameters are page (minimum 1) and per_page (default 15, maximum 100). Trips without a location remain in the list with latest_location: null and eta: null. Ordering is departure_at, then Trip ID.

Both Manager reads use destination_location for ETA. Manager resources never include Passenger lists, phone numbers, email addresses, or Driver profile documents.

6. ETA calculation

Slice 5 uses a deterministic, provider-free estimate:

  1. Calculate great-circle distance with the haversine formula and mean Earth radius 6,371,000 metres.
  2. Use the latest reported speed when it is greater than zero; otherwise use 25 km/h.
  3. eta_seconds = ceil(distance_meters / (speed_kmh * 1000 / 3600)).
  4. Return integer distance_meters, speed_kmh, eta_seconds, and the server calculated_at timestamp.

This is a straight-line lower-bound estimate, not traffic-aware routing. Directions/traffic and D2D target selection are deferred. No ETA is fabricated when the target coordinates or latest location are missing, or when the Trip is not in_transit.

7. Realtime contract

Location updates use one event on both authorized channels:

Wire channel Authorized subscribers
private-company.{companyId} Active Manager of that Company in a non-suspended Company.
private-trip.{tripId} Active Manager of the Trip's Company, assigned active Driver, or active Passenger with an approved Booking on the Trip.

The private- prefix on private-trip.{tripId} is required by Laravel private-channel authorization; the previous logical name trip.{tripId} is superseded.

Event name: trip.location_updated. Payload:

{
  "event": "trip.location_updated",
  "trip_id": 12,
  "company_id": 1,
  "status": "in_transit",
  "location": {
    "lat": 15.3694,
    "lng": 44.1910,
    "heading": 90,
    "speed": 32.5,
    "recorded_at": "2026-11-27T12:00:00Z"
  }
}

The event contains no Driver/Passenger identity or ETA. Clients calculate or fetch target-specific ETA through the REST resource. Channel authorization re-reads Membership, Company, Trip, assignment, and Booking state for every subscription request; denied or stale access is refused.

8. Persistence

  • trip_tracking_points: immutable accepted history with direct company_id and trip_id, coordinates, optional heading/speed, recorded_at, and timestamps.
  • trip_latest_locations: one row per (company_id, trip_id), referencing the history point selected by greatest recorded_at.
  • Both models use CompanyScope. Application services explicitly verify that the point Company equals the Trip Company.
  • Indexes support Company/Trip history lookup and latest-row uniqueness.

9. Offline, retention, and recovery boundary

  • Clients may send an older recorded_at; it is retained but cannot move the live marker backwards.
  • Slice 5 does not define replay-safe idempotency keys or history retention periods. These remain open product/operations decisions.
  • After reconnect, clients use the REST tracking endpoints to replace local state. Reverb does not provide replay/backfill in this slice.

10. Maps and localization

Maps stay LTR in Arabic and English interfaces. API timestamps are UTC. Realtime payload fields are stable snake_case and locale-independent.