Overview
The Medical Appointments API includes a comprehensive audit logging system that tracks user actions throughout the application. Audit logs provide accountability, security monitoring, and help with debugging and compliance requirements.Audit Log Model
The audit log data structure is defined in the Prisma schema:Field Descriptions
Auto-incrementing unique identifier for the audit log entry.
Reference to the user who performed the action. Links to the User model.
Description of the action that was performed. Can be a predefined action name or HTTP method with endpoint.
Automatically recorded timestamp of when the action occurred.
What Gets Logged
The system logs various user actions throughout the application:Authentication Events
User Registration
User Registration
When a new user registers, an audit log is created with action
"register".From /home/daytona/workspace/source/src/services/authService.js:26:User Login
User Login
Each successful login is logged with action
"login".From /home/daytona/workspace/source/src/services/authService.js:45:API Operations
The audit middleware can log any successful API request:The middleware only logs successful operations (HTTP status 200-299). Failed requests are not logged to avoid cluttering the audit trail with failed attempts.
Creating Audit Logs
ThelogAudit function from the audit service creates new audit log entries:
Error Handling
The
logAudit function is designed to never throw exceptions. If logging fails, it returns false and logs an error to the console, allowing the main operation to continue uninterrupted.Querying Audit Logs
Only users with theADMIN role can access audit logs. The system provides flexible querying with pagination and filters.
Query Parameters
ThegetAuditLogs function accepts the following options:
Page number for pagination.
Number of records per page. Maximum: 1000.
Filter logs by specific user ID.
Filter logs by action name (case-insensitive partial match).
Filter logs from this timestamp onwards.
Filter logs up to this timestamp.
Example Query
From/home/daytona/workspace/source/src/services/audit.js:49:
- Includes related user information
- Orders results by newest first
- Implements pagination
- Enforces a maximum limit of 1000 records
Access Control
Audit log access is restricted to administrators:Relationship with Users
Audit logs are linked to users with a cascade delete policy:- Each audit log belongs to exactly one user
- When a user is deleted, all their audit logs are automatically deleted
- This ensures referential integrity and compliance with data deletion requirements
Use Cases
Security Monitoring
Track suspicious login patterns or unauthorized access attempts:User Activity Reports
Generate reports of specific user actions:Compliance and Auditing
Maintain an immutable record of who did what and when for regulatory compliance.Best Practices
- Log Important Actions: Focus on security-relevant and business-critical operations
- Descriptive Actions: Use clear, consistent action names for easy filtering
- Regular Review: Administrators should periodically review audit logs for unusual patterns
- Retention Policy: Consider implementing a retention policy to archive or delete old audit logs
- Performance: Audit logging is asynchronous and designed to not impact application performance