Database Architecture
The application follows a single source of truth pattern implemented insrc/services/supabaseService.ts:
- SELECT: Reads from views and tables
- INSERT: Creates incident records in
incidencias - UPDATE: Updates trip status in
viajes_camiones - Realtime: Subscribes to
postgres_changesfor live UI updates
Core Tables
usuarios
Stores user accounts with role-based access control.- Passwords are stored as MD5 hashes (matching PostgreSQL
md5()function) - Row Level Security enabled (see
supabase_usuarios.sql:23) - Two roles:
admin(can modify semaphore times) andcliente(read-only times)
viajes_camiones
Tracks truck trips from arrival to departure. Key Columns:id(SERIAL PRIMARY KEY) - Internal database IDid_viaje(TEXT UNIQUE) - Business trip identifier (e.g., “VJ-2024-001”)tracto- License platefrotcom- Fleet management ID (optional)propietario- Owner companyfecha- Date of arrivalhora_llegada(TIME) - Arrival timehora_salida(TIME) - Departure time (null while in yard)tipo_unidad- Truck type: “parihuelero”, “jumbo”, “bi-tren”, “tolva”, or “otros”operacion- “Carga” or “Descarga”tipo_carga/tipo_descarga- Product type being loaded/unloadedbahia_actual- Currently assigned bay (e.g., “b1”, “b2”)estado- Trip status: “En cola”, “Cargando”, “Descargando”, “Finalizado”conteo_incidencias- Number of registered incidents (max 3)
src/services/supabaseService.ts:73-120 for the data mapping logic.
incidencias
Records incidents that pause truck processing time. Schema:id_incidencia(SERIAL PRIMARY KEY)id_camion(INTEGER) - Foreign key toviajes_camiones.idhora_inicio(TIME) - Incident start timehora_fin(TIME) - Incident end time (null if still open)duracion_calculada(INTERVAL) - Auto-calculated duration by PostgreSQL
- Maximum 3 incidents per truck
- 4th incident triggers critical alert to developers
- Open incidents have
hora_fin IS NULL - Incident time is excluded from net yard time calculations
src/services/supabaseService.ts:251-285 for incident management.
configuracion_roles
Stores semaphore time thresholds per role. Purpose:- Admin users can modify yellow/red time thresholds
- Client users view thresholds in read-only mode
- Green: ≤ 60 minutes
- Yellow: 61-120 minutes
- Red: ≥ 121 minutes
Database Views
Views provide aggregated, read-only data for dashboard panels.vista_unidad_prioridad
Returns the truck with the highest priority (longest wait time). Columns:tracto- License platehora_llegada- Arrival timebahia_actual- Current bay assignment (or null if in queue)tiempo_transcurrido- Time elapsed since arrival (PostgreSQL interval)
- Filters:
estado ≠ 'Finalizado' - Orders by:
hora_llegada ASC - Limit: 1
src/services/supabaseService.ts:127-136.
vista_dashboard_turnos
Counts finalized trucks by shift and date. Columns:fecha- Date (YYYY-MM-DD)turno_1- Count for shift 1 (07:00-15:00)turno_2- Count for shift 2 (15:01-23:00)turno_3- Count for shift 3 (23:01-06:59)
src/services/supabaseService.ts:144-154.
vista_promedio_patio_neto
Calculates average net yard time (excluding incident durations). Columns:promedio_neto_patio- Average net time as PostgreSQL interval (“HH:MM:SS”)
intervalAMinutos() helper (see src/services/supabaseService.ts:182-211) converts PostgreSQL intervals to minutes, handling:
- Standard format: “HH:MM:SS”
- Milliseconds: “HH:MM:SS.mmm”
- Negative values: “-HH:MM:SS” (for overnight shifts)
- Multi-day: “1 day 02:30:00”
- Timezone offsets: “HH:MM:SS+00”
src/services/supabaseService.ts:162-170.
Row Level Security
Theusuarios table has RLS enabled with a policy that allows anonymous reads of active users:
activo flag.
Installation Script
Run the SQL setup script in your Supabase SQL Editor:Open Supabase Dashboard
Navigate to your project at app.supabase.com
Data Flow
Realtime Subscriptions
The application subscribes to table changes for live updates:Next Steps
Environment Variables
Configure Supabase connection strings
Webhooks
Set up n8n webhook automation
