All files / src/infrastructure/audit audit-logger.ts

66.66% Statements 2/3
100% Branches 2/2
66.66% Functions 2/3
66.66% Lines 2/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                            21x     139x                              
import { PrismaClient } from '../../generated/prisma/client';
import { logger } from '../../shared/logging/logger';
 
export interface AuditLogEntry {
  userId: string;
  action: string;
  resourceType: string;
  resourceId: string;
  babyId?: string | null;
}
 
// Fire-and-forget: callers never await this on the request/response path — a slow or failing
// audit write must not delay or break the actual health-data mutation it's logging.
export class AuditLogger {
  constructor(private readonly prisma: PrismaClient) {}
 
  log(entry: AuditLogEntry): void {
    void this.prisma.auditLog
      .create({
        data: {
          userId: entry.userId,
          action: entry.action,
          resourceType: entry.resourceType,
          resourceId: entry.resourceId,
          babyId: entry.babyId ?? null,
        },
      })
      .catch((error: unknown) => {
        logger.error({ err: error, entry }, 'audit_log.write_failed');
      });
  }
}