Skip to content

Trips and Cars

This page specifies the Slice 4 core trip contract: company-scoped trip planning, Passenger trip discovery and booking, and Driver trip execution. It draws from the domain model, the preserved build spec (§5.3), and the API conventions.

1. Scope

Slice 4 implements:

  • Manager create, list, show, full update, and cancel for company trips.
  • Passenger available-trip listing, booking requests, own-booking listing/detail/cancellation, and reservation deadlines.
  • Manager booking review, approval, and rejection with trip-capacity enforcement.
  • Driver assigned-trip listing/detail and start/complete lifecycle actions.

The following remain separate slices: NavRoute/Vehicle management UI and endpoints, trip duplication, trip-group presentation beyond trip_group_code, extra cars, boarding/GPS/live operations, D2D, reviews, and analytics. Slice 4 creates the minimum company-scoped nav_routes and vehicles records required by trip references; it does not expose management endpoints for them.

2. Core records

Entity Slice 4 fields / rules
NavRoute id, company_id, name_en, name_ar, origin, destination, nullable stops JSON. A Trip snapshots the route origin and destination.
Vehicle id, company_id, name, positive seat_capacity, status = active\|inactive. Platform owns no vehicles.
Driver An active Membership with role driver. A Trip's driver_id is the Membership ID, not the User ID.
Trip id, company_id, nav_route_id, origin, destination, departure_at, vehicle_id, driver_id, positive seat_capacity, nullable reservation_deadline_at, nullable trip_group_code, nullable notes, status, nullable started_at, completed_at, cancelled_at, timestamps.
Booking id, company_id, trip_id, user_id (Passenger), status, nullable rejection_reason, nullable decided_at, cancelled_at, boarded_at, timestamps. No charge or payment fields.

seat_capacity is a Trip snapshot and must be positive and no greater than the assigned Vehicle's capacity. Route, Vehicle, and Driver references must belong to the Manager's Company. A cross-company reference on create/update returns 422 with the corresponding field error; a cross-company Trip URL returns 404.

Trips and Bookings carry company_id directly and use CompanyScope. Booking company_id must equal its Trip's company_id. company_id is never accepted from a client.

3. Trip lifecycle

State Description Allowed next states
scheduled Created/planned; bookings may be requested and approved. in_transit, cancelled
in_transit Assigned Driver has started the trip. completed, cancelled
completed Assigned Driver completed the trip. Terminal. None
cancelled Manager cancelled the trip. Terminal. None
  • POST /api/driver/trips/{id}/start transitions scheduled → in_transit and sets started_at.
  • POST /api/driver/trips/{id}/complete transitions in_transit → completed and sets completed_at.
  • POST /api/company/trips/{id}/cancel transitions scheduled|in_transit → cancelled, sets cancelled_at, and cancels every pending|approved Booking on the Trip.
  • Starting a Trip rejects its remaining pending Bookings. Managers cannot approve Bookings after a Trip has started.
  • Planning fields can be fully replaced only while the Trip is scheduled. Lifecycle timestamps/status cannot be supplied by a Manager.
  • Invalid transitions return 422 with errors.status; inaccessible or cross-company Trip IDs return 404.

There is no separate boarding Trip state in Slice 4. Boarding is a later Live Ops action recorded by bookings.boarded_at; it does not change the Slice 4 Booking status vocabulary.

4. Manager trip endpoints

All routes require auth:sanctum, company, and Manager authorization.

Method Path Purpose
GET /api/company/trips Paginated company Trip list. Filters: status=scheduled\|in_transit\|completed\|cancelled\|all, page, per_page (default 15, max 100).
POST /api/company/trips Create a scheduled Trip.
GET /api/company/trips/{id} Show a company Trip.
PUT /api/company/trips/{id} Replace all planning fields while scheduled.
POST /api/company/trips/{id}/cancel Cancel a scheduled or in-transit Trip and its active Bookings.
GET /api/company/trips/{id}/bookings Paginated Bookings for a company Trip. Filters: status, page, per_page.
POST /api/company/trips/{id}/bookings/{booking_id}/approve Approve a pending Booking.
POST /api/company/trips/{id}/bookings/{booking_id}/reject Reject a pending Booking. Optional {reason} (max 1000).

Passenger search/list:

