Skip to main content

Audit Service — Application Logic

Status: populated Owner: TBD Last updated: 2026-04-18 Companion: Service Template · 02 DDD

1. Use Cases (Commands)

IDUse CaseTriggerActor
UC-AUDIT-01Ingest eventNATS message receivedSystem (NATS consumer)
UC-AUDIT-02Request exportPOST /api/v1/audit/exportsSuper Admin
UC-AUDIT-03Process export jobExport worker tickSystem (background worker)

2. Use Cases (Queries)

IDQueryTriggerActor
UC-AUDIT-04Query audit entriesGET /api/v1/audit/entriesTenant Admin / Super Admin
UC-AUDIT-05Get single entryGET /api/v1/audit/entries/:idTenant Admin / Super Admin
UC-AUDIT-06Accounting-of-disclosuresGET /api/v1/audit/disclosuresTenant Admin / Super Admin
UC-AUDIT-07Check export statusGET /api/v1/audit/exports/:idSuper Admin

3. Orchestration Flows

3.1 Ingest Event (UC-AUDIT-01)

3.2 Request Export (UC-AUDIT-02)

3.3 Process Export Job (UC-AUDIT-03)

The export worker runs on a configurable polling interval (default 30 s):

  1. Query audit_exports for status = 'queued'; claim one job (UPDATE with optimistic lock).
  2. Set status to processing.
  3. Cursor-stream matching audit_entries rows from PostgreSQL.
  4. Write NDJSON or CSV to object storage under exports/{tenantId}/{exportId}.{format}.
  5. Generate pre-signed URL (TTL 24 h).
  6. Update audit_exports: status = completed, fileUrl, recordCount, completedAt.
  7. Publish audit.export.completed.

4. Ports

Port nameTypeDirectionAdapter
AuditEntriesRepositoryportoutboundpostgres-audit-entries.adapter.ts
AuditExportsRepositoryportoutboundpostgres-audit-exports.adapter.ts
EventPublisherportoutboundnats-event-publisher.adapter.ts
ObjectStorageportoutbounds3-compatible-storage.adapter.ts
EventSubscriberportinboundnats-jetstream.adapter.ts

5. Background Jobs

JobScheduleDescription
Export workerPoll every 30 sPicks up queued exports and processes them
Chain integrity verifierDaily at 02:00 UTCReads audit_entries in order and verifies each chain hash; alerts on mismatch

6. Error Handling

ScenarioHandling
NATS delivery fails (network)NATS JetStream retries automatically
DB INSERT fails (transient)NACK the NATS message; NATS will redeliver with backoff: [1s, 5s, 30s, 2min, 10min]
DB INSERT fails 5 timesMessage moves to audit.dlq; audit.dlq.alert event emitted; platform-admin notified
Duplicate source_event_idIdempotently ACK'd; no duplicate entry created
Export worker failsSet status = failed; log error with exportId; surface in Grafana alert
Chain verification mismatchEmit CRITICAL log span; page on-call via Alertmanager