Database Information
The system uses PostgreSQL with the schemadb_ambiotec for all application data. The database architecture follows a normalized design with clear separation of concerns.
Core Schema Structure
Primary Schema
- pgcrypto: For UUID generation and cryptographic functions
- postgis: For geographic data types and spatial operations
Main Entity Groups
The database is organized into several logical domains:User Management
Users & Authentication
Users & Authentication
Core user management including authentication, roles, and permissions.Key Tables:
users- User accounts and profile informationroles- System roles (admin, vendedor, gerente, etc.)user_roles- Many-to-many relationship between users and rolesuser_status- User account status (active, inactive, suspended)user_access_log- Login/logout tracking with device informationuser_api_usage- API endpoint usage tracking
Permissions System
Permissions System
Granular module and action-based permission system.Key Tables:
modules- Application modules (clientes, ventas, chat, etc.)module_actions- Actions within each moduleuser_module_permissions- Per-user module accessuser_action_permissions- Per-user action permissionsrole_module_permissions- Default module permissions per rolepermission_versions- Cache versioning for permission changes
Customer Management
Customer Core
Customer Core
Normalized customer data structure supporting multiple addresses, contacts, and documents.Key Tables:
customers- Master customer table with commercial entity datacustomer_addresses- Multiple addresses per customer (fiscal, billing, service, shipping)customer_contacts- Contact persons with roles (primary, billing, purchasing, site)customer_documents- Document storage with expiration tracking (DPI, RTU, contracts)company_name- Multiple legal entities (razones sociales) per customer
Contact Management
Contact Management
Flexible contact information supporting multiple phone numbers and emails.Key Tables:
contact_phones- Multiple phone numbers per contactcontact_emails- Multiple email addresses per contactcontact_logs- Interaction history with customers
Service Requests
Request Core
Request Core
Service request management with type-specific data and workflow.Key Tables:
service_requests- Main service request tableservice_type- Types of services (sanitarios, fosas, PTAR, etc.)service_requests_status- Request workflow statesservice_requests_addresses- Geocoded service location addressesrequest_sanitarios- Sanitarios-specific data (rental details)request_fosas- Fosas-specific data (water type, volume)request_ptar- PTAR-specific data (treatment plant design)request_others- Generic service requests
Request Workflow
Request Workflow
Complete tracking of request lifecycle from lead to completion.Key Tables:
service_request_logs- Detailed change historyservice_request_status_log- Status change trackingservice_request_state_timeline- SLA tracking with policy deadlinesservice_request_assignments- Vendor assignment historyservice_request_manager_review- Manager approval process
Quotations & Acceptance
Quotations & Acceptance
Quotation generation and customer acceptance workflow.Key Tables:
service_request_quotations- Generated quotations with Drive/S3 linksquotation_followups- Sales followup trackingquotation_acceptances- Formal acceptance with customer data snapshotservice_request_media- Attached files and media
Products & Catalog
Product Management
Product Management
Product catalog with automatic SKU generation and pricing history.Key Tables:
product_categories- Hierarchical product categories with SKU prefixesproducts- Master product catalog with pricingrequest_products- Products associated with service requestsproduct_price_history- Audit trail of price changes
Payments
Payment Processing
Payment Processing
Payment tracking with OCR support and validation workflow.Key Tables:
payments- Main payment records with OCR metadatapayment_status_history- Status change audit trailpayment_logs- Detailed operation logsbanks- Bank catalog for Guatemala
Notifications
Notification System
Notification System
Multi-channel notification system (web, push, email).Key Tables:
notifications- Core notification recordsnotification_channels- Delivery channels (web, push, email)notification_deliveries- Per-channel delivery trackinguser_device_tokens- Device registration for push notificationsnotification_groups- Predefined notification recipient groupsnotification_group_members- Group membership
Geographic Data
Location Management
Location Management
Guatemala-specific geographic hierarchy.Key Tables:
departments- Departments (departamentos)municipio- Municipalities within departmentszona- Zones for address specification
Supporting Tables
File Storage
File Storage
S3-backed file storage with metadata.Key Tables:
storage_files- File metadata and S3 referencesservice_request_media- Request-specific media filesform_ptar_service_files- PTAR form attachments
Reporting & Incidents
Reporting & Incidents
System monitoring and incident tracking.Key Tables:
extortion_incidents- Security incident trackingreported_system_cases- Bug/error reports from public pagesassignment_notifications- Assignment event tracking
Design Principles
Normalization
The schema follows database normalization principles to reduce redundancy:- Customer data is separated into multiple related tables
- Contact information uses dedicated tables for phones and emails
- Address data is normalized with geographic hierarchies
Audit Trails
Most core tables include audit tracking:- Change logs for critical data (service requests, payments, etc.)
- Status history tables for workflow tracking
- Timestamp fields (
created_at,updated_at) on all major tables
Soft Deletes
Many tables use flags instead of physical deletion:is_blockedfor users and customersis_activefor contacts, addresses, and documents- Preserves historical data and relationships
JSONB Metadata
Flexible metadata storage using PostgreSQL JSONB:metadatacolumns for extensible data without schema changesocr_datafor unstructured OCR resultsextra_datafor logging and tracking
Geographic Support
PostGIS integration for location-based features:geography(POINT, 4326)for address coordinates- Spatial indexing for distance-based queries
Naming Conventions
- Tables: Lowercase with underscores, prefixed with entity domain
- Primary Keys:
{table}_id(e.g.,customer_id,request_id) - Foreign Keys: Match referenced primary key name
- Boolean Fields: Prefixed with
is_orhas_ - Timestamp Fields: Suffixed with
_at(e.g.,created_at,updated_at) - Stored Procedures: Prefixed with
fn_for functions,trg_for triggers
Next Steps
Tables Reference
Detailed table schemas and relationships
Stored Procedures
Database functions and procedures
Migrations
Database migration strategy
Seeding Data
Initial data and catalogs