Method Path Purpose
GET /api/passenger/trips Paginated bookable Trips for the authenticated Passenger's Company. Returns only scheduled, future-departure Trips whose reservation deadline has not passed and that still have seats left. Filters: origin, destination, date (Y-m-d), nav_route_id, page, per_page. company_id is never accepted from the client; company scope comes from membership.

4.1 Create and update body

Create and full update use the same body:

{
  "nav_route_id": 3,
  "origin": "Company office",
  "destination": "Airport",
  "departure_at": "2026-12-01T06:00:00Z",
  "vehicle_id": 2,
  "driver_id": 8,
  "seat_capacity": 14,
  "reservation_deadline_at": "2026-11-30T18:00:00Z",
  "trip_group_code": "2026-12-01-0600",
  "notes": "Leave from the north gate"
}
Field Type Required Notes
nav_route_id integer yes Existing NavRoute in the authenticated Company.
origin string yes Max 255; snapshot of the route origin.
origin_lat number|null no Must be supplied together with origin_lng; latitude -90..90. Used as Passenger pickup ETA target.
origin_lng number|null no Must be supplied together with origin_lat; longitude -180..180.
destination string yes Max 255; snapshot of the route destination.
destination_lat number|null no Must be supplied together with destination_lng; latitude -90..90. Used as Manager destination ETA target.
destination_lng number|null no Must be supplied together with destination_lat; longitude -180..180.
departure_at ISO-8601 datetime yes Returned in UTC.
vehicle_id integer yes Active Vehicle in the authenticated Company.
driver_id integer yes Active Driver Membership in the authenticated Company.
seat_capacity integer yes 1–100 and no greater than Vehicle capacity.
reservation_deadline_at ISO-8601 datetime|null no Nullable; when present must be at or before departure_at.
trip_group_code string|null no Max 100; groups cars sharing route and departure time.
notes string|null no Max 1000; Manager operational notes.

company_id, status, booked_seats, seats_left, lifecycle timestamps, and price/payment fields are not accepted. booked_seats/seats_left are derived from approved Bookings. Passengers pay nothing, so Slice 4 defines no Trip or Booking price field.

Create returns 201 TripResource; show/update/cancel return 200 TripResource.

4.2 TripResource

{
  "id": 12,
  "company_id": 1,
  "nav_route_id": 3,
  "route": {
    "id": 3,
    "name_en": "Office to Airport",
    "name_ar": "المكتب إلى المطار",
    "origin": "Company office",
    "destination": "Airport"
  },
  "origin": "Company office",
  "origin_location": { "lat": 15.30, "lng": 44.10 },
  "destination": "Airport",
  "destination_location": { "lat": 15.40, "lng": 44.20 },
  "departure_at": "2026-12-01T06:00:00Z",
  "reservation_deadline_at": "2026-11-30T18:00:00Z",
  "vehicle": { "id": 2, "name": "Van 1", "seat_capacity": 14 },
  "driver": { "id": 8, "user_id": 4, "name": "Driver Name" },
  "seat_capacity": 14,
  "seats_left": 10,
  "trip_group_code": "2026-12-01-0600",
  "notes": "Leave from the north gate",
  "status": "scheduled",
  "started_at": null,
  "completed_at": null,
  "cancelled_at": null,
  "created_at": "2026-11-27T12:00:00Z",
  "updated_at": "2026-11-27T12:00:00Z"
}

Collections use data, meta (current_page, last_page, per_page, total), and links.

5. Capacity, grouping, and concurrency

  • Only approved Bookings occupy Trip seats. pending, rejected, and cancelled Bookings do not reduce seats_left.
  • Approval locks the Trip row, re-counts approved Bookings, and rejects approval with 422 errors.seats when approved_count >= seat_capacity.
  • A Manager cannot lower seat_capacity below the existing approved Booking count; update returns 422 errors.seats.
  • A Passenger may have at most one active Booking (pending or approved) per Trip. Request creation locks the Trip row before checking/creating the Booking.
  • Duplicate active requests return 422 errors.trip_id. A Passenger may request again after a previous Booking is rejected or cancelled.
  • Trip cancellation releases all occupied seats by cancelling active Bookings.
  • trip_group_code is stored and returned in Slice 4; grouped-car presentation is a later surface concern. Each Trip enforces its own capacity.

