20 — Audit

Audit logging, compliance, tracking

Markdown source — 20-AUDIT.md

CEO Summary

Audit is the compliance backbone. Every critical action (login, payment, config change, data access) is logged. Business value: Regulatory compliance, dispute resolution, security forensics. Key metrics: Audit coverage, retention, query performance. If this breaks: Compliance gaps; cannot trace who did what.

Architecture

flowchart TB subgraph sources [Event Sources] POS[pos finalize] Reg[register open/close] GC[giftcards] HR[HR services] Auth[auth] end subgraph api [API] AR[audit.routes] AL[auditLogger middleware] end subgraph svc [Service] AS[audit.service.js] end subgraph db [Tenant Analytics DB] Logs[(audit_logs)] end subgraph reports [Reporting] RS[reporting.service] BC[Business Center] end POS --> AS Reg --> AS GC --> AS HR --> AS Auth --> AL AL --> AS AR --> AS AS --> Logs RS --> AS AS --> BC

Overview

Audit logs every critical action. Stored in tenant analytics DB. Used by reporting, Business Center, compliance.

File Structure

backend/
├── routes/
│   └── audit.routes.js          # Record, logs, summaries, export
├── services/
│   └── audit.service.js        # AuditService class
├── middleware/
│   └── auditLogger.js          # Auto-attach to routes
└── models/
    └── auditEvent.model.js     # Legacy Sequelize (audit_logs raw)

API Endpoints

MethodPathScopePurpose
GET/api/audit/taxonomyaudit:readCategories, eventTypes, entityTypes, filterPresets (from auditTaxonomy.js)
POST/api/audit/recordaudit:writeRecord audit event
GET/api/audit/logsaudit:readGet audit logs with filters
GET/api/audit/summary/event-typeaudit:readSummary by event type
GET/api/audit/summary/useraudit:readSummary by user
GET/api/audit/securityaudit:readSecurity audit summary
GET/api/audit/data-accessaudit:readData access audit summary
GET/api/audit/financialaudit:readFinancial audit summary
GET/api/audit/complianceaudit:readCompliance audit summary
POST/api/audit/entity-trailaudit:writeCreate entity audit trail
GET/api/audit/exportaudit:readExport logs (JSON/CSV)

Audit Schema

FieldPurpose
eventTypecreate, update, delete, view, login, logout, payment, refund
entityTypeuser, customer, product, order, payment, inventory
entityIdTarget entity ID
actionAction performed
performedByActor ID
performedByTypeuser, system, api, admin
oldValues, newValuesJSON — before/after
severitycritical, error, warning, info, debug
categoryauthentication, authorization, data_access, configuration, financial, system, general
ipAddress, userAgent, deviceId, sessionIdRequest context

Integration Points

SystemConnection
POSpos.routes — finalize AuditService.recordAuditEventWithTenantContext
Registerpos-register.routes — open/close register
Gift CardsgiftCard.service — giftcard.issue, giftcard.reload, giftcard.redeem
HRemployee.service, department.service, benefits.service — hr_audit_log
Reportingreporting.service — getFinancialAuditSummary, getAuditSummaryByEventType
Business CenterReports Hub — transaction audit

HR Audit (Separate)

HR services use hr_audit_log in the tenant main DB for employee, department, and benefits changes. See 10 — HR & Payroll.

Key Paths

PathPurpose
backend/routes/audit.routes.jsAudit API
backend/services/audit.service.jsAuditService
backend/constants/auditTaxonomy.jsCategories, event/entity types, Admin filter presets
backend/middleware/auditLogger.jsAuto-attach middleware