import { AsyncLocalStorage } from 'async_hooks'; /** * Audit-Kontext für die Übertragung von Before/After-Werten * zwischen Prisma-Middleware und Audit-Middleware */ export interface AuditContext { before?: Record; after?: Record; } // AsyncLocalStorage für den Audit-Kontext const auditStorage = new AsyncLocalStorage(); /** * Startet einen neuen Audit-Kontext für einen Request */ export function runWithAuditContext(fn: () => T): T { return auditStorage.run({}, fn); } /** * Setzt die "Before"-Werte im aktuellen Kontext */ export function setBeforeValues(values: Record): void { const context = auditStorage.getStore(); if (context) { context.before = values; } } /** * Setzt die "After"-Werte im aktuellen Kontext */ export function setAfterValues(values: Record): void { const context = auditStorage.getStore(); if (context) { context.after = values; } } /** * Holt den aktuellen Audit-Kontext */ export function getAuditContext(): AuditContext | undefined { return auditStorage.getStore(); } /** * Express Middleware zum Initialisieren des Audit-Kontexts */ export function auditContextMiddleware( req: unknown, res: unknown, next: () => void ): void { runWithAuditContext(() => { next(); }); }