Add intelligent auto-archiving system that automatically moves sermons to the "Previous Sermons" list when they are 1 day past their most recent date. Features: - Auto-archive logic that checks both primary and additional sermon dates - Finds the most recent date across all dates for a sermon - Archives sermon 1 day after the most recent date has passed - Manual trigger via "Run Auto-Archive Now" button on admin page - Automatic daily execution via scheduled cleanup task - Clear admin UI with explanatory text and status messages - Manual archive/unarchive functionality preserved Implementation: - Added getMostRecentSermonDate() helper to find latest date from primary and additional dates - Added autoArchiveOldSermons() function to database utils - Created /api/sermons/auto-archive endpoint for manual triggering - Integrated into daily cleanup plugin schedule - Updated admin UI with auto-archive button and status indicators - Added unarchiveSermon() function for completeness The system runs automatically every 24 hours and can be manually triggered by admins. Sermons are moved to the previous sermons dropdown on the home page exactly 1 day after their final presentation date, ensuring the main page always shows current and upcoming content while preserving access to past sermons. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { getSessionUsername } from '~/server/utils/auth'
|
|
import { autoArchiveOldSermons, getUserByUsername } from '~/server/utils/database'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// Check authentication
|
|
const username = await getSessionUsername(event)
|
|
|
|
if (!username) {
|
|
throw createError({
|
|
statusCode: 401,
|
|
message: 'Unauthorized'
|
|
})
|
|
}
|
|
|
|
// Check admin role
|
|
const user = getUserByUsername(username)
|
|
|
|
if (!user || user.is_admin !== 1) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: 'Forbidden - Admin access required'
|
|
})
|
|
}
|
|
|
|
try {
|
|
const result = autoArchiveOldSermons()
|
|
|
|
return {
|
|
success: true,
|
|
message: result.archivedCount > 0
|
|
? `Successfully auto-archived ${result.archivedCount} sermon(s)`
|
|
: 'No sermons needed to be archived',
|
|
archivedCount: result.archivedCount
|
|
}
|
|
} catch (error: any) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: error.message || 'Failed to auto-archive sermons'
|
|
})
|
|
}
|
|
})
|