Transactional mail: use tools to format message

This commit is contained in:
Alexey Zinkevych 2019-04-02 16:15:35 +03:00
parent 76b4f8b8c2
commit e588e218b6
2 changed files with 18 additions and 17 deletions

View file

@ -1,6 +1,7 @@
'use strict';
const mailers = require('./mailers');
const tools = require('./tools');
const templates = require('../models/templates');
class TemplateSender {
@ -21,11 +22,13 @@ class TemplateSender {
templates.getById(options.context, this.templateId, false)
]);
const html = this._substituteVariables(
const html = tools.formatTemplate(
template.html,
options.variables
null,
options.variables,
true
);
const subject = this._substituteVariables(
const subject = tools.formatTemplate(
options.subject || template.description || template.name,
options.variables
);
@ -63,17 +66,6 @@ class TemplateSender {
throw new Error('Missing locale');
}
}
_substituteVariables(html, variables) {
if (!variables) {
return html;
}
return Object.keys(variables).reduce(
(res, key) =>
res.replace(new RegExp(`\\[${key}\\]`, 'gmi'), variables[key]),
html
);
}
}
module.exports = TemplateSender;