Files
hotel777-manager/mailer.js
2026-05-03 16:36:18 +05:00

52 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT) || 587,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
});
async function sendNotification(emails, subject, text) {
if (!emails) return;
const recipients = emails.split(',').map(e => e.trim()).filter(Boolean);
if (recipients.length === 0) return;
try {
await transporter.sendMail({
from: process.env.SMTP_USER,
to: recipients.join(','),
subject,
text
});
console.log(`Уведомление отправлено на ${recipients.join(', ')}`);
} catch (err) {
console.error('Ошибка отправки письма:', err);
}
}
async function notifyNewBooking(booking) {
const subject = `Новая заявка №${booking.external_id} (${process.env.SERVICE_NAME})`;
const text = `Поступила новая заявка на заселение.\n
Имя: ${booking.name}
Телефон: ${booking.phone_raw}
Даты: с ${booking.checkin_date} по ${booking.checkout_date}
Взрослых: ${booking.adults}, детей: ${booking.children}
Ссылка: ${process.env.SERVICE_URL}/admin/bookings`;
await sendNotification(process.env.EMAIL_NOTIFY_NEW, subject, text);
}
async function notifyBookingUpdate(booking, changes) {
const subject = `Изменение заявки №${booking.external_id} (${process.env.SERVICE_NAME})`;
const text = `Заявка №${booking.external_id} была изменена.\n
Новый статус: ${booking.status}
Комментарий: ${booking.comments || 'нет'}
Изменения: ${JSON.stringify(changes, null, 2)}
Ссылка: ${process.env.SERVICE_URL}/admin/bookings`;
await sendNotification(process.env.EMAIL_NOTIFY_UPDATE, subject, text);
}
module.exports = { notifyNewBooking, notifyBookingUpdate };