Skip to main content

Introduction

The supabaseService.ts module is the single source of truth for all data operations in Dashboard Backus. It provides a clean, type-safe interface to:
  • SELECT from Supabase views and tables (queue, dashboard panels)
  • INSERT incidents directly into the incidencias table
  • UPDATE trip records in viajes_camiones (bay assignment, status, departure time)
  • Realtime subscriptions via Postgres change events

Architecture

All data flows through this service to ensure consistency and maintainability. The service integrates with:
  • Tables: viajes_camiones, incidencias, usuarios
  • Views: vista_unidad_prioridad, vista_dashboard_turnos, vista_promedio_patio_neto
  • Types: Defined in src/types.ts for full TypeScript safety

Environment Configuration

Table and view names can be overridden via environment variables:
VITE_TABLE_VIAJES=viajes_camiones
VITE_TABLE_INCIDENCIAS=incidencias
VITE_VIEW_PRIORIDAD=vista_unidad_prioridad
VITE_VIEW_TURNOS=vista_dashboard_turnos
VITE_VIEW_PROMEDIO=vista_promedio_patio_neto

Function Categories

Queue Operations

Fetch and manage trucks in the queue

Incident Operations

Open, close, and query incidents

Trip Operations

Update bay assignments and mark departures

Dashboard Panels

Fetch priority units, shift counts, and average times

Core Types

UsuarioLogin

Authenticated user object returned by loginUsuario.
id
number
required
User primary key
email
string
required
User email address
rol
'admin' | 'cliente'
required
User role for authorization
nombre
string | null
required
User display name (may be null)

IncidenciaRow

Incident record structure.
id_incidencia
number
Primary key (auto-generated)
id_camion
number
required
Foreign key to viajes_camiones.id
hora_inicio
string
required
Start time in “HH:MM:SS” format (Postgres TIME)
hora_fin
string
End time in “HH:MM:SS” format (null if incident is still open)

Authentication

The loginUsuario function provides MD5-based authentication against the usuarios table, using MD5 hashing (RFC 1321) to match PostgreSQL’s md5() function. See the Trip Operations documentation for implementation details.

Error Handling

All functions use a centralized error handler that:
  • Logs errors to the console with context
  • Returns null, false, or empty arrays on failure
  • Never throws exceptions (safe for UI consumption)
function manejarError(contexto: string, error: unknown): null {
  const msg = error instanceof Error ? error.message : String(error);
  console.error(`[supabaseService] ${contexto}:`, msg);
  return null;
}

Utility Functions

intervalAMinutos

Converts PostgreSQL interval strings to minutes.
interval
string | null | undefined
Postgres interval (e.g., “02:30:00”, “1 day 02:30:00”, “-01:15:00”)
return
number
Total minutes (negative for intervals like night shift crossovers)
Supported formats:
  • "HH:MM:SS" - Standard format
  • "HH:MM:SS.mmm" - With milliseconds
  • "-HH:MM:SS" - Negative intervals
  • "1 day 02:30:00" - Multi-day intervals
  • "HH:MM:SS+00" - With timezone offset (stripped)
import { intervalAMinutos } from './services/supabaseService';

const minutes = intervalAMinutos("02:30:00"); // 150
const negative = intervalAMinutos("-00:15:00"); // -15
const multiDay = intervalAMinutos("1 day 12:00:00"); // 2160

intervalATexto

Formats PostgreSQL intervals as human-readable text.
interval
string | null | undefined
Postgres interval
return
string
Formatted string (“Xh Ym”, “Ym”, or ”< 1m”)
import { intervalATexto } from './services/supabaseService';

console.log(intervalATexto("02:30:00")); // "2h 30m"
console.log(intervalATexto("00:45:00")); // "45m"
console.log(intervalATexto("00:00:30")); // "< 1m"

Next Steps

Queue Operations

Learn how to fetch and manage the truck queue

Incident Operations

Understand incident lifecycle management

Build docs developers (and LLMs) love