Files
nlcc-itinerary/server/api/sermons/cleanup.post.ts
Joshua Ryder 66172e0baa Add sermon retention policy feature
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>
2025-11-04 14:07:14 -05:00

58 lines
1.5 KiB
TypeScript

import { getSessionUsername } from '~/server/utils/auth'
import { getUserByUsername, getSetting, deleteOldSermons } from '~/server/utils/database'
export default defineEventHandler(async (event) => {
// Check if user is authenticated and is admin
const username = await getSessionUsername(event)
if (!username) {
throw createError({
statusCode: 401,
message: 'Unauthorized'
})
}
const user = getUserByUsername(username)
if (!user || user.is_admin !== 1) {
throw createError({
statusCode: 403,
message: 'Forbidden - Admin access required'
})
}
// Get the retention policy setting
const setting = getSetting('sermon_retention_policy')
const retentionPolicy = setting ? setting.value : 'forever'
// Map retention policy to days
const retentionDaysMap: Record<string, number> = {
'forever': 0, // 0 means no deletion
'1_month': 30,
'3_months': 90,
'6_months': 180,
'1_year': 365,
'3_years': 1095,
'5_years': 1825,
'10_years': 3650
}
const retentionDays = retentionDaysMap[retentionPolicy] || 0
if (retentionDays === 0) {
return {
success: true,
message: 'Retention policy is set to forever, no sermons deleted',
deletedCount: 0
}
}
// Delete old sermons
const result = deleteOldSermons(retentionDays)
return {
success: true,
message: `Deleted sermons older than ${retentionDays} days`,
deletedCount: result.changes,
retentionPolicy
}
})