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',
templatePath: './lib/templates',
defaultLanguage: 'en',
defaultUnit: 'btc',
},
};
module.exports = config;

46
lib/emailservice.js

@ -66,6 +66,7 @@ EmailService.prototype.start = function(opts, cb) {
var self = this;
self.defaultLanguage = opts.defaultLanguage || 'en';
self.defaultUnit = opts.defaultUnit || 'btc';
self.templatePath = path.normalize((opts.templatePath || (__dirname + '/templates')) + '/');
async.parallel([
@ -163,7 +164,7 @@ EmailService.prototype._getRecipientsList = function(notification, emailType, cb
copayerId: p.copayerId,
emailAddress: p.email,
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 data = _.cloneDeep(notification.data);
data.subjectPrefix = _.trim(self.subjectPrefix) + ' ';
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) {
if (err) return cb(err);
@ -228,30 +229,31 @@ EmailService.prototype._send = function(email, cb) {
EmailService.prototype._readAndApplyTemplates = function(notification, emailType, recipientsList, cb) {
var self = this;
async.waterfall([
async.map(recipientsList, function(recipient, next) {
async.waterfall([
function(next) {
self._getDataForTemplate(notification, next);
},
function(data, next) {
var languages = _.uniq(_.pluck(recipientsList, 'language'));
async.map(languages, function(lang, next) {
async.waterfall([
function(next) {
async.parallel([
function(next) {
self._readTemplate(emailType.filename, lang, next);
self._readTemplate(emailType.filename, recipient.language, next);
},
function(template, next) {
self._applyTemplate(template, data, next);
function(next) {
self._getDataForTemplate(notification, recipient, next);
},
], function(err, res) {
next(err, [lang, res]);
});
}, function(err, res) {
return next(err, _.zipObject(res));
});
},
], cb);
], next);
},
function(result, next) {
var template = result[0];
var data = result[1];
self._applyTemplate(template, data, next);
},
], function(err, res) {
next(err, [recipient.language, res]);
});
}, function(err, res) {
return cb(err, _.zipObject(res));
});
};
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) {
server.savePreferences({
email: 'copayer' + (++i) + '@domain.com',
unit: 'bit',
}, next);
});
}, 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
server.savePreferences({
email: 'copayer1@domain.com',
@ -571,7 +572,7 @@ describe('Wallet service', function() {
spanish.from.should.equal('bws@dummy.net');
spanish.subject.should.contain('Nuevo pago recibido');
spanish.text.should.contain(wallet.name);
spanish.text.should.contain('0.123000 btc');
spanish.text.should.contain('0.123 btc');
var english = _.find(emails, {
to: 'copayer2@domain.com'
});

Loading…
Cancel
Save