feat: Add cron job for audit logs cleanup
- Add cleanupAuditLogs.js with daily cleanup job - Delete audit logs older than 7 days - Runs every day at midnight (00:00) - Export cleanupOldAuditLogs for manual triggers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
57
src/cron/cleanupAuditLogs.js
Normal file
57
src/cron/cleanupAuditLogs.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import cron from 'node-cron';
|
||||
import { db } from '../config/database.js';
|
||||
import { auditLogs } from '../db/schema.js';
|
||||
import { lt, sql } from 'drizzle-orm';
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
const RETENTION_DAYS = 7;
|
||||
|
||||
/**
|
||||
* Delete audit logs older than RETENTION_DAYS
|
||||
*/
|
||||
export const cleanupOldAuditLogs = async () => {
|
||||
const cutoffDate = new Date();
|
||||
cutoffDate.setDate(cutoffDate.getDate() - RETENTION_DAYS);
|
||||
|
||||
logger.info(`[Audit Cleanup] Spúšťam čistenie audit logov starších ako ${RETENTION_DAYS} dní (pred ${cutoffDate.toISOString()})`);
|
||||
|
||||
try {
|
||||
const result = await db
|
||||
.delete(auditLogs)
|
||||
.where(lt(auditLogs.createdAt, cutoffDate))
|
||||
.returning({ id: auditLogs.id });
|
||||
|
||||
const deletedCount = result.length;
|
||||
|
||||
if (deletedCount > 0) {
|
||||
logger.info(`[Audit Cleanup] Úspešne zmazaných ${deletedCount} starých audit logov`);
|
||||
} else {
|
||||
logger.info('[Audit Cleanup] Žiadne staré audit logy na zmazanie');
|
||||
}
|
||||
|
||||
return deletedCount;
|
||||
} catch (error) {
|
||||
logger.error('[Audit Cleanup] Chyba pri mazaní audit logov:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Start the audit logs cleanup cron job
|
||||
* Runs every day at midnight (00:00)
|
||||
*/
|
||||
export const startAuditCleanupCron = () => {
|
||||
// Schedule: '0 0 * * *' = every day at 00:00
|
||||
cron.schedule('0 0 * * *', () => {
|
||||
logger.info('[Audit Cleanup] Cron job spustený');
|
||||
cleanupOldAuditLogs()
|
||||
.then(() => {
|
||||
logger.info('[Audit Cleanup] Cron job dokončený');
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error('[Audit Cleanup] Cron job zlyhal:', error);
|
||||
});
|
||||
});
|
||||
|
||||
logger.info('[Audit Cleanup] Cron job naplánovaný - každý deň o 00:00');
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import { logger } from '../utils/logger.js';
|
||||
import { startCalendarNotificationCron, triggerEventNotifications, triggerSingleEventNotification } from './calendar/index.js';
|
||||
import { startAuditCleanupCron, cleanupOldAuditLogs } from './cleanupAuditLogs.js';
|
||||
|
||||
/**
|
||||
* Start all cron jobs
|
||||
@@ -10,8 +11,11 @@ export const startAllCronJobs = () => {
|
||||
// Calendar event notifications
|
||||
startCalendarNotificationCron();
|
||||
|
||||
// Audit logs cleanup (daily at midnight)
|
||||
startAuditCleanupCron();
|
||||
|
||||
logger.info('=== Všetky cron jobs inicializované ===');
|
||||
};
|
||||
|
||||
// Export individual functions for testing/manual triggers
|
||||
export { triggerEventNotifications, triggerSingleEventNotification };
|
||||
export { triggerEventNotifications, triggerSingleEventNotification, cleanupOldAuditLogs };
|
||||
|
||||
Reference in New Issue
Block a user