Files
nlcc-itinerary/server/api/notes/email/[sermonId].post.ts
Joshua Ryder 47b4a14c4b fix: Ensure email configuration from environment variables is properly used
Fixed an issue where SMTP configuration would fall back to defaults despite
environment variables being set in docker-compose.yml. The email utility now
properly accesses runtime configuration by accepting the H3 event context.

Changes:
- Created getEmailConfig() helper with dual-strategy config access
- Pass event context from API handlers to email functions
- Added fallback to direct process.env access for reliability
- Added debug logging to diagnose configuration issues in production

This ensures Office365 and other SMTP providers work correctly when configured
via environment variables.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 18:11:13 -05:00

108 lines
2.7 KiB
TypeScript

import { getSermonNote, getSermonBySlug, getUserByUsername, getDatabase } from '~/server/utils/database'
import { getSessionUsername } from '~/server/utils/auth'
import { sendSermonNotesEmail } from '~/server/utils/email'
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 || !user.email) {
throw createError({
statusCode: 400,
message: 'User email 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)
// Convert line breaks to HTML breaks for email display
const userNotes = noteRecord?.notes ? noteRecord.notes.replace(/\n/g, '<br>') : ''
// Format bible references for HTML email
let bibleReferencesText = ''
try {
const refs = JSON.parse(sermon.bible_references)
bibleReferencesText = refs.map((ref: any) =>
`<div style="margin-bottom: 15px;">
<div style="font-weight: bold; margin-bottom: 5px;">${ref.reference} (${ref.version})</div>
<div>${ref.text}</div>
</div>`
).join('')
} 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(' - ')
// Send email
try {
await sendSermonNotesEmail(
user.email,
user.first_name || user.username,
sermon.title,
sermonDate,
bibleReferencesText,
sermon.personal_appliance,
sermon.pastors_challenge,
userNotes,
event
)
return {
success: true,
message: 'Notes emailed successfully'
}
} catch (error) {
console.error('Failed to send email:', error)
throw createError({
statusCode: 500,
message: 'Failed to send email'
})
}
})