import type { PoolClient } from "pg"; export interface AuditEntry { businessId: number | null; actorUserId: number | null; entity: string; entityId: number | null; action: string; before?: unknown; after?: unknown; ip?: string | null; } /** * Escribe una fila de auditoría **con el cliente de la transacción en curso**. * Recibe el `PoolClient` a propósito y no usa el pool por su cuenta: si el * cambio se revierte, su rastro tiene que revertirse con él. Una auditoría que * registra cambios que no ocurrieron es peor que no tener auditoría. */ export async function writeAudit(c: PoolClient, e: AuditEntry): Promise { await c.query( `INSERT INTO audit_log (business_id, actor_user_id, entity, entity_id, action, before, after, ip) VALUES ($1,$2,$3,$4,$5,$6::jsonb,$7::jsonb,$8)`, [ e.businessId, e.actorUserId, e.entity, e.entityId, e.action, e.before === undefined ? null : JSON.stringify(e.before), e.after === undefined ? null : JSON.stringify(e.after), e.ip ?? null, ] ); }