server/lib/notifications.js Repository wynajem_motorowek Original path server/lib/notifications.jsRole SOURCE Size 3183 bytes Lines 77 SHA-256 a703ce74b76bcf5d8dfe420d4688888232a4d10dc01619ff8563a1af146c1e18Displayed range 1–77 Previous file/page · Project index · Next file/page
const { sendMail } = require("./mailer");
function formatDateTime(value) {
const date = new Date(String(value).replace(" ", "T"));
const dd = String(date.getDate()).padStart(2, "0");
const mm = String(date.getMonth() + 1).padStart(2, "0");
const yyyy = date.getFullYear();
const hh = String(date.getHours()).padStart(2, "0");
const min = String(date.getMinutes()).padStart(2, "0");
return `${dd}.${mm}.${yyyy} ${hh}:${min}`;
}
async function getSupportRecipients(db) {
const ownerEmail = String(process.env.OWNER_NOTIFY_EMAIL || process.env.SMTP_USER || "").trim();
const rows = (await db.query(`SELECT email FROM support_emails WHERE active = 1 ORDER BY email ASC`)).rows;
const set = new Set();
if (ownerEmail) set.add(ownerEmail.toLowerCase());
for (const row of rows) {
const email = String(row.email || "").trim().toLowerCase();
if (email) set.add(email);
}
return Array.from(set);
}
function bookingConfirmationMessage({ reservation, phoneNumber, cancelUrl }) {
const lines = [
`Potwierdzenie rezerwacji`,
`Numer rezerwacji: ${reservation.order_number || "-"}`,
`Start: ${formatDateTime(reservation.start_time)}`,
`Koniec: ${formatDateTime(reservation.end_time)}`,
`Telefon kontaktowy: ${phoneNumber}`,
`Anulowanie: ${cancelUrl}`,
];
return lines.join("\n");
}
async function sendReservationConfirmedEmails({ db, reservation, resourceName }) {
const customerEmail = String(reservation.customer_email || "").trim().toLowerCase();
const recipients = await getSupportRecipients(db);
const publicBase = String(process.env.PUBLIC_BASE_URL || `http://localhost:${process.env.PORT || 3030}`).replace(/\/$/, "");
const cancelUrl = `${publicBase}/cancel_reservation?order=${encodeURIComponent(reservation.order_number || "")}&token=${encodeURIComponent(reservation.cancel_token || "")}`;
const phoneNumber = String(process.env.CONTACT_PHONE || "+48 000 000 000");
const subject = `Potwierdzenie rezerwacji ${reservation.order_number || ""}`;
const text = bookingConfirmationMessage({ reservation, phoneNumber, cancelUrl }) + `\nŁódka: ${resourceName}`;
if (customerEmail) {
await sendMail({ to: customerEmail, subject, text });
}
if (recipients.length) {
await sendMail({ to: recipients.join(","), subject: `[OWNER COPY] ${subject}`, text });
}
}
async function sendCancellationEmails({ db, reservation, reason }) {
const customerEmail = String(reservation.customer_email || "").trim().toLowerCase();
const recipients = await getSupportRecipients(db);
const subject = `Anulowanie rezerwacji ${reservation.order_number || ""}`;
const text = [
`Rezerwacja została anulowana.`,
`Numer rezerwacji: ${reservation.order_number || "-"}`,
`Powód: ${reason || "Klient"}`,
`Status płatności: ${reservation.payment_status || "-"}`,
].join("\n");
if (customerEmail) {
await sendMail({ to: customerEmail, subject, text });
}
if (recipients.length) {
await sendMail({ to: recipients.join(","), subject: `[OWNER COPY] ${subject}`, text });
}
}
module.exports = {
sendReservationConfirmedEmails,
sendCancellationEmails,
formatDateTime,
};