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>
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { getSessionUsername } from '~/server/utils/auth'
|
|
import { getUserByUsername, setSetting } 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 from the request body
|
|
const body = await readBody(event)
|
|
const { retentionPolicy } = body
|
|
|
|
// Validate the retention policy value
|
|
const validPolicies = ['forever', '1_month', '3_months', '6_months', '1_year', '3_years', '5_years', '10_years']
|
|
if (!validPolicies.includes(retentionPolicy)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Invalid retention policy'
|
|
})
|
|
}
|
|
|
|
// Save the retention policy setting
|
|
setSetting('sermon_retention_policy', retentionPolicy)
|
|
|
|
return {
|
|
success: true,
|
|
retentionPolicy
|
|
}
|
|
})
|