workspace-v1/projects/erp-mecanicas-diesel/docs/03-modelo-datos
rckrdmrd 66161b1566 feat: Workspace-v1 complete migration with NEXUS v3.4
Sistema NEXUS v3.4 migrado con:

Estructura principal:
- core/orchestration: Sistema SIMCO + CAPVED (27 directivas, 28 perfiles)
- core/catalog: Catalogo de funcionalidades reutilizables
- shared/knowledge-base: Base de conocimiento compartida
- devtools/scripts: Herramientas de desarrollo
- control-plane/registries: Control de servicios y CI/CD
- orchestration/: Configuracion de orquestacion de agentes

Proyectos incluidos (11):
- gamilit (submodule -> GitHub)
- trading-platform (OrbiquanTIA)
- erp-suite con 5 verticales:
  - erp-core, construccion, vidrio-templado
  - mecanicas-diesel, retail, clinicas
- betting-analytics
- inmobiliaria-analytics
- platform_marketing_content
- pos-micro, erp-basico

Configuracion:
- .gitignore completo para Node.js/Python/Docker
- gamilit como submodule (git@github.com:rckrdmrd/gamilit-workspace.git)
- Sistema de puertos estandarizado (3005-3199)

Generated with NEXUS v3.4 Migration System
EPIC-010: Configuracion Git y Repositorios
2026-01-04 03:37:42 -06:00
..
INTEGRACION-ERP-CORE.md feat: Workspace-v1 complete migration with NEXUS v3.4 2026-01-04 03:37:42 -06:00
README.md feat: Workspace-v1 complete migration with NEXUS v3.4 2026-01-04 03:37:42 -06:00
SCHEMA-PARTS-MANAGEMENT.md feat: Workspace-v1 complete migration with NEXUS v3.4 2026-01-04 03:37:42 -06:00
SCHEMA-SERVICE-MANAGEMENT.md feat: Workspace-v1 complete migration with NEXUS v3.4 2026-01-04 03:37:42 -06:00
SCHEMA-VEHICLE-MANAGEMENT.md feat: Workspace-v1 complete migration with NEXUS v3.4 2026-01-04 03:37:42 -06:00

Modelo de Datos - Mecanicas Diesel

Vision General

El modelo de datos esta organizado en 3 schemas PostgreSQL propios, mas la integracion con erp-core para autenticacion y tenants. Implementa arquitectura multi-tenant con Row-Level Security (RLS) completo.

Arquitectura de Schemas

┌─────────────────────────────────────────────────────────────────────┐
│                    erp-core (heredado)                               │
├─────────────────────────────────────────────────────────────────────┤
│  auth              │  Usuarios, sesiones, tokens                    │
│  core              │  Tenants, partners, configuracion              │
├─────────────────────────────────────────────────────────────────────┤
│                    mecanicas-diesel (propio)                         │
├─────────────────────────────────────────────────────────────────────┤
│  service_management│  Ordenes, diagnosticos, cotizaciones (~18 tab) │
│  parts_management  │  Inventario, refacciones (~12 tablas)          │
│  vehicle_management│  Vehiculos, flotas, motores (~8 tablas)        │
└─────────────────────────────────────────────────────────────────────┘

Documentos

Schemas Propios

Integracion

Row-Level Security (RLS)

Todas las tablas con datos de tenant implementan RLS completo:

-- Funcion macro que genera politicas SELECT, INSERT, UPDATE, DELETE
SELECT create_tenant_rls_policies('service_management', 'service_orders');

-- Equivale a:
-- CREATE POLICY ... FOR SELECT USING (tenant_id = get_current_tenant_id());
-- CREATE POLICY ... FOR INSERT WITH CHECK (tenant_id = get_current_tenant_id());
-- CREATE POLICY ... FOR UPDATE USING (...) WITH CHECK (...);
-- CREATE POLICY ... FOR DELETE USING (...);

Convenciones

Nombres de Tablas

  • Plural en ingles: service_orders, vehicles
  • Snake_case: order_items, inventory_movements
  • Prefijo de schema implicito

Columnas Estandar

id              UUID PRIMARY KEY DEFAULT gen_random_uuid()
tenant_id       UUID NOT NULL  -- Referencia a core.tenants
created_at      TIMESTAMP WITH TIME ZONE DEFAULT NOW()
updated_at      TIMESTAMP WITH TIME ZONE DEFAULT NOW()
created_by      UUID  -- Referencia a auth.users

CHECK Constraints

Todos los campos de tipo enum usan CHECK constraints:

status VARCHAR(20) DEFAULT 'pending'
    CHECK (status IN ('pending', 'in_progress', 'completed', 'cancelled'))

Soft Delete (donde aplique)

deleted_at      TIMESTAMP WITH TIME ZONE
deleted_by      UUID

Diagrama ER Simplificado

erp-core
   │
   ├── core.tenants ────────────────────────┐
   │                                         │
   ├── core.partners ───────┐               │
   │                        │               │
   └── auth.users ─────────┐│               │
                           ││               │
mecanicas-diesel           ││               │
                           ▼▼               ▼
   vehicle_management.vehicles ◄── tenant_id
          │
          ├── vehicle_engines
          │
          └── fleets
                   │
   service_management.service_orders ◄───────┘
          │
          ├── order_items
          ├── diagnostics ── diagnostic_items
          └── quotes ─────── quote_items
                   │
   parts_management.parts
          │
          ├── part_locations
          ├── inventory_movements
          └── stock_alerts

Scripts de Inicializacion

Orden de ejecucion en /database/init/:

  1. 00-extensions.sql - uuid-ossp, pgcrypto
  2. 01-create-schemas.sql - Schemas propios
  3. 02-rls-functions.sql - Funciones RLS
  4. 03-service-management-tables.sql
  5. 04-parts-management-tables.sql
  6. 05-vehicle-management-tables.sql
  7. 06-seed-data.sql - Datos iniciales (motores, tipos de prueba)

Creado por: Requirements-Analyst Fecha: 2025-12-06 Actualizado: 2025-12-06 (Alineacion con erp-core, RLS completo)