Especificación Técnica: cat_estados_cobro¶
Esquema: fuerza_ventas
Base de Datos: crm
Servicio: svc-fuerza-ventas
Tabla Legacy Origen: co_estatus
Propósito: estados de workflow (catálogo, no ENUM)
1. Justificación y Mejoras de Arquitectura¶
- Normalización Relacional Rigurosa: Aplica diseño normalizado (1FN a BCNF), eliminando grupos repetidos de booleanos, desnormalizaciones innecesarias y redundancias del sistema legacy.
- Aislamiento Multi-Tenant (RLS): Integra
empresa_id BIGINT NOT NULLindexado, gobernado por políticas PostgreSQL Row-Level Security (USINGyWITH CHECK) para garantizar la total separación de datos por tenant. - Integridad Referencial Compuesta: Todas las claves foráneas internas hacia tablas con tenencia son compuestas
(empresa_id, <fk_id>), asegurando la integridad física en PostgreSQL. - Trazabilidad y Auditoría: Auditoría estándar (
created_at,updated_at,deleted_at,created_by,updated_by) con auditoría integral.
2. Definiciones de Implementación¶
Table fuerza_ventas.cat_estados_cobro {
id uuid [pk, default: `gen_random_uuid()`, note: 'Identificador único UUID']
empresa_id bigint [not null, note: 'Identificador BIGINT de la empresa en core.identidad (RLS)']
codigo varchar(30) [not null, note: 'Campo codigo']
nombre varchar(60) [not null, note: 'Campo nombre']
is_inicial boolean [not null, note: 'Campo is_inicial']
is_conciliado boolean [not null, note: 'Campo is_conciliado']
is_final boolean [not null, note: 'Campo is_final']
orden int [not null, note: 'Campo orden']
// 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, id) [unique, name: 'uq_cat_estados_cobro_empresa_id']
(empresa_id, codigo) [unique, name: 'uq_cat_estados_cobro_empresa_codigo']
(empresa_id, is_activo) [name: 'ix_cat_estados_cobro_empresa_activo']
}
}
CREATE TABLE IF NOT EXISTS fuerza_ventas.cat_estados_cobro (
id UUID NOT NULL DEFAULT gen_random_uuid(),
empresa_id BIGINT NOT NULL,
codigo VARCHAR(30) NOT NULL,
nombre VARCHAR(60) NOT NULL,
is_inicial BOOLEAN NOT NULL,
is_conciliado BOOLEAN NOT NULL,
is_final BOOLEAN NOT NULL,
orden INT 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 pk_cat_estados_cobro PRIMARY KEY (id),
CONSTRAINT uq_cat_estados_cobro_empresa_id UNIQUE (empresa_id, id),
CONSTRAINT uq_cat_estados_cobro_empresa_codigo UNIQUE (empresa_id, codigo)
);
CREATE INDEX IF NOT EXISTS ix_cat_estados_cobro_empresa_activo ON fuerza_ventas.cat_estados_cobro (empresa_id, is_activo);
ALTER TABLE fuerza_ventas.cat_estados_cobro ENABLE ROW LEVEL SECURITY;
CREATE POLICY rls_cat_estados_cobro_tenant_isolation ON fuerza_ventas.cat_estados_cobro
FOR ALL
USING (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT)
WITH CHECK (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT);