Files
nlcc-itinerary/server/api/notes/download/[sermonId].get.ts
2025-10-12 00:24:27 -04:00

119 lines
3.2 KiB
TypeScript

import { getSermonNote, getUserByUsername, getDatabase } from '~/server/utils/database'
import { getSessionUsername } from '~/server/utils/auth'
export default defineEventHandler(async (event) => {
const username = await getSessionUsername(event)
if (!username) {
throw createError({
statusCode: 401,
message: 'Unauthorized'
})
}
const sermonId = parseInt(event.context.params?.sermonId || '')
if (!sermonId) {
throw createError({
statusCode: 400,
message: 'Invalid sermon ID'
})
}
// Get user info
const user = getUserByUsername(username)
if (!user) {
throw createError({
statusCode: 400,
message: 'User not found'
})
}
// Get sermon info
const db = getDatabase()
const sermon = db.prepare('SELECT * FROM sermons WHERE id = ?').get(sermonId) as any
if (!sermon) {
throw createError({
statusCode: 404,
message: 'Sermon not found'
})
}
// Get user's notes
const noteRecord = getSermonNote(user.id!, sermonId)
const userNotes = noteRecord?.notes || 'No notes taken'
// Format bible references
let bibleReferencesText = ''
try {
const refs = JSON.parse(sermon.bible_references)
bibleReferencesText = refs.map((ref: any) =>
`${ref.reference} (${ref.version})\n${ref.text}`
).join('\n\n')
} catch {
bibleReferencesText = sermon.bible_references
}
// Format date
const formatDate = (dateString: string) => {
const date = new Date(dateString + 'T00:00:00')
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
}
const dates = [sermon.date]
if (sermon.dates) {
try {
const additionalDates = JSON.parse(sermon.dates)
dates.push(...additionalDates)
} catch {}
}
const sermonDate = dates.map(formatDate).join(' - ')
// Create formatted text content
const textContent = `
================================================================================
New Life Christian Church - SERMON NOTES
================================================================================
Title: ${sermon.title}
Date: ${sermonDate}
================================================================================
BIBLE REFERENCES
================================================================================
${bibleReferencesText}
================================================================================
PERSONAL APPLIANCE
================================================================================
${sermon.personal_appliance}
================================================================================
PASTOR'S CHALLENGE
================================================================================
${sermon.pastors_challenge}
================================================================================
MY NOTES
================================================================================
${userNotes}
`.trim()
// Set headers for file download
setResponseHeaders(event, {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Disposition': `attachment; filename="${sermon.slug}-notes.txt"`
})
return textContent
})