# ERP Construccion - Sistema de Administracion de Obra e INFONAVIT Sistema ERP especializado para empresas de construccion de vivienda con integracion INFONAVIT. Arquitectura multi-tenant SaaS con Row Level Security (RLS). ## Estado del Proyecto | Campo | Valor | |-------|-------| | **Estado** | 🚧 En desarrollo (55%) | | **Version** | 0.2.0 | | **Modulos** | 18 (14 MAI + 3 MAE + 1 MAA) | | **DDL Schemas** | 7 (110 tablas) | | **Entidades Backend** | 30 | | **Services Backend** | 8 | --- ## Quick Start ### Prerequisitos - Node.js >= 18.0.0 - Docker & Docker Compose - PostgreSQL 15+ con PostGIS (incluido en docker-compose) ### Instalacion ```bash # Clonar repositorio cd apps/verticales/construccion # Copiar variables de entorno cp .env.example .env # Levantar servicios con Docker docker-compose up -d # O desarrollo local cd backend && npm install && npm run dev ``` ### URLs de Desarrollo | Servicio | URL | |----------|-----| | Backend API | http://localhost:3000 | | Frontend | http://localhost:5173 | | Adminer (DB) | http://localhost:8080 | | Mailhog | http://localhost:8025 | --- ## Arquitectura ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Frontend (React) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Budgets β”‚ β”‚Progress β”‚ β”‚Estimatesβ”‚ β”‚ HSE β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Backend (Express + TypeORM) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Auth Middleware (JWT + RLS) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Budgets β”‚ β”‚Progress β”‚ β”‚Estimatesβ”‚ β”‚ Auth β”‚ β”‚ β”‚ β”‚ Service β”‚ β”‚ Service β”‚ β”‚ Service β”‚ β”‚ Service β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ PostgreSQL 15 + PostGIS β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Row Level Security (tenant_id) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ auth β”‚ β”‚constru.β”‚ β”‚ hr β”‚ β”‚ hse β”‚ β”‚estim.β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Stack Tecnologico | Capa | Tecnologia | |------|------------| | **Backend** | Node.js 20, Express 4, TypeORM 0.3 | | **Frontend** | React 18, Vite 5, TypeScript 5 | | **Database** | PostgreSQL 15 + PostGIS | | **Cache** | Redis 7 | | **Auth** | JWT + Refresh Tokens | | **Multi-tenant** | RLS (Row Level Security) | --- ## Estructura del Proyecto ``` construccion/ β”œβ”€β”€ backend/ β”‚ └── src/ β”‚ β”œβ”€β”€ modules/ β”‚ β”‚ β”œβ”€β”€ auth/ # Autenticacion JWT β”‚ β”‚ β”œβ”€β”€ budgets/ # MAI-003 Presupuestos β”‚ β”‚ β”œβ”€β”€ progress/ # MAI-005 Control de Obra β”‚ β”‚ β”œβ”€β”€ estimates/ # MAI-008 Estimaciones β”‚ β”‚ β”œβ”€β”€ construction/ # MAI-002 Proyectos β”‚ β”‚ β”œβ”€β”€ hr/ # MAI-007 RRHH β”‚ β”‚ β”œβ”€β”€ hse/ # MAA-017 HSE β”‚ β”‚ └── core/ # Entidades base β”‚ └── shared/ β”‚ β”œβ”€β”€ constants/ # SSOT (schemas, rutas, enums) β”‚ └── services/ # BaseService multi-tenant β”œβ”€β”€ frontend/ β”‚ β”œβ”€β”€ web/ # App web React β”‚ └── mobile/ # App movil (futuro) β”œβ”€β”€ database/ β”‚ └── schemas/ # DDL por schema β”‚ β”œβ”€β”€ 01-construction-schema-ddl.sql # 24 tablas β”‚ β”œβ”€β”€ 02-hr-schema-ddl.sql # 8 tablas β”‚ β”œβ”€β”€ 03-hse-schema-ddl.sql # 58 tablas β”‚ β”œβ”€β”€ 04-estimates-schema-ddl.sql # 8 tablas β”‚ β”œβ”€β”€ 05-infonavit-schema-ddl.sql # 8 tablas β”‚ β”œβ”€β”€ 06-inventory-ext-schema-ddl.sql # 4 tablas β”‚ └── 07-purchase-ext-schema-ddl.sql # 5 tablas β”œβ”€β”€ docs/ # Documentacion completa β”œβ”€β”€ devops/ β”‚ └── scripts/ # Validacion SSOT β”œβ”€β”€ docker-compose.yml └── .env.example ``` --- ## Modulos Implementados ### MAI-003: Presupuestos y Costos βœ… ```typescript // Entidades - Concepto // Catalogo jerarquico de conceptos - Presupuesto // Presupuestos versionados - PresupuestoPartida // Lineas con calculo automatico // Services - ConceptoService // Arbol, busqueda - PresupuestoService // CRUD, versionamiento, aprobacion ``` ### MAI-005: Control de Obra βœ… ```typescript // Entidades - AvanceObra // Avances fisicos con workflow - FotoAvance // Evidencias con GPS - BitacoraObra // Bitacora diaria - ProgramaObra // Programa maestro - ProgramaActividad // WBS/Actividades // Services - AvanceObraService // Workflow captura->revision->aprobacion - BitacoraObraService // Entradas secuenciales ``` ### MAI-008: Estimaciones βœ… ```typescript // Entidades - Estimacion // Estimaciones periodicas - EstimacionConcepto // Lineas con acumulados - Generador // Numeros generadores - Anticipo // Anticipos con amortizacion - Amortizacion // Descuentos por estimacion - Retencion // Fondo de garantia - FondoGarantia // Acumulado por contrato - EstimacionWorkflow // Historial de estados // Services - EstimacionService // Workflow completo, calculo totales ``` ### Auth: Autenticacion JWT βœ… ```typescript // Funcionalidades - Login con email/password - Registro de usuarios - Refresh tokens - Logout (revocacion) - Middleware de autorizacion por roles - Configuracion RLS por tenant ``` --- ## API Endpoints ### Autenticacion ```http POST /api/v1/auth/login POST /api/v1/auth/register POST /api/v1/auth/refresh POST /api/v1/auth/logout POST /api/v1/auth/change-password ``` ### Presupuestos ```http GET /api/v1/conceptos GET /api/v1/conceptos/:id POST /api/v1/conceptos GET /api/v1/conceptos/tree GET /api/v1/presupuestos GET /api/v1/presupuestos/:id POST /api/v1/presupuestos POST /api/v1/presupuestos/:id/partidas POST /api/v1/presupuestos/:id/approve POST /api/v1/presupuestos/:id/version ``` ### Control de Obra ```http GET /api/v1/avances GET /api/v1/avances/:id POST /api/v1/avances POST /api/v1/avances/:id/fotos POST /api/v1/avances/:id/review POST /api/v1/avances/:id/approve GET /api/v1/bitacora/:fraccionamientoId POST /api/v1/bitacora ``` ### Estimaciones ```http GET /api/v1/estimaciones GET /api/v1/estimaciones/:id POST /api/v1/estimaciones POST /api/v1/estimaciones/:id/conceptos POST /api/v1/estimaciones/:id/submit POST /api/v1/estimaciones/:id/review POST /api/v1/estimaciones/:id/approve ``` --- ## Base de Datos ### Schemas (7 total, 110 tablas) | Schema | Tablas | Descripcion | |--------|--------|-------------| | `auth` | 10 | Usuarios, roles, permisos, tenants | | `construction` | 24 | Proyectos, lotes, presupuestos, avances | | `hr` | 8 | Empleados, asistencias, cuadrillas | | `hse` | 58 | Incidentes, capacitaciones, EPP, STPS | | `estimates` | 8 | Estimaciones, anticipos, retenciones | | `infonavit` | 8 | Registro RUV, derechohabientes | | `inventory` | 4 | Almacenes, requisiciones | ### Row Level Security ```sql -- Todas las tablas tienen RLS activado ALTER TABLE construction.fraccionamientos ENABLE ROW LEVEL SECURITY; -- Politica de aislamiento por tenant CREATE POLICY tenant_isolation ON construction.fraccionamientos FOR ALL USING (tenant_id = current_setting('app.current_tenant_id')::UUID); ``` --- ## Scripts NPM ```bash # Backend npm run dev # Desarrollo con hot-reload npm run build # Compilar TypeScript npm run start # Produccion npm run lint # ESLint npm run test # Jest tests npm run validate:constants # Validar SSOT npm run sync:enums # Sincronizar enums a frontend # Docker docker-compose up -d # Levantar servicios docker-compose --profile dev up # Con Adminer y Mailhog docker-compose down # Detener servicios ``` --- ## Variables de Entorno ```bash # Application NODE_ENV=development APP_PORT=3000 # Database DB_HOST=localhost DB_PORT=5432 DB_USER=construccion DB_PASSWORD=construccion_dev_2024 DB_NAME=erp_construccion # JWT JWT_SECRET=your-secret-key-min-32-chars JWT_EXPIRES_IN=1d JWT_REFRESH_EXPIRES_IN=7d # Redis REDIS_HOST=localhost REDIS_PORT=6379 ``` Ver `.env.example` para la lista completa. --- ## Documentacion | Documento | Ubicacion | |-----------|-----------| | Estado del Proyecto | `PROJECT-STATUS.md` | | Mapa de Base de Datos | `database/_MAP.md` | | Constantes SSOT | `backend/src/shared/constants/` | | Modulos (18) | `docs/02-definicion-modulos/` | | Requerimientos (87 RF) | `docs/03-requerimientos/` | | User Stories (149 US) | `docs/05-user-stories/` | | ADRs (12) | `docs/97-adr/` | --- ## Proximos Pasos 1. **Corto Plazo** - Controllers REST para modulos nuevos - Backend MAI-009 (Calidad y Postventa) - Backend MAI-011 (INFONAVIT) - Testing de modulos existentes 2. **Mediano Plazo** - Frontend: Integracion con API - Modulos de Presupuestos y Estimaciones - Curva S y reportes de avance --- ## Licencia UNLICENSED - Proyecto privado --- **Ultima actualizacion:** 2025-12-12