Audit Logs

Comprehensive audit trail for compliance and debugging. Tracks user actions, data changes, and access patterns across the entire system. Every significant operation is logged with who, what, when, where, and change details.

Source Files

LayerPath
Schemabackend/src/db/schema.ts
Routesbackend/src/routes/auditLogs.ts
Middlewarebackend/src/middleware/auditLogger.ts
Componentsfrontend/src/components/audit-log/ (8 files)
Queriesfrontend/src/lib/queries/activityQueries.ts

Database Tables

TablePurpose
audit_logsSystem-wide audit trail — every significant action with field-level change data
activity_logsBuilding-anchored collaboration feed — comments, action items, appointments (scoped task/phase/deal/building)
component_change_logsDedicated change tracking for Building Components (field diffs, change reason, automated flag)

Log Entry Structure

Each audit_logs entry captures:

FieldPurpose
userIdWho performed the action (user reference, set null on delete)
actionWhat happened (e.g., create, update, delete)
entityType / entityIdWhich entity was affected (e.g., building, project, document)
oldValue / newValueJSONB before/after values
componentField / changeTypeField-level change descriptor
buildingIdBuilding context (cascade)
sessionIdSession that performed the action (set null)
ipAddress / userAgentClient metadata for security auditing
createdAtWhen the action occurred

Attribution note (Human FK rule): audit_logs.userId still references users.id — a grandfathered legacy FK. New attribution columns must reference contacts.id (durable identity); e.g., workflow_transition_log.actorId was migrated to contacts.id in PR#1931 (I#1759). See Contacts.

Activity Logs

activity_logs is not a mirror of the audit trail — it is the user-facing activity/collaboration feed. Entries are anchored on a building (buildingId required) with optional projectId, quoteId, and workflow-package references, carry an authorId, free-text content, and support action-item fields (assigneeId, dueDate, completedAt) and appointment fields (appointmentDate, appointmentType, scheduledDate) — see Appointments.

Middleware Integration

The auditLogger middleware (backend/src/middleware/auditLogger.ts) provides:

  • Mutation logginglogEntityChange / withAuditLogging record create, update, and delete operations
  • Field-level diffscreateFieldChanges compares old/new objects into per-field change records
  • Request metadataextractRequestMetadata captures IP address and user agent

The middleware integrates into the Backend Architecture middleware stack and runs after RBAC Authorization checks.

Frontend Components

ComponentPurpose
AuditLogTablePaginated, filterable table of audit entries
AuditLogDetailExpanded view of a single log entry with change diff
AuditLogFiltersFilter by user, action, entity type, date range
ActivityFeedCompact recent-activity timeline shown on entity detail pages
ActivityItemSingle activity entry with icon, description, and timestamp

Query Patterns

  • Entity history — fetch all audit entries for a specific entity (entityType + entityId)
  • User activity — fetch all actions by a specific user
  • Building history — fetch all audit entries for a building via buildingId
  • Date-range queries — compliance reporting for specific time periods

Relationships

AuditLog *──1 Users (actor, legacy FK)
AuditLog *──1 Building (context)
AuditLog *──1 Session
AuditLog ──> any entity (via entityType + entityId)
ActivityLog *──1 Building (anchor, required)
ActivityLog *──1 Users (author)
ActivityLog ──> Project / Quote / Workflow Package (optional context)

Users | RBAC Authorization | Admin Dashboard | Backend Architecture | Service Layer Pattern | Database Architecture