Skip to content

Build Asset History and Audit Trail System #383

@yusuftomilola

Description

@yusuftomilola

Description

Implement a comprehensive audit trail system that tracks all changes to assets and related entities, providing complete history and accountability for compliance and reporting.

Requirements

AssetHistory Entity:

AssetHistory {
  id: UUID (primary key)
  asset: ManyToOne -> Asset
  eventType: enum (CREATED, UPDATED, ASSIGNED, TRANSFERRED, STATUS_CHANGED, 
                   MAINTENANCE_SCHEDULED, MAINTENANCE_COMPLETED, RETIRED, 
                   DOCUMENT_ADDED, DOCUMENT_REMOVED, NOTE_ADDED, DELETED)
  actor: ManyToOne -> User (who performed the action)
  changes: jsonb (before/after values)
  metadata: jsonb (additional context)
  ipAddress: string (optional)
  userAgent: string (optional)
  timestamp: timestamp
  relatedEntity: string (optional - e.g., transfer ID, maintenance ID)
  relatedEntityType: string (optional)
}

AuditLog Entity:

AuditLog {
  id: UUID (primary key)
  entityType: string (Asset, Department, User, etc.)
  entityId: UUID
  action: enum (CREATE, READ, UPDATE, DELETE)
  actor: ManyToOne -> User
  changes: jsonb (detailed change log)
  metadata: jsonb
  ipAddress: string
  userAgent: string
  endpoint: string (API endpoint called)
  method: string (GET, POST, PUT, DELETE)
  statusCode: integer (HTTP response code)
  requestId: string (correlation ID)
  duration: integer (milliseconds)
  timestamp: timestamp
}

API Endpoints:

  • GET /api/v1/assets/:id/history - Get asset history timeline
  • GET /api/v1/assets/:id/history/:eventType - Filter history by event type
  • GET /api/v1/audit-logs - Get system-wide audit logs (admin only)
  • GET /api/v1/audit-logs/entity/:type/:id - Get audit logs for specific entity
  • GET /api/v1/audit-logs/user/:userId - Get all actions by specific user
  • GET /api/v1/audit-logs/export - Export audit logs to CSV/JSON
  • GET /api/v1/audit-logs/statistics - Get audit statistics (events per day, top actors)
  • POST /api/v1/audit-logs/search - Advanced search with multiple filters

Business Logic:

  • Auto-create history entry on every asset modification
  • Calculate and store field-level changes (diff)
  • Capture user context from JWT token
  • Store IP address and user agent from request headers
  • Link related events (e.g., transfer and assignment change)
  • Support filtering by date range, event type, actor
  • Implement efficient pagination for large history sets
  • Retain audit logs indefinitely (configurable retention policy)
  • Anonymize data on user deletion (GDPR compliance)

Change Tracking Format:

{
  field: "status",
  oldValue: "ACTIVE",
  newValue: "MAINTENANCE",
  changedAt: "2025-01-19T10:30:00Z",
  changedBy: "user-id"
}

Validation Rules:

  • Event type must be valid enum value
  • Actor must be authenticated user
  • Changes object must be valid JSON
  • Timestamp must be accurate server time
  • IP address must be valid format

Technical Specifications

  • Use TypeORM subscribers/listeners to auto-create history entries
  • Implement middleware to capture request context
  • Store audit logs in separate database/schema for isolation
  • Use partitioning for audit_logs table (partition by month)
  • Create indexes on: asset_id, actor_id, timestamp, event_type
  • Implement full-text search on changes/metadata fields (PostgreSQL tsvector)
  • Use database triggers as fallback for critical operations
  • Implement log streaming to external services (optional - ELK stack)
  • Add request correlation IDs for tracing

Acceptance Criteria

  • Every asset change creates history entry automatically
  • Change diffs are accurate and complete
  • User context is captured correctly
  • History timeline displays events chronologically
  • Filtering and search work efficiently
  • Pagination handles large result sets (10,000+ entries)
  • Audit logs are immutable (cannot be edited or deleted)
  • Export functionality generates valid CSV/JSON
  • Statistics API returns accurate aggregations
  • System performance is not degraded by logging

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions