Browse Source

include unit when rendering template

activeAddress
Ivan Socolsky 10 years ago
parent
commit
2d5fdaca1b
  1. 1
      config.js
  2. 46
      lib/emailservice.js
  3. 5
      test/integration/server.js

1
config.js

@ -54,6 +54,7 @@ var config = {
from: 'wallet-service@bitcore.io', from: 'wallet-service@bitcore.io',
templatePath: './lib/templates', templatePath: './lib/templates',
defaultLanguage: 'en', defaultLanguage: 'en',
defaultUnit: 'btc',
}, },
}; };
module.exports = config; module.exports = config;

46
lib/emailservice.js

@ -66,6 +66,7 @@ EmailService.prototype.start = function(opts, cb) {
var self = this; var self = this;
self.defaultLanguage = opts.defaultLanguage || 'en'; self.defaultLanguage = opts.defaultLanguage || 'en';
self.defaultUnit = opts.defaultUnit || 'btc';
self.templatePath = path.normalize((opts.templatePath || (__dirname + '/templates')) + '/'); self.templatePath = path.normalize((opts.templatePath || (__dirname + '/templates')) + '/');
async.parallel([ async.parallel([
@ -163,7 +164,7 @@ EmailService.prototype._getRecipientsList = function(notification, emailType, cb
copayerId: p.copayerId, copayerId: p.copayerId,
emailAddress: p.email, emailAddress: p.email,
language: p.language, language: p.language,
unit: p.unit || 'btc', unit: p.unit || self.defaultUnit,
}; };
})); }));
@ -171,13 +172,13 @@ EmailService.prototype._getRecipientsList = function(notification, emailType, cb
}); });
}; };
EmailService.prototype._getDataForTemplate = function(notification, cb) { EmailService.prototype._getDataForTemplate = function(notification, recipient, cb) {
var self = this; var self = this;
var data = _.cloneDeep(notification.data); var data = _.cloneDeep(notification.data);
data.subjectPrefix = _.trim(self.subjectPrefix) + ' '; data.subjectPrefix = _.trim(self.subjectPrefix) + ' ';
if (data.amount) { if (data.amount) {
data.amount = WalletUtils.formatAmount(+data.amount, 'bit') + ' bits'; data.amount = WalletUtils.formatAmount(+data.amount, recipient.unit) + ' ' + recipient.unit;
} }
self.storage.fetchWallet(notification.walletId, function(err, wallet) { self.storage.fetchWallet(notification.walletId, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
@ -228,30 +229,31 @@ EmailService.prototype._send = function(email, cb) {
EmailService.prototype._readAndApplyTemplates = function(notification, emailType, recipientsList, cb) { EmailService.prototype._readAndApplyTemplates = function(notification, emailType, recipientsList, cb) {
var self = this; var self = this;
async.waterfall([ async.map(recipientsList, function(recipient, next) {
async.waterfall([
function(next) { function(next) {
self._getDataForTemplate(notification, next); async.parallel([
},
function(data, next) {
var languages = _.uniq(_.pluck(recipientsList, 'language'));
async.map(languages, function(lang, next) {
async.waterfall([
function(next) { function(next) {
self._readTemplate(emailType.filename, lang, next); self._readTemplate(emailType.filename, recipient.language, next);
}, },
function(template, next) { function(next) {
self._applyTemplate(template, data, next); self._getDataForTemplate(notification, recipient, next);
}, },
], function(err, res) { ], next);
next(err, [lang, res]); },
}); function(result, next) {
}, function(err, res) { var template = result[0];
return next(err, _.zipObject(res)); var data = result[1];
}); self._applyTemplate(template, data, next);
}, },
], cb); ], function(err, res) {
next(err, [recipient.language, res]);
});
}, function(err, res) {
return cb(err, _.zipObject(res));
});
}; };
EmailService.prototype.sendEmail = function(notification, cb) { EmailService.prototype.sendEmail = function(notification, cb) {

5
test/integration/server.js

@ -321,6 +321,7 @@ describe('Wallet service', function() {
helpers.getAuthServer(copayer.id, function(server) { helpers.getAuthServer(copayer.id, function(server) {
server.savePreferences({ server.savePreferences({
email: 'copayer' + (++i) + '@domain.com', email: 'copayer' + (++i) + '@domain.com',
unit: 'bit',
}, next); }, next);
}); });
}, function(err) { }, function(err) {
@ -543,7 +544,7 @@ describe('Wallet service', function() {
}); });
}); });
it.only('should build each email using preferences of the copayers', function(done) { it('should build each email using preferences of the copayers', function(done) {
// Set same email address for copayer1 and copayer2 // Set same email address for copayer1 and copayer2
server.savePreferences({ server.savePreferences({
email: 'copayer1@domain.com', email: 'copayer1@domain.com',
@ -571,7 +572,7 @@ describe('Wallet service', function() {
spanish.from.should.equal('bws@dummy.net'); spanish.from.should.equal('bws@dummy.net');
spanish.subject.should.contain('Nuevo pago recibido'); spanish.subject.should.contain('Nuevo pago recibido');
spanish.text.should.contain(wallet.name); spanish.text.should.contain(wallet.name);
spanish.text.should.contain('0.123000 btc'); spanish.text.should.contain('0.123 btc');
var english = _.find(emails, { var english = _.find(emails, {
to: 'copayer2@domain.com' to: 'copayer2@domain.com'
}); });

Loading…
Cancel
Save