ESLint: Auto reformat and fixing some warnings / errors.
This commit is contained in:
parent
5237db38e0
commit
91690509d3
50 changed files with 2141 additions and 1493 deletions
server/services
|
@ -1,38 +1,40 @@
|
|||
import _ from "lodash";
|
||||
import deepExtend from "deep-extend";
|
||||
import {readFileSync, promises as fs} from "graceful-fs";
|
||||
import { readFileSync, promises as fs } from "graceful-fs";
|
||||
import moment from "moment";
|
||||
import {htmlToText} from "nodemailer-html-to-text";
|
||||
import { htmlToText } from "nodemailer-html-to-text";
|
||||
|
||||
import {config} from "../config";
|
||||
import { config } from "../config";
|
||||
import Logger from "../logger";
|
||||
import {editNodeUrl} from "../utils/urlBuilder";
|
||||
import {Transporter} from "nodemailer";
|
||||
import {MailData, Mail} from "../types";
|
||||
import { editNodeUrl } from "../utils/urlBuilder";
|
||||
import { Transporter } from "nodemailer";
|
||||
import { MailData, Mail } from "../types";
|
||||
|
||||
const templateBasePath = __dirname + '/../mailTemplates';
|
||||
const snippetsBasePath = templateBasePath + '/snippets';
|
||||
const templateBasePath = __dirname + "/../mailTemplates";
|
||||
const snippetsBasePath = templateBasePath + "/snippets";
|
||||
|
||||
const templateFunctions: {
|
||||
[key: string]:
|
||||
| ((name: string, data: MailData) => string)
|
||||
| ((data: MailData) => string)
|
||||
| ((href: string, text: string) => string)
|
||||
| ((unix: number) => string)
|
||||
| ((unix: number) => string);
|
||||
} = {};
|
||||
|
||||
function renderSnippet(this: any, name: string, data: MailData): string {
|
||||
const snippetFile = snippetsBasePath + '/' + name + '.html';
|
||||
const snippetFile = snippetsBasePath + "/" + name + ".html";
|
||||
|
||||
return _.template(readFileSync(snippetFile).toString())(deepExtend(
|
||||
{},
|
||||
this, // parent data
|
||||
data,
|
||||
templateFunctions
|
||||
));
|
||||
return _.template(readFileSync(snippetFile).toString())(
|
||||
deepExtend(
|
||||
{},
|
||||
this, // parent data
|
||||
data,
|
||||
templateFunctions
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function snippet(name: string): ((this: any, data: MailData) => string) {
|
||||
function snippet(name: string): (this: any, data: MailData) => string {
|
||||
return function (this: any, data: MailData): string {
|
||||
return renderSnippet.bind(this)(name, data);
|
||||
};
|
||||
|
@ -44,7 +46,7 @@ function renderLink(href: string, text: string): string {
|
|||
'<a href="<%- href %>#" style="color: #E5287A;"><%- text %></a>'
|
||||
)({
|
||||
href: href,
|
||||
text: text || href
|
||||
text: text || href,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -53,17 +55,17 @@ function renderHR(): string {
|
|||
}
|
||||
|
||||
function formatDateTime(unix: number): string {
|
||||
return moment.unix(unix).locale('de').local().format('DD.MM.YYYY HH:mm');
|
||||
return moment.unix(unix).locale("de").local().format("DD.MM.YYYY HH:mm");
|
||||
}
|
||||
|
||||
function formatFromNow(unix: number): string {
|
||||
return moment.unix(unix).locale('de').fromNow();
|
||||
return moment.unix(unix).locale("de").fromNow();
|
||||
}
|
||||
|
||||
templateFunctions.header = snippet('header');
|
||||
templateFunctions.footer = snippet('footer');
|
||||
templateFunctions.header = snippet("header");
|
||||
templateFunctions.footer = snippet("footer");
|
||||
|
||||
templateFunctions.monitoringFooter = snippet('monitoring-footer');
|
||||
templateFunctions.monitoringFooter = snippet("monitoring-footer");
|
||||
|
||||
templateFunctions.snippet = renderSnippet;
|
||||
|
||||
|
@ -73,24 +75,29 @@ templateFunctions.hr = renderHR;
|
|||
templateFunctions.formatDateTime = formatDateTime;
|
||||
templateFunctions.formatFromNow = formatFromNow;
|
||||
|
||||
export function configureTransporter (transporter: Transporter): void {
|
||||
transporter.use('compile', htmlToText({
|
||||
tables: ['.table']
|
||||
}));
|
||||
export function configureTransporter(transporter: Transporter): void {
|
||||
transporter.use(
|
||||
"compile",
|
||||
htmlToText({
|
||||
tables: [".table"],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function render(mailOptions: Mail): Promise<{subject: string, body: string}> {
|
||||
const templatePathPrefix = templateBasePath + '/' + mailOptions.email;
|
||||
export async function render(
|
||||
mailOptions: Mail
|
||||
): Promise<{ subject: string; body: string }> {
|
||||
const templatePathPrefix = templateBasePath + "/" + mailOptions.email;
|
||||
|
||||
const subject = await fs.readFile(templatePathPrefix + '.subject.txt');
|
||||
const body = await fs.readFile(templatePathPrefix + '.body.html');
|
||||
const subject = await fs.readFile(templatePathPrefix + ".subject.txt");
|
||||
const body = await fs.readFile(templatePathPrefix + ".body.html");
|
||||
|
||||
const data = deepExtend(
|
||||
{},
|
||||
mailOptions.data,
|
||||
{
|
||||
community: config.client.community,
|
||||
editNodeUrl: editNodeUrl()
|
||||
editNodeUrl: editNodeUrl(),
|
||||
},
|
||||
templateFunctions
|
||||
);
|
||||
|
@ -98,12 +105,13 @@ export async function render(mailOptions: Mail): Promise<{subject: string, body:
|
|||
try {
|
||||
return {
|
||||
subject: _.template(subject.toString())(data).trim(),
|
||||
body: _.template(body.toString())(data)
|
||||
body: _.template(body.toString())(data),
|
||||
};
|
||||
} catch (error) {
|
||||
Logger
|
||||
.tag('mail', 'template')
|
||||
.error('Error rendering template for mail[' + mailOptions.id + ']:', error);
|
||||
Logger.tag("mail", "template").error(
|
||||
"Error rendering template for mail[" + mailOptions.id + "]:",
|
||||
error
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue