import { getSetting, deleteOldSermons } from '../utils/database' // Map retention policy to days const retentionDaysMap: Record = { 'forever': 0, '1_month': 30, '3_months': 90, '6_months': 180, '1_year': 365, '3_years': 1095, '5_years': 1825, '10_years': 3650 } async function runCleanup() { try { // Get the retention policy setting const setting = getSetting('sermon_retention_policy') const retentionPolicy = setting ? setting.value : 'forever' const retentionDays = retentionDaysMap[retentionPolicy] || 0 if (retentionDays === 0) { console.log('[Sermon Cleanup] Retention policy is set to forever, skipping cleanup') return } // Delete old sermons const result = deleteOldSermons(retentionDays) if (result.changes > 0) { console.log(`[Sermon Cleanup] Deleted ${result.changes} sermon(s) older than ${retentionDays} days`) } else { console.log(`[Sermon Cleanup] No sermons to delete (policy: ${retentionPolicy})`) } } catch (error) { console.error('[Sermon Cleanup] Error running cleanup:', error) } } export default defineNitroPlugin((nitroApp) => { console.log('[Sermon Cleanup] Scheduling daily cleanup task') // Run cleanup once at startup (after a short delay) setTimeout(async () => { console.log('[Sermon Cleanup] Running initial cleanup') await runCleanup() }, 10000) // 10 seconds after startup // Schedule cleanup to run daily (every 24 hours) const dailyInterval = 24 * 60 * 60 * 1000 // 24 hours in milliseconds setInterval(async () => { console.log('[Sermon Cleanup] Running scheduled cleanup') await runCleanup() }, dailyInterval) })