1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00

import email templates into code for easy packaging - incomplete

This commit is contained in:
Massimo Candela
2019-10-26 13:39:50 +02:00
parent 9d80390fe9
commit 6ba24d4baa
2 changed files with 25 additions and 12 deletions

View File

@@ -32,7 +32,7 @@
*/
import fs from "fs";
import path from "path";
const templateHijack = '${summary}\n\
\n\
@@ -88,34 +88,42 @@ See in BGPlay: ${bgplay}';
export default class emailTemplates {
constructor(logger) {
const directory = 'email_templates/';
this.indexedFiles = {};
const templateFiles = [
{
file: 'reports/email_templates/hijack.txt',
channel: 'hijack',
content: templateHijack
},
{
file: 'reports/email_templates/newprefix.txt',
channel: 'newprefix',
content: templateNewPrefix
},
{
file: 'reports/email_templates/path.txt',
channel: 'path',
content: templatePath
},
{
file: 'reports/email_templates/software-update.txt',
channel: 'software-update',
content: templateSoftwareUpdate
},
{
file: 'reports/email_templates/visibility.txt',
channel: 'visibility',
content: templateVisibility
}
];
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
templateFiles
.forEach(template => {
try {
if (!fs.existsSync(template.file)) {
fs.writeFileSync(template.file, template.content);
const file = path.resolve(directory, template.channel + '.txt');
if (!fs.existsSync(file)) {
fs.writeFileSync(file, template.content);
this.indexedFiles[template.channel] = template.content;
}
} catch (error) {
logger.log({
@@ -124,7 +132,13 @@ export default class emailTemplates {
});
}
});
}
getTemplate = (channel) => {
if (!this.indexedFiles[channel]) {
this.indexedFiles[channel] = fs.readFileSync(); // get it from fileee
}
return this.indexedFiles[channel];
};
}

View File

@@ -42,7 +42,7 @@ export default class ReportEmail extends Report {
constructor(channels,params, env) {
super(channels, params, env);
new emailTemplates(this.logger);
this.emailTemplates = new emailTemplates(this.logger);
this.templates = {};
this.emailBacklog = [];
@@ -66,8 +66,7 @@ export default class ReportEmail extends Report {
for (let channel of channels) {
try {
const file = path.resolve('reports/email_templates', `${channel}.txt`);
this.templates[channel] = fs.readFileSync(file, "utf8");
this.templates[channel] = this.emailTemplates.getTemplate(channel);
} catch (error) {
this.logger.log({
level: 'error',