Overview
CEDIS Pedidos uses PostgreSQL with Supabase, featuring 6 core tables with Row Level Security (RLS) policies. The schema supports multi-branch ordering with granular access control.Enums
categoria_enum
Material category types:materia_prima- Raw materials (40 items)esencia- Fragrances/essences (82 items)varios- Miscellaneous items (21 items)envase_vacio- Empty containers (14 items)color- Colors/pigments (11 items)
rol_enum
User role types:admin- Administrator with full system accesssucursal- Branch user with restricted access
estado_pedido
Order status workflow:borrador- Draft (editable by branch)enviado- Submitted (awaiting approval)aprobado- Approved (awaiting printing)impreso- Printed (order fulfilled)
Tables
sucursales
Branch/location information for the organization.Primary key, auto-generated with
gen_random_uuid()Full branch name (e.g., “Pachuca I”, “Guadalajara”)
Unique short code for the branch (e.g., “PAC1”, “GDL”, “CDMX”)Constraint: UNIQUE
City where the branch is located
Whether the branch is currently active
RLS Policy: All authenticated users can read all branches (
sucursales_select)Seed Data
users
User profiles linked to Supabase Auth with role-based access control.Primary key, references
auth.users(id) ON DELETE CASCADEUser’s full name
User’s email addressConstraint: UNIQUE
User role:
admin or sucursalForeign key to
sucursales(id) ON DELETE SET NULLRequired for sucursal role users, NULL for adminsAccount status:
pendiente, activo, or inactivoConstraint: CHECK IN (‘pendiente’,‘activo’,‘inactivo’)Flag for super administrator privileges
RLS Policies:
- Users can read their own profile
- Admins can read all profiles
- Users can insert/update their own profile
materiales
Catalog of all materials available for ordering (168 total items).Primary key, auto-generated with
gen_random_uuid()Optional material code (legacy identifier)Constraint: UNIQUE
Material name (e.g., “Aceite De Pino”, “Lavanda Francesa”)
Category:
materia_prima, esencia, varios, envase_vacio, or colorBase unit of measurement (e.g., “kgs”, “Lt”, “Pzas”, “Paquete”)
Approximate weight/quantity per container
Container description (e.g., “200lts”, “25kgs”, “Caja”)
Display order within category
RLS Policy: All authenticated users can read all materials (
materiales_select)Material Counts by Category
- Materias Primas: 40 items
- Esencias: 82 items
- Varios: 21 items
- Envases Vacíos: 14 items
- Colores: 11 items
- Total: 168 materials
pedidos
Order headers containing delivery date, status, and metadata.Primary key, auto-generated with
gen_random_uuid()Human-readable order code (e.g., “PAC1-2026-001”)Constraint: UNIQUE
Foreign key to
sucursales(id), identifies which branch placed the orderRequested delivery date
Total weight/quantity sum from
pedido_detalleBusiness rule: Frontend enforces 11,500 kg limit; database function validates < 13,000 kg as absolute maximumOrder status:
borrador, enviado, aprobado, or impresoOrder creation timestamp
Last update timestamp (auto-updated via trigger)
Timestamp when order was submitted (status changed to
enviado)Foreign key to
users(id), user who submitted the orderIndexes
Triggers
Auto-updateupdated_at:
RLS Policies: See Row Level Security
pedido_detalle
Order line items linking orders to materials with quantities.Primary key, auto-generated with
gen_random_uuid()Foreign key to
pedidos(id) ON DELETE CASCADEForeign key to
materiales(id)Quantity in kilograms (used for materials measured in kgs)
Requested quantity (used for piece-based materials)
Total weight for this line item
Batch/lot number (filled by admin during fulfillment)
Actual weight (filled by admin during fulfillment)
Indexes
RLS Policies: Access follows parent
pedidos table rules. See Row Level Securitysolicitudes_acceso
Access requests for new user registration (approval workflow).Primary key, auto-generated with
gen_random_uuid()Foreign key to
users(id) ON DELETE CASCADE (set after account creation)Requester’s full name
Requester’s email address
Foreign key to
sucursales(id), requested branch assignmentOptional message from requester
Request status:
pendiente, aprobado, or rechazadoConstraint: CHECK IN (‘pendiente’,‘aprobado’,‘rechazado’)Foreign key to
users(id), admin who reviewed the requestTimestamp when request was reviewed
Request creation timestamp
RLS Policies:
- Any authenticated user can insert
- Users can read their own requests
- Admins can read and update all requests
Database Functions
validate_pedido_limit(p_pedido_id uuid)
Validates that an order’s total weight is under 13,000 kg (absolute database maximum). Note: The frontend enforces a stricter 11,500 kg limit for operational safety. Returns:boolean
Usage:
Relationships
Key Relationships
- sucursales → users: One branch has many users (ON DELETE SET NULL)
- sucursales → pedidos: One branch has many orders
- pedidos → pedido_detalle: One order has many line items (ON DELETE CASCADE)
- materiales → pedido_detalle: One material appears in many line items
- auth.users → users: One-to-one with CASCADE delete
- users → solicitudes_acceso: User can have multiple access requests
TypeScript Interfaces
See/workspace/source/src/lib/types.ts for complete type definitions: