const BREVO_API_URL = 'https://api.brevo.com/v3/smtp/email'; /** * Send email via Brevo API */ async function sendEmail({ to, subject, htmlContent, textContent }) { const apiKey = process.env.BREVO_API_KEY; if (!apiKey) { console.error('BREVO_API_KEY not configured'); throw new Error('Email service not configured'); } const payload = { sender: { name: process.env.BREVO_SENDER_NAME || 'WellNuo', email: process.env.BREVO_SENDER_EMAIL || 'noreply@wellnuo.com' }, to: [{ email: to }], subject, htmlContent, textContent }; const response = await fetch(BREVO_API_URL, { method: 'POST', headers: { 'accept': 'application/json', 'api-key': apiKey, 'content-type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { const error = await response.text(); console.error('Brevo API error:', error); throw new Error('Failed to send email'); } return await response.json(); } /** * Send password reset email */ async function sendPasswordResetEmail(email, resetToken) { const frontendUrl = process.env.FRONTEND_URL || 'https://wellnuo.smartlaunchhub.com'; const resetLink = `${frontendUrl}/reset-password?token=${resetToken}`; const htmlContent = `

WellNuo

Password Reset Request

We received a request to reset your password. Click the button below to create a new password:

Reset Password

Or copy and paste this link into your browser:

${resetLink}

This link will expire in 1 hour.

If you didn't request a password reset, you can safely ignore this email.

`; const textContent = ` WellNuo - Password Reset We received a request to reset your password. Click this link to reset your password: ${resetLink} This link will expire in 1 hour. If you didn't request a password reset, you can safely ignore this email. WellNuo - Elderly Care Monitoring `; return sendEmail({ to: email, subject: 'WellNuo - Password Reset Request', htmlContent, textContent }); } /** * Send OTP code for email-based login */ async function sendOTPEmail(email, code, userName) { const htmlContent = `
WellNuo

Your verification code

${userName ? `Hi ${userName}, use` : 'Use'} this code to sign in

${code}

Code expires in 10 minutes

If you didn't request this code, you can safely ignore this email.

`; const textContent = ` WellNuo - Your verification code ${userName ? `Hi ${userName}, use` : 'Use'} this code to sign in: ${code} Code expires in 10 minutes. If you didn't request this code, you can safely ignore this email. `; try { await sendEmail({ to: email, subject: `${code} - Your WellNuo Login Code`, htmlContent, textContent }); return true; } catch (error) { console.error('Failed to send OTP email:', error); return false; } } /** * Send invitation email to share beneficiary access */ async function sendInvitationEmail({ email, inviterName, beneficiaryName, role, inviteCode }) { const frontendUrl = process.env.FRONTEND_URL || 'https://wellnuo.smartlaunchhub.com'; const acceptLink = `${frontendUrl}/accept-invite?code=${inviteCode}`; const roleText = role === 'guardian' ? 'Guardian (full access)' : 'Caretaker (view only)'; const htmlContent = `

WellNuo

Elderly Care Monitoring

You've been invited!

${inviterName || 'Someone'} has invited you to help monitor ${beneficiaryName || 'their loved one'} on WellNuo.

Your role:

${roleText}

Your invitation code:

${inviteCode}

Accept Invitation

This invitation expires in 7 days.

If you didn't expect this invitation, you can safely ignore this email.

`; const textContent = ` WellNuo - You've been invited! ${inviterName || 'Someone'} has invited you to help monitor ${beneficiaryName || 'their loved one'} on WellNuo. Your role: ${roleText} Your invitation code: ${inviteCode} Accept your invitation: ${acceptLink} This invitation expires in 7 days. If you didn't expect this invitation, you can safely ignore this email. WellNuo - Elderly Care Monitoring `; try { await sendEmail({ to: email, subject: `${inviterName || 'Someone'} invited you to WellNuo`, htmlContent, textContent }); return true; } catch (error) { console.error('Failed to send invitation email:', error); return false; } } module.exports = { sendEmail, sendPasswordResetEmail, sendOTPEmail, sendInvitationEmail };