82 lines
2.8 KiB
JavaScript
82 lines
2.8 KiB
JavaScript
/**
|
|
* Routing helpers for sending notifications based on a user's
|
|
* notificationPreference (email | whatsapp | both).
|
|
*
|
|
* Security-critical messages (password reset, login alert, password changed,
|
|
* account closed) always send email regardless of preference, and additionally
|
|
* send a WhatsApp message when preference includes it.
|
|
*
|
|
* Informational messages (registration, payment, tickets) respect the
|
|
* preference fully.
|
|
*/
|
|
|
|
const { isValidZAPhone, sendText, sendPdf } = require('./whatsapp');
|
|
|
|
/** Returns true when the user should receive a WhatsApp notification. */
|
|
function canWhatsApp(user) {
|
|
if (!user) return false;
|
|
const pref = user.notificationPreference || 'email';
|
|
return (pref === 'whatsapp' || pref === 'both') && isValidZAPhone(user.phoneNumber);
|
|
}
|
|
|
|
/** Returns true when the user should receive an email notification. */
|
|
function shouldEmail(user) {
|
|
if (!user) return false;
|
|
const pref = user.notificationPreference || 'email';
|
|
return pref === 'email' || pref === 'both';
|
|
}
|
|
|
|
/**
|
|
* Fire-and-forget WhatsApp text to a user.
|
|
* Silently skips if the user can't receive WhatsApp.
|
|
*/
|
|
async function waText(user, message) {
|
|
if (!canWhatsApp(user)) return;
|
|
try {
|
|
await sendText(user.phoneNumber, message);
|
|
} catch (e) {
|
|
console.warn('[notify] WhatsApp text failed:', e?.response?.data?.message || e.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fire-and-forget WhatsApp PDF to a user.
|
|
* Silently skips if the user can't receive WhatsApp.
|
|
*/
|
|
async function waPdf(user, localPdfPath, filename, caption) {
|
|
if (!canWhatsApp(user)) return;
|
|
try {
|
|
await sendPdf(user.phoneNumber, localPdfPath, filename, caption);
|
|
} catch (e) {
|
|
console.warn('[notify] WhatsApp PDF failed:', e?.response?.data?.message || e.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Like waText but bypasses the notification preference check.
|
|
* Use as a fallback when email is not available (guest / no valid email),
|
|
* so the user still receives notifications via WhatsApp if they have a phone.
|
|
*/
|
|
async function waTextAny(user, message) {
|
|
if (!user || !isValidZAPhone(user.phoneNumber)) return;
|
|
try {
|
|
await sendText(user.phoneNumber, message);
|
|
} catch (e) {
|
|
console.warn('[notify] WhatsApp text (fallback) failed:', e?.response?.data?.message || e.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Like waPdf but bypasses the notification preference check.
|
|
* Use as a fallback when email is not available (guest / no valid email).
|
|
*/
|
|
async function waPdfAny(user, localPdfPath, filename, caption) {
|
|
if (!user || !isValidZAPhone(user.phoneNumber)) return;
|
|
try {
|
|
await sendPdf(user.phoneNumber, localPdfPath, filename, caption);
|
|
} catch (e) {
|
|
console.warn('[notify] WhatsApp PDF (fallback) failed:', e?.response?.data?.message || e.message);
|
|
}
|
|
}
|
|
|
|
module.exports = { canWhatsApp, shouldEmail, waText, waPdf, waTextAny, waPdfAny }; |