diff --git a/server/main.js b/server/main.js index 30dab51..06ac30b 100644 --- a/server/main.js +++ b/server/main.js @@ -26,6 +26,7 @@ require('./resources/nodeResource'); require('./resources/monitoringResource'); require('./services/mailService'); +require('./services/mailTemplateService'); require('./services/nodeService'); require('./services/monitoringService'); diff --git a/server/services/mailService.js b/server/services/mailService.js index 1ed6d84..06ffe15 100644 --- a/server/services/mailService.js +++ b/server/services/mailService.js @@ -3,7 +3,7 @@ angular.module('ffffng') .service('MailService', function ( Database, - UrlBuilder, + MailTemplateService, config, _, async, @@ -25,10 +25,7 @@ angular.module('ffffng') } )); - var htmlToText = require('nodemailer-html-to-text').htmlToText; - transporter.use('compile', htmlToText({ - tables: ['.table'] - })); + MailTemplateService.configureTransporter(transporter); function sendMail(options, callback) { Logger @@ -39,44 +36,17 @@ angular.module('ffffng') options.id, options.email, options.failures ); - var templateBasePath = __dirname + '/../mailTemplates/' + options.email; - async.parallel({ - subject: _.partial(fs.readFile, templateBasePath + '.subject.txt'), - body: _.partial(fs.readFile, templateBasePath + '.body.html') - }, - function (err, templates) { + MailTemplateService.render(options, function (err, renderedTemplate) { if (err) { return callback(err); } - var data = deepExtend( - {}, - options.data, - { - community: config.client.community, - 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); - } + var mailOptions = { + from: options.sender, + to: options.recipient, + subject: renderedTemplate.subject, + html: renderedTemplate.body + }; transporter.sendMail(mailOptions, function (err) { if (err) { diff --git a/server/services/mailTemplateService.js b/server/services/mailTemplateService.js new file mode 100644 index 0000000..26dcb45 --- /dev/null +++ b/server/services/mailTemplateService.js @@ -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); + } + ); + } + }; +});