Every ride in CruciDrive follows a deterministic lifecycle enforced entirely on the server. The backend’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/DavidCevallos15/Crucidrive---APP/llms.txt
Use this file to discover all available pages before exploring further.
cambiarEstadoViaje controller validates the current state of a ride before applying any transition, ensuring that no client — regardless of role — can skip a step or revert a completed ride. The estado column on the viajes table is constrained by a PostgreSQL CHECK to only accept the five valid values, adding a final database-level safeguard beneath the application logic.
Ride States
| State | Who can trigger | Description |
|---|---|---|
solicitado | pasajero | Ride has been requested; waiting for a driver to accept |
aceptado | conductor | Driver has accepted; a private chat thread is automatically created |
en_curso | pasajero or conductor | Driver has started the trip; passengers are being transported |
finalizado | pasajero or conductor | Trip has been completed successfully |
cancelado | pasajero or conductor | Ride was cancelled before or during transit (not applicable once finalizado) |
State Transition Rules
ThecambiarEstadoViaje controller applies three hard guards before writing any state change to the database. These guards are checked in order and return a 400 Bad Request if violated:
pasajero_id and conductor_id are compared against req.user.id, so only the two participants of a specific ride can change its state.
State machine
cancelado can be triggered from solicitado, aceptado, or en_curso. It is blocked only when the ride is already finalizado or already cancelado. The idle state is a frontend-only concept used by the useRideStore Zustand store to represent the pre-request UI. It has no corresponding database row.
Ride creation — solicitarViaje
Apasajero calls POST /api/viajes/solicitar with origen and destino coordinate objects. The backend validates that both lat and lng are present, converts them to PostGIS Well-Known Text via the toWKT utility, and inserts a new row into viajes with a fixed fare of $1.50:
Ride acceptance — aceptarViaje
Accepting a ride is a multi-step transactional flow. The backend first verifies the ride is still insolicitado state, then performs three sequential writes with rollback logic at each step:
Create chat thread
A new row is inserted into
threads with the viaje_id. If this insert fails, the ride is rolled back to solicitado and the conductor assignment is cleared.Frontend state — RideStore
The React Native frontend mirrors the backend state enum through theuseRideStore Zustand store. The RideStatus type adds an idle state for the pre-request UI:
setRideStatus
Updates only the
status field of the active ride. Used by Socket.io event handlers when the server broadcasts a state change.setDriver
Assigns
DriverInfo to the active ride and automatically sets status to 'aceptado'. Called when a conductor accepts the ride.updateRide
Applies a partial update (
Partial<ActiveRide>) to the active ride. Useful for updating chatThreadId after acceptance.clearRide
Resets
activeRide to null and isRequesting to false. Called on finalizado or cancelado to return the UI to idle.