services/MailService.jsjavascript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const winston_1 = __importDefault(require("../plugins/winston.js"));
const nodemailer_1 = require("../plugins/nodemailer");
const { timeStringToMillis } = require("../utils/time");
const i18n = require("i18n");
/**
* Service for sending emails and managing mail operations.
* Handles email composition, sending, and related logic.
*
* @class MailService
*/
class MailService {
adminEmails;
/**
* Creates an instance of MailService.
* Initializes the admin email addresses from environment variables.
*
* @constructor
*/
constructor() {
if (process.env.SMTP_SERVER &&
process.env.SMTP_PORT &&
process.env.SMTP_FROM_ADDRESS &&
process.env.SMTP_USER &&
process.env.SMTP_PASSWORD &&
process.env.EMAIL_ADMIN_ADDRESSES &&
process.env.BACKEND_DOMAIN) {
(0, nodemailer_1.verify)();
}
this.adminEmails = process.env.EMAIL_ADMIN_ADDRESSES?.split(",") || [];
}
/**
* Sends a notice to admin emails about a new user registration.
* @param {string} username - The username of the new user.
* @param {UUID} userId - The ID of the new user.
* @param {string} userEmail - The email of the new user.
* @returns {Promise<void[]>} Resolves when all emails are sent.
*/
sendUserRegistrationNotice(username, userId, userEmail) {
winston_1.default.debug(`MailService sendUserRegistrationNotice ${JSON.stringify({
username,
userId,
userEmail,
})}`);
return Promise.all(this.adminEmails.map((recipient) => {
return (0, nodemailer_1.sendMail)(recipient, "Neuer Nutzer", "newUser.ejs", {
username,
userId,
userEmail,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], "de");
}));
}
/**
* Sends a welcome email to a new user.
* @param {string} username - The username of the new user.
* @param {UUID} userId - The ID of the new user.
* @param {string} userEmail - The email of the new user.
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendWelcomeEmail(username, userId, userEmail, locale) {
winston_1.default.debug(`MailService sendWelcomeEmail ${JSON.stringify({
username,
userId,
userEmail,
locale,
})}`);
return (0, nodemailer_1.sendMail)(userEmail, i18n.__({ phrase: "mail.welcome.willkommen-beim-choreo-planer", locale }), "welcome.ejs", {
username,
userId,
backendDomain: process.env.BACKEND_DOMAIN,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], locale);
}
/**
* Sends an email confirmation request to a user.
* @param {string} username - The username of the user.
* @param {UUID} userId - The ID of the user.
* @param {string} userEmail - The email of the user.
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendEmailConfirmationEmail(username, userId, userEmail, locale) {
winston_1.default.debug(`MailService sendEmailConfirmationEmail ${JSON.stringify({
username,
userId,
userEmail,
locale,
})}`);
return (0, nodemailer_1.sendMail)(userEmail, i18n.__({
phrase: "mail.confirmEmail.bitte-bestaetige-deine-e-mail-adresse",
locale,
}), "confirmEmail.ejs", {
username,
userId,
userEmail,
backendDomain: process.env.BACKEND_DOMAIN,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], locale);
}
/**
* Sends a feedback notice to admin emails.
* @param {string} username - The username of the user providing feedback.
* @param {string} userEmail - The email of the user providing feedback.
* @param {number} stars - The rating provided by the user.
* @param {string} text - The feedback text provided by the user.
* @returns {Promise<void[]>} Resolves when all emails are sent.
*/
sendFeedbackNotice(username, userEmail, stars, text) {
winston_1.default.debug(`MailService sendFeebackNotice ${JSON.stringify({
username,
userEmail,
stars,
text,
})}`);
return Promise.all(this.adminEmails.map((recipient) => {
return (0, nodemailer_1.sendMail)(recipient, "Feedback ist eingegangen", "newFeedback.ejs", {
username,
userEmail,
stars,
text,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], "en");
}));
}
/**
* Sends an SSO login email to a user.
* @param {string} userEmail - The email of the user.
* @param {string} username - The username of the user.
* @param {string} ssoToken - The SSO token for login.
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendSsoEmail(userEmail, username, ssoToken, locale) {
winston_1.default.debug(`MailService sendSsoEmail ${JSON.stringify({
userEmail,
username,
ssoToken,
locale,
})}`);
return (0, nodemailer_1.sendMail)(userEmail, i18n.__({ phrase: "mail.ssoLogin.dein-login-link", locale }), "ssoLogin.ejs", {
username,
ssoToken,
frontendDomain: process.env.FRONTEND_DOMAIN,
expirationDate: new Date(new Date().valueOf() +
timeStringToMillis(process.env.SSO_TOKEN_EXPIRES_IN)).toLocaleString(locale, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
}),
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], locale);
}
/**
* Sends a notification email to a user.
* @param {string} userEmail - The email of the user.
* @param {string} username - The username of the user.
* @param {string} ssoToken - The SSO token for login.
* @param {string|null} notificationTitle - The title of the notification.
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendNotificationNotice(userEmail, username, ssoToken, notificationTitle = null, locale) {
winston_1.default.debug(`MailService sendNotificationNotice ${JSON.stringify({
userEmail,
username,
ssoToken,
notificationTitle,
locale,
})}`);
return (0, nodemailer_1.sendMail)(userEmail, i18n.__({ phrase: "mail.newNotification.neue-benachrichtigung", locale }), "newNotification.ejs", {
username,
ssoToken,
notificationTitle,
frontendDomain: process.env.FRONTEND_DOMAIN,
expirationDate: new Date(new Date().valueOf() +
timeStringToMillis(process.env.SSO_TOKEN_EXPIRES_IN)).toLocaleString(locale, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
}),
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], locale);
}
/**
* Sends a contact message email to the support team.
* @param {string} name - The name of the sender.
* @param {string} email - The email of the sender.
* @param {string} subject - The subject of the message.
* @param {string} message - The content of the message.
* @param {string} category - The category of the message (e.g., "bug", "feedback", "other").
* @param {User|null} user - The user object if the sender is logged in, otherwise null.
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendContactMessage(name, email, subject, message, category, user, messageLocale) {
winston_1.default.debug(`MailService sendContactMessage ${JSON.stringify({
name,
email,
subject,
message,
category,
user,
messageLocale,
})}`);
return Promise.all(this.adminEmails.map((adminEmail) => {
return (0, nodemailer_1.sendMail)(adminEmail, i18n.__({
phrase: "mail.newContactMessage.new-message",
locale: "en",
}) +
" " +
subject, "newContactMessage.ejs", {
name,
email,
subject,
message,
category,
user,
messageLocale,
frontendDomain: process.env.FRONTEND_DOMAIN,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], "en");
}));
}
/**
* Sends a contact message email to the user.
* @param {string} name - The name of the sender.
* @param {string} email - The email of the sender.
* @param {string} subject - The subject of the message.
* @param {string} message - The content of the message.
* @param {string} category - The category of the message (e.g., "bug", "feedback", "other").
* @param {string} locale - The locale for the email content.
* @returns {Promise<void>} Resolves when the email is sent.
*/
sendContactNotification(name, email, subject, message, category, locale) {
winston_1.default.debug(`MailService sendContactNotification ${JSON.stringify({
name,
email,
subject,
message,
category,
locale,
})}`);
return (0, nodemailer_1.sendMail)(email, i18n.__({ phrase: "mail.newContactNotification.new-message", locale }) +
" " +
subject, "newContactNotification.ejs", {
name,
email,
subject,
message,
category,
locale,
frontendDomain: process.env.FRONTEND_DOMAIN,
}, [
{
filename: "logo.png",
path: "https://www.choreo-planer.de/Icon.png",
cid: "choreo-planer-icon",
},
], "en");
}
}
exports.default = new MailService();