6. Open questions (future slices)

  • Duplicate trip fields: Whether duplication copies or reschedules reservation_deadline_at, trip_group_code, and Driver assignment.
  • Trip-group presentation: Exact grouping/sort behavior for cars sharing a route and departure time.
  • Car request lifecycle/routing: Allowed car_requests states and whether a request creates a Trip, a separate dispatch entity, or both. car_requests remain a separate dispatch concept; this Fleet slice does not implement extra-car endpoints beyond Vehicle management.

7. Fleet management (Slice 7)

Slice 7 exposes company-scoped Manager CRUD for Vehicles and NavRoutes. These endpoints are Manager-only, company-scoped, suspended-company blocked (403), and cross-tenant access returns 404.

7.1 Vehicle

Field Type Required Notes
name string yes Max 255.
plate string|null no Max 50. Unique within company when present (case-insensitive trim).
capacity integer yes 1–100. Trip seat_capacity cannot exceed referenced Vehicle capacity.
type string|null no Max 50. Free-form fleet label (e.g. bus, van, sedan).
icon string|null no Max 100. UI icon key; not a URL upload.
status string no active or inactive; default active.

VehicleResource: {id, company_id, name, plate, capacity, type, icon, status, created_at, updated_at}. The legacy seat_capacity column remains the storage field; API reads/writes use capacity.

Endpoints:

Method Path Purpose
GET /api/company/vehicles Paginated list. Filters: status=active\|inactive\|all, page, per_page.
POST /api/company/vehicles Create vehicle.
GET /api/company/vehicles/{id} Show vehicle.
PUT /api/company/vehicles/{id} Update vehicle.
DELETE /api/company/vehicles/{id} Delete/deactivate. Returns 422 when referenced by active trips (scheduled or in_transit); otherwise deletes the vehicle record.

Validation: capacity 1–100; capacity cannot be reduced below any active trip seat_capacity referencing the vehicle (422 errors.capacity). Duplicate plate in same company returns 422 errors.plate.

7.2 NavRoute

Field Type Required Notes
name_en string yes Max 255.
name_ar string yes Max 255.
origin string yes Max 255.
destination string yes Max 255.
stops array|null no Ordered stop objects: {name, name_ar?, lat?, lng?}.
origin_lat number|null no With origin_lng, latitude -90..90.
origin_lng number|null no With origin_lat, longitude -180..180.
destination_lat number|null no With destination_lng.
destination_lng number|null no With destination_lat.
active boolean no Default true. Inactive routes cannot be assigned to new trips.

NavRouteResource: {id, company_id, name_en, name_ar, origin, destination, stops, origin_lat, origin_lng, destination_lat, destination_lng, active, created_at, updated_at}.

Endpoints:

Method Path Purpose
GET /api/company/nav-routes Paginated list. Filters: active=true\|false\|all, page, per_page.
POST /api/company/nav-routes Create nav route.
GET /api/company/nav-routes/{id} Show nav route.
PUT /api/company/nav-routes/{id} Update nav route.
DELETE /api/company/nav-routes/{id} Delete/deactivate. Returns 422 when referenced by active trips; otherwise deletes.

Validation: coordinate pairs must be supplied together; stop coordinates, when present, must be valid lat/lng pairs. Active trips referencing the route block deletion (422 errors.nav_route). Inactive routes are rejected by Trip create/update validation (errors.nav_route_id).

7.3 Ownership and deletion invariants

  • Company-owned: company_id derived from membership; never accepted from client.
  • Cross-tenant show/update/delete returns 404.
  • Manager-only; non-manager company users get 403.
  • Suspended company gets 403 on all company routes.
  • No hard delete of a Vehicle/NavRoute referenced by active trips (scheduled, in_transit). Delete returns 422 with errors.vehicle / errors.nav_route. Historical/completed/cancelled trip references do not block delete because FK behavior remains restrict; in that case the endpoint returns 422 errors.vehicle/errors.nav_route with "referenced by trips" unless the record is deactivated instead. Decision: delete attempts on any referenced record return 422 instructing deactivation; deactivation is represented by status=inactive/active=false via update. DELETE performs hard delete only when no trips reference the record. This preserves trip history and avoids cascading data loss.