Saltar a contenido

Especificación Técnica: rel_empresa_aplicacion

Esquema: core.identidad
Base de Datos: core
Servicio: svc-identidad
Propósito: Relación N:M entre empresas y aplicaciones activadas en el núcleo.


1. Justificación y Mejoras de Arquitectura

  • Controla qué empresas tienen habilitada cada aplicación.
  • Unique (empresa_id, aplicacion_id) impide activaciones duplicadas.
  • RLS por empresa actual al ser una configuración por empresa.

2. Definiciones de Implementación

Table core.identidad.rel_empresa_aplicacion {
    id                         uuid          [pk, default: `gen_random_uuid()`]
    empresa_id                 bigint        [not null, ref: > mae_empresas.id]
    aplicacion_id              uuid          [not null, ref: > cat_aplicaciones.id]

    // Auditoría General
    is_activo               boolean       [not null, default: `true`, note: 'Estado lógico del registro']
    created_at              timestamptz   [not null, default: `now()`, note: 'Fecha de creación']
    updated_at              timestamptz   [not null, default: `now()`, note: 'Fecha de última actualización']
    deleted_at              timestamptz   [note: 'Fecha de eliminación lógica']
    created_by              uuid          [note: 'Usuario creador']
    updated_by              uuid          [note: 'Usuario modificador']

    Indexes {
        (empresa_id, aplicacion_id) [unique, name: 'uq_rel_empresa_aplicacion']
    }

    // RLS por empresa actual
}
CREATE TABLE IF NOT EXISTS core.identidad.rel_empresa_aplicacion (
    id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
    empresa_id BIGINT NOT NULL,
    aplicacion_id UUID NOT NULL,

    -- Auditoría General
    is_activo BOOLEAN NOT NULL DEFAULT true,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    deleted_at TIMESTAMPTZ,
    created_by UUID,
    updated_by UUID,
    CONSTRAINT uq_rel_empresa_aplicacion UNIQUE (empresa_id, aplicacion_id),
    CONSTRAINT fk_rel_empresa_aplicacion_empresa FOREIGN KEY (empresa_id) REFERENCES core.identidad.mae_empresas (id),
    CONSTRAINT fk_rel_empresa_aplicacion_aplicacion FOREIGN KEY (aplicacion_id) REFERENCES core.identidad.cat_aplicaciones (id)
);

-- Row-Level Security
ALTER TABLE core.identidad.rel_empresa_aplicacion ENABLE ROW LEVEL SECURITY;
CREATE POLICY aislamiento_empresa ON core.identidad.rel_empresa_aplicacion
    FOR ALL
    USING (empresa_id = NULLIF(current_setting('app.current_empresa_id', true), '')::BIGINT);

-- Comentarios
COMMENT ON COLUMN core.identidad.rel_empresa_aplicacion.empresa_id IS 'Empresa';
COMMENT ON COLUMN core.identidad.rel_empresa_aplicacion.aplicacion_id IS 'Aplicación';