Overview
Branches (sucursales) are the organizational units in CEDIS Pedidos. Each branch can place orders, and users with thesucursal role are typically assigned to a specific branch location.
Branch data is used throughout the system for order routing, user assignments, and access control.
Branch Structure
Each branch contains the following information:src/lib/types.ts:22-28
Database Schema
supabase/schema.sql:18-24
Key Constraints
- Primary Key:
id(UUID) - Unique Constraint:
abreviacionmust be unique across all branches - Not Null: All fields except
activahave default values
Default Branches
The system ships with three pre-configured branches:- Abreviación: PAC1
- Ciudad: Pachuca
- Primary distribution center
- Abreviación: GDL
- Ciudad: Guadalajara
- Western region hub
- Abreviación: CDMX
- Ciudad: Ciudad de México
- Metropolitan area distribution
supabase/schema.sql:193-196
Branch Selection in Forms
Branches are loaded and filtered for active status:src/components/admin/SolicitudesPanel.tsx:50-54
User Interface
Branches appear in dropdown selectors:src/components/admin/SolicitudesPanel.tsx:474-482
Branch Assignment
User-to-Branch Linking
Users are linked to branches via thesucursal_id foreign key:
The
ON DELETE SET NULL constraint means if a branch is deleted, associated users will have their sucursal_id set to NULL rather than being deleted.Admin vs Sucursal Role
- Admin users: Typically have
sucursal_id = NULL(not tied to a specific branch) - Sucursal users: Must have a
sucursal_idassigned to operate
Filtering by Branch
The dashboard allows filtering orders by branch:src/pages/Dashboard.tsx:80-84
Filter Dropdown
src/pages/Dashboard.tsx:173-178
Active vs Inactive Branches
Theactiva boolean flag controls branch visibility:
- Active (
activa = true): Branch appears in dropdowns and can receive new orders - Inactive (
activa = false): Branch is hidden from most UI elements but historical data remains
Row-Level Security
All authenticated users can view branches:supabase/schema.sql:113
Currently, there are no policies for INSERT, UPDATE, or DELETE on the
sucursales table. These operations should be performed via direct SQL or Supabase Dashboard by super admins only.Managing Branches via SQL
Adding a New Branch
Deactivating a Branch
Updating Branch Details
Branch Display in Orders
Orders display branch information via joined queries:src/pages/Dashboard.tsx:46-49
Result structure:
src/pages/Dashboard.tsx:13-15
Access Control by Branch
Sucursal Users
Users withrol = 'sucursal' can only see orders from their assigned branch:
supabase/schema.sql:125-128
Admin Users
Admins can view and manage orders across all branches.Best Practices
- Unique Abbreviations: Keep abbreviations short (2-4 characters) and memorable
- Meaningful Names: Use full descriptive names (e.g., “CDMX Norte” instead of “Branch 3”)
- City Accuracy: Ensure ciudad field matches the actual geographic location
- Active Flag: Use the
activaflag instead of deleting branches - Avoid Orphaning: Before deactivating a branch, reassign or deactivate associated users
Related Data
Users Assigned to Branch
Query users by branch:Orders from Branch
Troubleshooting
Branch Not Appearing in Dropdowns
Cause: The branch may be inactive. Solution:Users Cannot Create Orders
Cause: User may not have asucursal_id assigned.
Solution: Assign the user to an active branch via user management interface.
Duplicate Abbreviation Error
Cause: Theabreviacion must be unique.
Solution: Choose a different abbreviation:
Related Sections
User Management
Assign users to branch locations
Access Requests
Review branch selection in access requests