feature/bitacora-correccion-por-tenant
This commit is contained in:
@@ -8,6 +8,7 @@ from sqlalchemy import event, inspect
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from api.v1.modules.a76.general_catalogs.company.models import Company
|
||||
from api.v1.modules.a76.invoices.models import InvoiceHeader
|
||||
from core.database import rls_company_var, rls_tenant_var
|
||||
|
||||
from .services.service import AuditService
|
||||
@@ -48,15 +49,39 @@ def _resolve_audit_company_tenant(session: Session, target) -> tuple:
|
||||
company_id / tenant_id desde la fila ORM, ContextVars RLS (petición HTTP),
|
||||
o ``Company.tenant_id`` por ``company_id``.
|
||||
"""
|
||||
resolution_source = "target"
|
||||
company_id = getattr(target, "company_id", None)
|
||||
if company_id is None and getattr(target, "__tablename__", None) == "company":
|
||||
company_id = getattr(target, "id", None)
|
||||
if company_id is not None:
|
||||
resolution_source = "company_self_id"
|
||||
if company_id is None:
|
||||
company_id = rls_company_var.get()
|
||||
if company_id is not None:
|
||||
resolution_source = "rls_context"
|
||||
|
||||
tenant_id = getattr(target, "tenant_id", None)
|
||||
if tenant_id is None:
|
||||
tenant_id = rls_tenant_var.get()
|
||||
if tenant_id is not None and resolution_source == "target":
|
||||
resolution_source = "rls_context"
|
||||
|
||||
# Common fallback for invoice child tables where invoice_id points to header scope.
|
||||
if (company_id is None or tenant_id is None) and hasattr(target, "invoice_id"):
|
||||
invoice_id = getattr(target, "invoice_id", None)
|
||||
if invoice_id is not None:
|
||||
invoice_scope = (
|
||||
session.query(InvoiceHeader.company_id, InvoiceHeader.tenant_id)
|
||||
.filter(InvoiceHeader.id == invoice_id, InvoiceHeader.deleted_at.is_(None))
|
||||
.first()
|
||||
)
|
||||
if invoice_scope:
|
||||
if company_id is None:
|
||||
company_id = invoice_scope[0]
|
||||
if tenant_id is None:
|
||||
tenant_id = invoice_scope[1]
|
||||
resolution_source = "invoice_header_lookup"
|
||||
|
||||
if tenant_id is None and company_id is not None:
|
||||
row = (
|
||||
session.query(Company.tenant_id)
|
||||
@@ -65,8 +90,9 @@ def _resolve_audit_company_tenant(session: Session, target) -> tuple:
|
||||
)
|
||||
if row:
|
||||
tenant_id = int(row[0])
|
||||
resolution_source = "company_lookup"
|
||||
|
||||
return company_id, tenant_id
|
||||
return company_id, tenant_id, resolution_source
|
||||
|
||||
|
||||
def after_insert_listener(mapper, connection, target):
|
||||
@@ -79,13 +105,17 @@ def after_insert_listener(mapper, connection, target):
|
||||
|
||||
session = Session(bind=connection)
|
||||
try:
|
||||
company_id, tenant_id = _resolve_audit_company_tenant(session, target)
|
||||
company_id, tenant_id, resolution_source = _resolve_audit_company_tenant(
|
||||
session, target
|
||||
)
|
||||
if company_id is None or tenant_id is None:
|
||||
logger.debug(
|
||||
"Audit skip INSERT %s: missing company_id=%s tenant_id=%s",
|
||||
logger.warning(
|
||||
"Audit skip INSERT table=%s record_id=%s company_id=%s tenant_id=%s source=%s",
|
||||
table_name,
|
||||
getattr(target, "id", None),
|
||||
company_id,
|
||||
tenant_id,
|
||||
resolution_source,
|
||||
)
|
||||
return
|
||||
|
||||
@@ -131,13 +161,17 @@ def after_update_listener(mapper, connection, target):
|
||||
|
||||
session = Session(bind=connection)
|
||||
try:
|
||||
company_id, tenant_id = _resolve_audit_company_tenant(session, target)
|
||||
company_id, tenant_id, resolution_source = _resolve_audit_company_tenant(
|
||||
session, target
|
||||
)
|
||||
if company_id is None or tenant_id is None:
|
||||
logger.debug(
|
||||
"Audit skip UPDATE %s: missing company_id=%s tenant_id=%s",
|
||||
logger.warning(
|
||||
"Audit skip UPDATE table=%s record_id=%s company_id=%s tenant_id=%s source=%s",
|
||||
table_name,
|
||||
getattr(target, "id", None),
|
||||
company_id,
|
||||
tenant_id,
|
||||
resolution_source,
|
||||
)
|
||||
return
|
||||
|
||||
@@ -169,13 +203,17 @@ def after_delete_listener(mapper, connection, target):
|
||||
|
||||
session = Session(bind=connection)
|
||||
try:
|
||||
company_id, tenant_id = _resolve_audit_company_tenant(session, target)
|
||||
company_id, tenant_id, resolution_source = _resolve_audit_company_tenant(
|
||||
session, target
|
||||
)
|
||||
if company_id is None or tenant_id is None:
|
||||
logger.debug(
|
||||
"Audit skip DELETE %s: missing company_id=%s tenant_id=%s",
|
||||
logger.warning(
|
||||
"Audit skip DELETE table=%s record_id=%s company_id=%s tenant_id=%s source=%s",
|
||||
table_name,
|
||||
getattr(target, "id", None),
|
||||
company_id,
|
||||
tenant_id,
|
||||
resolution_source,
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user