All files / audit audit.service.ts

75% Statements 6/8
0% Branches 0/2
0% Functions 0/1
66.66% Lines 4/6

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 34 35 364x 4x 4x                                       4x                          
import { Injectable } from '@nestjs/common';
import { db } from '../db/client';
import { auditLog } from '../db/schema';
 
type Transaction = Parameters<Parameters<(typeof db)['transaction']>[0]>[0];
 
export interface AuditEntry {
  tenantId: string;
  actorId: string;
  action: string;
  resource: string;
  reason?: string;
  correlationId?: string;
}
 
/**
 * Blueprint 3.4: "Audit 100% pentru operatiuni materiale". Serviciile de domeniu
 * apeleaza explicit record() in aceeasi tranzactie cu scrierea de domeniu --
 * explicit in loc de interceptor magic, ca sa fie evident in code review ce
 * operatiuni sunt auditate si ce nu.
 */
@Injectable()
export class AuditService {
  async record(tx: Transaction | null, entry: AuditEntry): Promise<void> {
    const executor = tx ?? db;
    await executor.insert(auditLog).values({
      tenantId: entry.tenantId,
      actorId: entry.actorId,
      action: entry.action,
      resource: entry.resource,
      reason: entry.reason,
      correlationId: entry.correlationId,
    });
  }
}