Implemented a configurable retention policy system for sermons with automatic cleanup: - Added settings table to store retention policy configuration - Created API endpoints for getting/setting retention policy - Added Database Settings section to admin page with retention options (forever, 1-10 years) - Implemented manual cleanup endpoint for on-demand deletion - Added automated daily cleanup task via Nitro plugin - Sermons are deleted based on their date field according to the retention policy 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { getSetting, deleteOldSermons } from '../utils/database'
|
|
|
|
// Map retention policy to days
|
|
const retentionDaysMap: Record<string, number> = {
|
|
'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)
|
|
})
|