Skip to main content

Overview

Queue operations handle fetching trucks currently waiting in the “En cola” state from the viajes_camiones table. The main function maps raw database records to the internal Camion type used throughout the React application.

fetchCamionesCola

Retrieves all trucks currently in the queue, ordered by arrival time.
export async function fetchCamionesCola(): Promise<Camion[]>

Return Type

return
Camion[]
Array of trucks in queue, ordered by hora_llegada ascending. Returns empty array on error.

Query Details

The function performs the following query:
SELECT * FROM viajes_camiones
WHERE estado = 'En cola'
ORDER BY hora_llegada ASC

Type Mapping

The raw database records are transformed to the internal Camion type:
id
string
String representation of the primary key
id_db
number
Integer primary key from database
id_viaje
string
Trip identifier (falls back to id if null)
placa
string
License plate from tracto column (”—” if null)
frotcom
string | undefined
Frotcom tracking ID (undefined if null)
propietario
string
Owner name (”—” if null)
fecha
string
Arrival date
hora
string
Arrival time from hora_llegada
tipoOriginal
string
Raw truck type from tipo_unidad column
tipoCodigo
TipoCamion
Normalized truck type code: ‘P’ (pari-holero), ‘J’ (jumbo), ‘B’ (bi-tren), ‘T’ (tolva), ‘O’ (other)
operacionCodigo
OperacionCodigo
‘C’ for “Carga” (loading), ‘D’ for “Descarga” (unloading)
producto
string
Product type from tipo_carga (if loading) or tipo_descarga (if unloading)
tiempoEntradaPatio
number
Timestamp in milliseconds when truck entered the yard
tiempoLlegadaCola
number
Timestamp in milliseconds when truck joined the queue
estadoAlerta
'verde'
Alert status (always ‘verde’ for queued trucks)
maxAlertaReached
'verde'
Maximum alert level reached (always ‘verde’ initially)
bahiaActual
string | undefined
Current bay assignment (undefined if not assigned)
incidencias
number
Count of incidents from conteo_incidencias (0 if null)
incidenciaAbierta
boolean
Whether truck has an open incident (always false in queue)

Truck Type Mapping

The tipo_unidad field is normalized using this mapping:
Database ValueCodeType
”parihuelero”, “pariholero”, “pari-holero”PPari-holero
”jumbo”JJumbo
”bi-tren”, “bitren”BBi-tren
”tolva”TTolva
(any other)OOther

Time Conversion

The hora_llegada field (Postgres TIME format “HH:MM:SS”) is converted to a JavaScript timestamp:
function horaATimestamp(hora: string | null): number {
  if (!hora) return Date.now();
  const [h, m, s] = hora.split(':').map(Number);
  const d = new Date();
  d.setHours(h, m, s ?? 0, 0);
  return d.getTime();
}

Example Usage

import { fetchCamionesCola } from './services/supabaseService';

// Fetch all trucks in queue
const trucks = await fetchCamionesCola();

console.log(`${trucks.length} trucks in queue`);

trucks.forEach(truck => {
  console.log(`${truck.placa} - ${truck.tipoCodigo} - ${truck.producto}`);
});

Real-world Example

import { useEffect, useState } from 'react';
import { fetchCamionesCola } from './services/supabaseService';
import type { Camion } from './types';

function QueuePanel() {
  const [trucks, setTrucks] = useState<Camion[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function loadQueue() {
      const data = await fetchCamionesCola();
      setTrucks(data);
      setLoading(false);
    }
    loadQueue();
  }, []);

  if (loading) return <div>Loading queue...</div>;

  return (
    <div>
      <h2>Trucks in Queue: {trucks.length}</h2>
      {trucks.map(truck => (
        <div key={truck.id}>
          <span>{truck.placa}</span>
          <span>{truck.tipoCodigo}</span>
          <span>{truck.producto}</span>
          <span>Incidents: {truck.incidencias}</span>
        </div>
      ))}
    </div>
  );
}

Error Handling

The function returns an empty array on error and logs to console:
if (error) { 
  manejarError('fetchCamionesCola', error); 
  return []; 
}
if (!data?.length) return [];
No exceptions are thrown, making it safe to use in UI components without try-catch blocks.

Incident Operations

Query and manage incidents for queued trucks

Trip Operations

Move trucks from queue to bays

Build docs developers (and LLMs) love