Files
kavehhn 72be01fee3 feat: add messaging toggles, password reset SMS, and payment discounts
Allow SuperAdmin to disable SMS, email, and bot from dashboard settings on top of env flags. Add admin password reset with credentials SMS, plus payment discounts, notes, and payable amount handling.
2026-08-16 06:58:08 +03:30

59 lines
1.6 KiB
JavaScript

// /utils/senders/emailSender.js
const nodemailer = require('nodemailer');
const config = require('../../config/config');
const logger = require('../logger');
const { isMessagingChannelEnabled } = require('../../components/settings/messagingFlags');
let transporter = null;
const getTransporter = () => {
if (!transporter) {
transporter = nodemailer.createTransport({
host: config.SMTP_HOST,
port: config.SMTP_PORT,
secure: config.SMTP_PORT === 465,
auth: config.SMTP_USER ? {
user: config.SMTP_USER,
pass: config.SMTP_PASS
} : undefined
});
}
return transporter;
};
const sendEmail = async ({ to, subject, body, html }) => {
try {
if (!to) throw new Error('Recipient email is required');
if (!(await isMessagingChannelEnabled('email'))) {
logger.info(`[EmailSender] Skipped (channel disabled) → ${to}`);
return { skipped: true, reason: 'channel_disabled' };
}
const mailOptions = {
from: config.EMAIL_FROM,
to,
subject,
text: body,
html: html || `<p>${body}</p>`
};
if (config.NODE_ENV === 'test' || !config.SMTP_USER) {
logger.info(`[EmailSender MOCK] To: ${to} | Subject: ${subject} | Body: ${body}`);
return { success: true, messageId: `mock_${Date.now()}` };
}
const info = await getTransporter().sendMail(mailOptions);
logger.info(`[EmailSender] Message sent: ${info.messageId} to ${to}`);
return { success: true, messageId: info.messageId };
} catch (error) {
logger.error(`[EmailSender ERROR] Failed to send email to ${to}: ${error.message}`);
throw error;
}
};
module.exports = {
sendEmail
};