Overview
CEDIS Pedidos is a modern web application built for managing material orders across multiple branch offices (sucursales). The system uses a three-tier architecture with React frontend, Supabase backend, and PostgreSQL database.The application enforces role-based access control (RBAC) with two user roles: admin (CEDIS administrators) and sucursal (branch office users).
Technology Stack
Frontend Technologies
React & Build Tools
React & Build Tools
- React 19.0.0 - UI library with latest concurrent features
- React Router DOM 7.2.0 - Client-side routing and navigation
- Vite 6.2.0 - Fast build tool and dev server
- TypeScript 5.7.2 - Type-safe development
- Tailwind CSS 4.0.9 - Utility-first styling framework
UI Components & Libraries
UI Components & Libraries
- Radix UI - Accessible headless component primitives:
- Alert Dialog, Avatar, Dialog, Dropdown Menu
- Label, Progress, Select, Separator, Slot
- Toast notifications, Tooltips
- Lucide React 0.475.0 - Icon library
- class-variance-authority - Component variant management
- clsx & tailwind-merge - Conditional class composition
State Management & Utilities
State Management & Utilities
- Zustand 5.0.3 - Lightweight state management
- date-fns 4.1.0 - Date manipulation and formatting
- html2pdf.js 0.10.2 - PDF generation for order prints
Backend Technologies
CEDIS Pedidos uses Supabase as a Backend-as-a-Service (BaaS), providing:
- PostgreSQL database with Row Level Security (RLS)
- Authentication and user management
- Real-time subscriptions
- RESTful API and PostgREST
- @supabase/supabase-js 2.48.1 - Official Supabase client library
- PostgreSQL - Relational database (via Supabase)
- PostgREST - Automatic REST API generation
Application Architecture
Frontend Structure
The application follows a component-based architecture organized by feature:Routing Architecture
The application uses role-based routing defined insrc/App.tsx:1:
Public Routes
Public Routes
/login- Authentication page (no auth required)
Admin Routes (rol='admin')
Admin Routes (rol='admin')
/dashboard- Order management dashboard/catalogo- Material catalog administration/historial- Complete order history
Sucursal Routes (rol='sucursal')
Sucursal Routes (rol='sucursal')
/nuevo-pedido- Create new order/mis-pedidos- View branch’s orders/nuevo-pedido/:id- Edit existing order
Shared Routes (authenticated)
Shared Routes (authenticated)
Authentication Flow
The
ProtectedRoute component (src/components/ProtectedRoute) enforces authentication and role-based access at the route level.Database Architecture
Core Tables
The database consists of 6 main tables:sucursales - Branch Offices
sucursales - Branch Offices
Stores branch office information:
id(uuid, PK)nombre- Full branch nameabreviacion- Unique branch codeciudad- City locationactiva- Active status flag
users - User Profiles
users - User Profiles
Extends Supabase auth.users with application-specific data:
id(uuid, PK) - References auth.usersnombre,email,rol(admin/sucursal)sucursal_id- Foreign key to sucursalesestado_cuenta- Account status (pendiente/activo/inactivo)es_superadmin- Superadmin flag
materiales - Material Catalog
materiales - Material Catalog
168 materials organized in 5 categories:
id(uuid, PK)codigo- Material code (nullable)nombre- Material namecategoria- Enum: materia_prima, esencia, varios, envase_vacio, colorunidad_base- Unit of measurementpeso_aproximado- Approximate weightenvase- Container typeorden- Display orderactivo- Active status
pedidos - Orders
pedidos - Orders
Main order records:
id(uuid, PK)codigo_pedido- Unique order codesucursal_id- Foreign key to branchfecha_entrega- Delivery datetipo_entrega- Delivery type (HINO/Recolección en CEDIS)total_kilos- Total weight in kgestado- Status: borrador, enviado, aprobado, impresocreated_at,updated_at- Timestampsenviado_at,enviado_por- Submission tracking
pedido_detalle - Order Line Items
pedido_detalle - Order Line Items
Individual material quantities per order:
id(uuid, PK)pedido_id- Foreign key to pedidosmaterial_id- Foreign key to materialescantidad_kilos- Quantity in kgcantidad_solicitada- Requested quantitypeso_total- Total weightlote,peso- Batch and weight fields- UNIQUE constraint on (pedido_id, material_id)
solicitudes_acceso - Access Requests
solicitudes_acceso - Access Requests
User registration and approval workflow:
id(uuid, PK)user_id- Foreign key to users (nullable)nombre,email,sucursal_idmensaje- Request messageestado- Status: pendiente, aprobado, rechazadorevisado_por,revisado_at- Approval trackingcreated_at- Timestamp
Database Functions
The system includes a custom PostgreSQL function:validate_pedido_limit(p_pedido_id uuid) - Validates order weight limit
- Returns
boolean - Checks if order total_kilos < 13,000 kg (absolute database maximum; frontend enforces 11,500 kg)
- Defined as
SECURITY DEFINER(runs with elevated privileges) - Source:
supabase/schema.sql:97
Supabase Client Configuration
The Supabase client is initialized insrc/lib/supabase.ts:1:
Environment variables are loaded from
.env file:VITE_SUPABASE_URL- Supabase project URLVITE_SUPABASE_ANON_KEY- Anonymous/public API key
- Authentication:
supabase.auth- Sign in, sign out, session management - Database:
supabase.from('table')- Query builder for CRUD operations - Real-time:
supabase.channel()- Real-time subscriptions (if needed)
Build Configuration
Vite configuration fromvite.config.ts:1:
Build Scripts
Frompackage.json:6:
npm run dev- Start Vite dev servernpm run build- TypeScript check + production buildnpm run preview- Preview production buildnpm run lint- Run ESLint
Security Features
Row Level Security (RLS)
Row Level Security (RLS)
All tables use PostgreSQL RLS policies to enforce access control at the database level. See Security for complete policy documentation.
Authentication
Authentication
- Supabase Auth handles user authentication
- JWT tokens stored in localStorage
- Session persistence and refresh
- Account status validation (
estado_cuenta = 'activo')
Frontend Guards
Frontend Guards
ProtectedRoutecomponent wraps authenticated routesallowedRolesprop enforces role-based access- Automatic redirect to login for unauthenticated users
Data Flow Example: Creating an Order
Every database operation is validated by RLS policies before execution, ensuring users can only access their own branch’s data (or all data for admins).
Performance Considerations
Database Indexes
Optimized queries with indexes on frequently accessed columns:idx_pedidos_sucursal- Fast branch filteringidx_pedidos_estado- Fast status filteringidx_pedidos_fecha- Date range queriesidx_detalle_pedido- Order line item lookupsidx_detalle_material- Material usage queries
supabase/schema.sql:73
Frontend Optimizations
- Code splitting - React Router lazy loading (can be implemented)
- Zustand state - Minimal re-renders
- Vite dev server - Hot Module Replacement (HMR)
- Production build - Tree shaking, minification
Deployment Architecture
Frontend: Deployed as static site (Vite build output)Backend: Supabase cloud hostingDatabase: Managed PostgreSQL on Supabase infrastructure
- Vercel, Netlify, or any static hosting
- Docker container (with nginx)
- Traditional web server (Apache, Nginx)