This implementation adds comprehensive organization-scoped audit logging and activity feed functionality to Trivela, enabling organizations to track and monitor all actions performed within their organization scope.
- Migration 015: Added
org_idcolumn toaudit_logstable - Indexes: Created composite indexes for efficient org-scoped queries
- PostgreSQL Support: Updated PostgreSQL schema with corresponding changes
- Organization Scoping: All audit log queries can now be filtered by organization
- Advanced Filtering: Support for filtering by actor, action, entity, date ranges
- Pagination: Built-in pagination support with limit/offset
- Counting: Efficient count queries for pagination metadata
- Organization Context: Automatic organization context handling
- Activity Descriptions: Human-readable activity descriptions
- Export Functionality: CSV and JSON export with proper escaping
- Statistics: Comprehensive audit statistics and analytics
GET /api/v1/orgs/:orgId/audit- Organization audit logs with filteringGET /api/v1/orgs/:orgId/audit/export/csv- CSV exportGET /api/v1/orgs/:orgId/audit/export/json- JSON exportGET /api/v1/orgs/:orgId/audit/stats- Audit statisticsGET /api/v1/orgs/:orgId/activity-feed- Activity feed for dashboard
- Organization Isolation: Users can only access their own organization's audit logs
- Permission-Based: Requires
audit:readpermission - API Key Authentication: Integrated with existing auth middleware
-- Added to audit_logs table
ALTER TABLE audit_logs ADD COLUMN org_id TEXT;
-- New indexes for performance
CREATE INDEX idx_audit_logs_org_id ON audit_logs(org_id);
CREATE INDEX idx_audit_logs_org_entity ON audit_logs(org_id, entity);
CREATE INDEX idx_audit_logs_org_action ON audit_logs(org_id, action);
CREATE INDEX idx_audit_logs_org_created_at ON audit_logs(org_id, created_at);GET /api/v1/orgs/org-123/audit?page=1&pageSize=50&action=create&startDate=2024-01-01GET /api/v1/orgs/org-123/audit/export/csv?entity=campaign&startDate=2024-01-01GET /api/v1/orgs/org-123/activity-feed?limit=20{
"success": true,
"data": [
{
"id": "123",
"actor": "apiKey:ab12...ef34",
"action": "create",
"entity": "campaign",
"entityId": "camp-456",
"orgId": "org-123",
"diff": { "after": { "name": "New Campaign" } },
"timestamp": "2024-01-15T10:30:00Z"
}
],
"pagination": {
"page": 1,
"pageSize": 50,
"totalCount": 150,
"totalPages": 3,
"hasNextPage": true,
"hasPreviousPage": false
}
}{
"success": true,
"data": [
{
"id": "123",
"actor": "apiKey:ab12...ef34",
"action": "create",
"entity": "campaign",
"entityId": "camp-456",
"orgId": "org-123",
"timestamp": "2024-01-15T10:30:00Z",
"description": "apiKey:ab12...ef34 created campaign \"camp-456\""
}
]
}{
"success": true,
"data": {
"totalActions": 1250,
"actionBreakdown": {
"create": 450,
"update": 600,
"delete": 200
},
"entityBreakdown": {
"campaign": 800,
"apiKey": 250,
"webhook": 200
},
"topActors": [
{ "actor": "apiKey:ab12...ef34", "count": 425 },
{ "actor": "apiKey:cd56...gh78", "count": 380 }
]
}
}- Migration:
backend/src/db/migrations/015_audit_logs_org_scoped.js - Repository:
backend/src/dal/sqliteAuditLogRepository.js(enhanced) - Interface:
backend/src/dal/auditLogRepository.js(enhanced) - Service:
backend/src/services/auditLogService.js(new) - Routes:
backend/src/routes/audit.js(new) - Main App:
backend/src/index.js(enhanced) - PostgreSQL Schema:
backend/src/dal/pg/migrations/001_initial_schema.sql(updated)
- Repository Tests:
backend/tests/integration/auditLogRepository.test.js(enhanced) - Service Tests:
backend/tests/integration/auditLogService.test.js(new) - Test Setup:
backend/tests/integration/setup.js(enhanced)
All audit log operations are scoped to organizations. When creating audit entries, the system automatically includes the organization context from the authenticated user.
- Actor filtering: Filter by specific API keys or users
- Action filtering: Filter by specific actions (create, update, delete, etc.)
- Entity filtering: Filter by resource types (campaign, apiKey, etc.)
- Date range filtering: Filter by start/end dates
- Combined filtering: Multiple filters can be combined
- CSV Export: Properly escaped CSV with all audit data
- JSON Export: Structured JSON with metadata and filters applied
- Large Dataset Support: Handles up to 10,000 records per export
- Human-readable descriptions: Converts raw audit data to readable activity descriptions
- Recent activity focus: Optimized for dashboard display
- Configurable limits: Adjustable result limits (default 20, max 50)
- Database indexes: Optimized indexes for org-scoped queries
- Pagination: Efficient pagination with proper counting
- Query optimization: Optimized SQL queries for filtering and sorting
- Users can only access audit logs for their own organization
- API endpoints validate organization membership
- Database queries are automatically scoped to user's organization
- Requires
audit:readpermission for all audit endpoints - Uses existing RBAC system for access control
- API key authentication required for all endpoints
- Actor information is anonymized (shows key prefixes, not full keys)
- Sensitive diff data is preserved but access-controlled
- Export functionality respects organization boundaries
The implementation includes comprehensive tests covering:
- Database schema and migrations
- Repository functionality with all filters
- Service layer with organization scoping
- Export functionality (CSV/JSON)
- Activity feed generation
- Pagination and counting
- Edge cases and error handling
The audit log and activity feed can be integrated into admin dashboards:
// Fetch recent activity for dashboard
const activityFeed = await fetch(`/api/v1/orgs/${orgId}/activity-feed?limit=10`);
// Get filtered audit logs
const auditLogs = await fetch(`/api/v1/orgs/${orgId}/audit?action=create&page=1`);
// Export audit data
const csvData = await fetch(`/api/v1/orgs/${orgId}/audit/export/csv`);Organizations can now:
- Track all administrative actions
- Monitor API key usage patterns
- Export compliance reports
- Generate activity dashboards
- Investigate security incidents
- Indexed queries: All org-scoped queries use database indexes
- Pagination: Efficient pagination prevents large result sets
- Export limits: Exports capped at 10K records to prevent timeouts
- Memory efficiency: Streaming approach for large datasets
- Cache-friendly: Consistent query patterns enable caching
Potential future improvements:
- Real-time notifications: WebSocket-based activity notifications
- Advanced search: Full-text search across audit descriptions
- Retention policies: Automatic cleanup of old audit data
- Additional export formats: PDF, Excel export options
- Audit log replay: Ability to replay sequence of actions
- Integration hooks: Webhook notifications for specific audit events
This implementation provides a comprehensive audit logging and activity feed system that enables organizations to:
- Track all administrative actions within their organization
- Export audit data for compliance and analysis
- Monitor activity through dashboard feeds
- Maintain security and access control
- Scale efficiently with proper indexing and pagination
The system is built with security, performance, and usability in mind, providing a solid foundation for organizational audit tracking and compliance requirements.