From f8d8bb2330780c99022a557744fabe418c11dd06 Mon Sep 17 00:00:00 2001 From: richardtekula Date: Wed, 17 Dec 2025 07:42:48 +0100 Subject: [PATCH] feat: Add cron job for audit logs cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/cron/cleanupAuditLogs.js | 57 ++++++++++++++++++++++++++++++++++++ src/cron/index.js | 6 +++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/cron/cleanupAuditLogs.js diff --git a/src/cron/cleanupAuditLogs.js b/src/cron/cleanupAuditLogs.js new file mode 100644 index 0000000..9ce58a9 --- /dev/null +++ b/src/cron/cleanupAuditLogs.js @@ -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'); +}; diff --git a/src/cron/index.js b/src/cron/index.js index 7b9a0cf..cf0c957 100644 --- a/src/cron/index.js +++ b/src/cron/index.js @@ -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 };