Files
motovaultpro/backend/src/features/audit-log/jobs/cleanup.job.ts
Eric Gullickson c98211f4a2
Some checks failed
Deploy to Staging / Build Images (pull_request) Successful in 4m42s
Deploy to Staging / Deploy to Staging (pull_request) Successful in 37s
Deploy to Staging / Verify Staging (pull_request) Failing after 6s
Deploy to Staging / Notify Staging Ready (pull_request) Has been skipped
Deploy to Staging / Notify Staging Failure (pull_request) Successful in 6s
feat: Implement centralized audit logging admin interface (refs #10)
- Add audit_logs table with categories, severities, and indexes
- Create AuditLogService and AuditLogRepository
- Add REST API endpoints for viewing and exporting logs
- Wire audit logging into auth, vehicles, admin, and backup features
- Add desktop AdminLogsPage with filters and CSV export
- Add mobile AdminLogsMobileScreen with card layout
- Implement 90-day retention cleanup job
- Remove old AuditLogPanel from AdminCatalogPage

Security fixes:
- Escape LIKE special characters to prevent pattern injection
- Limit CSV export to 5000 records to prevent memory exhaustion
- Add truncation warning headers for large exports

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-11 11:09:09 -06:00

75 lines
1.8 KiB
TypeScript

/**
* @ai-summary Job for audit log retention cleanup
* @ai-context Runs daily at 3 AM to delete logs older than 90 days
*/
import { Pool } from 'pg';
import { logger } from '../../../core/logging/logger';
import { AuditLogService } from '../domain/audit-log.service';
import { AuditLogRepository } from '../data/audit-log.repository';
let pool: Pool | null = null;
/**
* Sets the database pool for the job
*/
export function setAuditLogCleanupJobPool(dbPool: Pool): void {
pool = dbPool;
}
/**
* Retention period in days for audit logs
*/
const AUDIT_LOG_RETENTION_DAYS = 90;
/**
* Result of cleanup job
*/
export interface AuditLogCleanupResult {
deletedCount: number;
retentionDays: number;
success: boolean;
error?: string;
}
/**
* Processes audit log retention cleanup
*/
export async function processAuditLogCleanup(): Promise<AuditLogCleanupResult> {
if (!pool) {
throw new Error('Database pool not initialized for audit log cleanup job');
}
const repository = new AuditLogRepository(pool);
const service = new AuditLogService(repository);
try {
logger.info('Starting audit log cleanup job', {
retentionDays: AUDIT_LOG_RETENTION_DAYS,
});
const deletedCount = await service.cleanup(AUDIT_LOG_RETENTION_DAYS);
logger.info('Audit log cleanup job completed', {
deletedCount,
retentionDays: AUDIT_LOG_RETENTION_DAYS,
});
return {
deletedCount,
retentionDays: AUDIT_LOG_RETENTION_DAYS,
success: true,
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error('Audit log cleanup job failed', { error: errorMessage });
return {
deletedCount: 0,
retentionDays: AUDIT_LOG_RETENTION_DAYS,
success: false,
error: errorMessage,
};
}
}