# MGN-004: Multi-tenant ## Metadata | Campo | Valor | |-------|-------| | **ID** | MGN-004 | | **Nombre** | Multi-tenant | | **Fase** | 01 - Foundation | | **Prioridad** | P0 (Critico) | | **Story Points** | 35 SP | | **Estado** | Documentado | | **Dependencias** | MGN-001, MGN-002, MGN-003 | --- ## Descripcion Sistema de multi-tenancy que permite que multiples organizaciones/empresas utilicen la misma instancia del ERP con completo aislamiento de datos. Caracteristicas principales: - **Aislamiento RLS:** Row Level Security en PostgreSQL - **Configuracion por tenant:** Moneda, zona horaria, formato de fecha - **Planes de suscripcion:** Diferentes niveles de servicio - **Limites por plan:** Usuarios, almacenamiento, modulos - **Onboarding automatizado:** Creacion de tenant con datos iniciales --- ## Arquitectura ``` Request │ ▼ ┌──────────────────────┐ │ TenantMiddleware │ <- Extrae tenant_id del token JWT └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ SET app.tenant_id │ <- Establece contexto PostgreSQL └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ RLS Policies │ <- Filtra automaticamente │ tenant_isolation │ por tenant_id └──────────────────────┘ ``` --- ## Planes de Suscripcion | Plan | Usuarios | Almacenamiento | Modulos | |------|----------|----------------|---------| | Free | 3 | 1 GB | Core | | Starter | 10 | 5 GB | Core + Ventas | | Professional | 50 | 25 GB | Todos | | Enterprise | Ilimitado | Ilimitado | Todos + Soporte | --- ## Endpoints API | Metodo | Path | Descripcion | |--------|------|-------------| | GET | `/api/v1/tenants` | Listar tenants (super admin) | | POST | `/api/v1/tenants` | Crear tenant | | GET | `/api/v1/tenants/:id` | Obtener tenant | | PATCH | `/api/v1/tenants/:id` | Actualizar tenant | | GET | `/api/v1/tenants/current` | Tenant actual | | GET | `/api/v1/tenants/current/settings` | Config del tenant | | PATCH | `/api/v1/tenants/current/settings` | Actualizar config | | GET | `/api/v1/plans` | Listar planes | | POST | `/api/v1/subscriptions` | Crear suscripcion | | GET | `/api/v1/subscriptions/current` | Suscripcion actual | --- ## Tablas de Base de Datos | Tabla | Descripcion | |-------|-------------| | `tenants` | Organizaciones/empresas | | `tenant_settings` | Configuracion por tenant | | `plans` | Planes de suscripcion | | `subscriptions` | Suscripciones activas | --- ## Documentacion - **Mapa del modulo:** [_MAP.md](./_MAP.md) - **Trazabilidad:** [TRACEABILITY.yml](./implementacion/TRACEABILITY.yml) --- **Generado por:** Requirements-Analyst **Fecha:** 2025-12-05