2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
import deepExtend from "deep-extend";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { readFileSync, promises as fs } from "graceful-fs";
|
2020-04-10 00:43:15 +02:00
|
|
|
import moment from "moment";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { htmlToText } from "nodemailer-html-to-text";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
import { config } from "../config";
|
2020-04-10 00:43:15 +02:00
|
|
|
import Logger from "../logger";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { editNodeUrl } from "../utils/urlBuilder";
|
|
|
|
import { Transporter } from "nodemailer";
|
|
|
|
import { MailData, Mail } from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const templateBasePath = __dirname + "/../mailTemplates";
|
|
|
|
const snippetsBasePath = templateBasePath + "/snippets";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const templateFunctions: {
|
|
|
|
[key: string]:
|
|
|
|
| ((name: string, data: MailData) => string)
|
|
|
|
| ((data: MailData) => string)
|
|
|
|
| ((href: string, text: string) => string)
|
2022-08-23 20:08:53 +02:00
|
|
|
| ((unix: number) => string);
|
2022-07-18 17:49:42 +02:00
|
|
|
} = {};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-10 00:43:15 +02:00
|
|
|
function renderSnippet(this: any, name: string, data: MailData): string {
|
2022-08-23 20:08:53 +02:00
|
|
|
const snippetFile = snippetsBasePath + "/" + name + ".html";
|
|
|
|
|
|
|
|
return _.template(readFileSync(snippetFile).toString())(
|
|
|
|
deepExtend(
|
|
|
|
{},
|
|
|
|
this, // parent data
|
|
|
|
data,
|
|
|
|
templateFunctions
|
|
|
|
)
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-08-23 20:08:53 +02:00
|
|
|
function snippet(name: string): (this: any, data: MailData) => string {
|
2022-08-23 21:38:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-10 00:43:15 +02:00
|
|
|
return function (this: any, data: MailData): string {
|
|
|
|
return renderSnippet.bind(this)(name, data);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderLink(href: string, text: string): string {
|
|
|
|
// noinspection HtmlUnknownTarget
|
|
|
|
return _.template(
|
|
|
|
'<a href="<%- href %>#" style="color: #E5287A;"><%- text %></a>'
|
|
|
|
)({
|
|
|
|
href: href,
|
2022-08-23 20:08:53 +02:00
|
|
|
text: text || href,
|
2020-04-10 00:43:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderHR(): string {
|
|
|
|
return '<hr style="border-top: 1px solid #333333; border-left: 0; border-right: 0; border-bottom: 0;" />';
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatDateTime(unix: number): string {
|
2022-08-23 20:08:53 +02:00
|
|
|
return moment.unix(unix).locale("de").local().format("DD.MM.YYYY HH:mm");
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatFromNow(unix: number): string {
|
2022-08-23 20:08:53 +02:00
|
|
|
return moment.unix(unix).locale("de").fromNow();
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
templateFunctions.header = snippet("header");
|
|
|
|
templateFunctions.footer = snippet("footer");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
templateFunctions.monitoringFooter = snippet("monitoring-footer");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
templateFunctions.snippet = renderSnippet;
|
|
|
|
|
|
|
|
templateFunctions.link = renderLink;
|
|
|
|
templateFunctions.hr = renderHR;
|
|
|
|
|
|
|
|
templateFunctions.formatDateTime = formatDateTime;
|
|
|
|
templateFunctions.formatFromNow = formatFromNow;
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export function configureTransporter(transporter: Transporter): void {
|
|
|
|
transporter.use(
|
|
|
|
"compile",
|
|
|
|
htmlToText({
|
|
|
|
tables: [".table"],
|
|
|
|
})
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function render(
|
|
|
|
mailOptions: Mail
|
|
|
|
): Promise<{ subject: string; body: string }> {
|
|
|
|
const templatePathPrefix = templateBasePath + "/" + mailOptions.email;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const subject = await fs.readFile(templatePathPrefix + ".subject.txt");
|
|
|
|
const body = await fs.readFile(templatePathPrefix + ".body.html");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const data = deepExtend(
|
|
|
|
{},
|
|
|
|
mailOptions.data,
|
|
|
|
{
|
|
|
|
community: config.client.community,
|
2022-08-23 20:08:53 +02:00
|
|
|
editNodeUrl: editNodeUrl(),
|
2020-04-10 00:43:15 +02:00
|
|
|
},
|
|
|
|
templateFunctions
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return {
|
2022-07-28 13:16:13 +02:00
|
|
|
subject: _.template(subject.toString())(data).trim(),
|
2022-08-23 20:08:53 +02:00
|
|
|
body: _.template(body.toString())(data),
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("mail", "template").error(
|
|
|
|
"Error rendering template for mail[" + mailOptions.id + "]:",
|
|
|
|
error
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|