Browse Source

filter recipient list

activeAddress
Ivan Socolsky 10 years ago
parent
commit
4a0a3f1bad
  1. 77
      lib/emailservice.js
  2. 2
      lib/lock.js
  3. 2
      lib/server.js

77
lib/emailservice.js

@ -64,19 +64,44 @@ EmailService.prototype._applyTemplate = function(template, data, cb) {
return cb(null, result);
};
EmailService.prototype._getEmailAddresses = function(walletId, cb) {
self.storage.fetchPreferences(walletId, null, function(err, preferences) {
EmailService.prototype._getRecipientsList = function(notification, emailType, cb) {
var self = this;
self.storage.fetchPreferences(notification.walletId, null, function(err, preferences) {
if (err) return cb(err);
if (_.isEmpty(preferences)) return cb(null, {});
var addressesByCopayer = _.reduce(preferences, function(memo, p) {
if (p.email) {
memo[p.copayerId] = p.email;
}
return memo;
}, {});
var recipients = _.compact(_.map(preferences, function(p) {
if (!p.email) return;
if (notification.creatorId == p.copayerId && !emailType.notifyDoer) return;
return {
copayerId: p.copayerId,
emailAddress: p.email
};
}));
return cb(null, recipients);
});
};
EmailService.prototype._getDataForTemplate = function(notification, cb) {
var self = this;
return cb(null, addressesByCopayer);
var data = _.cloneDeep(notification.data);
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
if (err) return cb(err);
data.walletId = wallet.id;
data.walletName = wallet.name;
data.walletM = wallet.m;
data.walletN = wallet.n;
var copayer = _.find(wallet.copayers, {
copayerId: notification.creatorId
});
if (copayer) {
data.creatorId = copayer.id;
data.creatorName = copayer.name;
}
return cb(null, data);
});
};
@ -99,33 +124,43 @@ EmailService.prototype._send = function(email, cb) {
});
};
EmailService.prototype._generateFromNotification = function(notification, cb) {
EmailService.prototype.sendEmail = function(notification, cb) {
var self = this;
var emailType = EMAIL_TYPES[notification.type];
if (!emailType) return cb();
var emailByCopayer;
var recipientsList;
async.waterfall([
function(next) {
self._getEmailAddresses(notification.walletId, next);
self._getRecipientsList(notification, emailType, function(err, list) {
if (_.isEmpty(list)) return cb();
recipientsList = list;
return next();
});
},
function(emailAddresses, next) {
if (_.isEmpty(emailAddresses)) return cb();
emailByCopayer = emailAddresses;
self._readTemplate(emailType.filename, next);
function(next) {
async.parallel([
function(next) {
self._readTemplate(emailType.filename, next);
},
function(next) {
self._getDataForTemplate(notification, next);
},
], next);
},
function(template, next) {
self._applyTemplate(template, notification.data, next);
function(template, data, next) {
self._applyTemplate(template, data, next);
},
function(content, next) {
_.each(emailByCopayer, function(address, copayerId) {
_.each(recipientsList, function(recipient) {
var email = Model.Email.create({
walletId: notification.walletId,
copayerId: copayerId,
to: address,
copayerId: recipient.copayerId,
to: recipient.emailAddress,
subject: content.subject,
body: content.body,
});

2
lib/lock.js

@ -12,7 +12,7 @@ function Lock(opts) {
if (opts.lockerServer) {
this.lock = new RemoteLock(opts.lockerServer.port, opts.lockerServer.host);
log.info('Using locker server:' + opts.lockerServer.host + ':' + opts.lockerServer.port);
log.info('Using locker server:' + opts.lockerServer.host + ':' + opts.lockerServer.port);
this.lock.on('reset', function() {
log.debug('Locker server reset');

2
lib/server.js

@ -353,7 +353,7 @@ WalletService.prototype._notify = function(type, data, opts, cb) {
this.storage.storeNotification(walletId, notification, function() {
self.messageBroker.send(notification);
self.emailService._generateFromNotification(notification, function() {
self.emailService.sendEmail(notification, function() {
if (cb) return cb();
});
});

Loading…
Cancel
Save