|
|
@ -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, |
|
|
|
}); |
|
|
|