Moved mail template rendering into own service.

This commit is contained in:
baldo 2016-06-11 12:32:20 +02:00
parent 26aaec385a
commit c038546ac2
3 changed files with 77 additions and 39 deletions

View file

@ -26,6 +26,7 @@ require('./resources/nodeResource');
require('./resources/monitoringResource'); require('./resources/monitoringResource');
require('./services/mailService'); require('./services/mailService');
require('./services/mailTemplateService');
require('./services/nodeService'); require('./services/nodeService');
require('./services/monitoringService'); require('./services/monitoringService');

View file

@ -3,7 +3,7 @@
angular.module('ffffng') angular.module('ffffng')
.service('MailService', function ( .service('MailService', function (
Database, Database,
UrlBuilder, MailTemplateService,
config, config,
_, _,
async, async,
@ -25,10 +25,7 @@ angular.module('ffffng')
} }
)); ));
var htmlToText = require('nodemailer-html-to-text').htmlToText; MailTemplateService.configureTransporter(transporter);
transporter.use('compile', htmlToText({
tables: ['.table']
}));
function sendMail(options, callback) { function sendMail(options, callback) {
Logger Logger
@ -39,44 +36,17 @@ angular.module('ffffng')
options.id, options.email, options.failures options.id, options.email, options.failures
); );
var templateBasePath = __dirname + '/../mailTemplates/' + options.email; MailTemplateService.render(options, function (err, renderedTemplate) {
async.parallel({
subject: _.partial(fs.readFile, templateBasePath + '.subject.txt'),
body: _.partial(fs.readFile, templateBasePath + '.body.html')
},
function (err, templates) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var data = deepExtend( var mailOptions = {
{}, from: options.sender,
options.data, to: options.recipient,
{ subject: renderedTemplate.subject,
community: config.client.community, html: renderedTemplate.body
editNodeUrl: UrlBuilder.editNodeUrl() };
}
);
function render(field) {
return _.template(templates[field].toString())(data);
}
var mailOptions;
try {
mailOptions = {
from: options.sender,
to: options.recipient,
subject: _.trim(render('subject')),
html: render('body')
};
}
catch (error) {
Logger
.tag('mail', 'queue')
.error('Error rendering template for pending mail[' + options.id + ']:', error);
return callback(error);
}
transporter.sendMail(mailOptions, function (err) { transporter.sendMail(mailOptions, function (err) {
if (err) { if (err) {

View file

@ -0,0 +1,67 @@
'use strict';
angular.module('ffffng')
.service('MailTemplateService', function (
UrlBuilder,
config,
_,
async,
deepExtend,
fs,
Logger
) {
var templateBasePath = __dirname + '/../mailTemplates';
return {
configureTransporter: function (transporter) {
var htmlToText = require('nodemailer-html-to-text').htmlToText;
transporter.use('compile', htmlToText({
tables: ['.table']
}));
},
render: function (mailOptions, callback) {
var templatePathPrefix = templateBasePath + '/' + mailOptions.email
async.parallel({
subject: _.partial(fs.readFile, templatePathPrefix + '.subject.txt'),
body: _.partial(fs.readFile, templatePathPrefix + '.body.html')
},
function (err, templates) {
if (err) {
return callback(err);
}
var data = deepExtend(
{},
mailOptions.data,
{
community: config.client.community,
editNodeUrl: UrlBuilder.editNodeUrl()
}
);
function render(field) {
return _.template(templates[field].toString())(data);
}
var renderedTemplate;
try {
renderedTemplate = {
subject: _.trim(render('subject')),
body: render('body')
};
}
catch (error) {
Logger
.tag('mail', 'template')
.error('Error rendering template for mail[' + mailOptions.id + ']:', error);
return callback(error);
}
callback(null, renderedTemplate);
}
);
}
};
});