|
|
@ -204,58 +204,69 @@ EmailService.prototype.sendEmail = function(notification, cb) { |
|
|
|
self._getRecipientsList(notification, emailType, function(err, recipientsList) { |
|
|
|
if (_.isEmpty(recipientsList)) return cb(); |
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
// TODO: Optimize so one process does not have to wait until all others are done
|
|
|
|
// Instead set a flag somewhere in the db to indicate that this process is free
|
|
|
|
// to serve another request.
|
|
|
|
self.lock.runLocked('email-' + notification.id, cb, function(cb) { |
|
|
|
self.storage.fetchEmailByNotification(notification.id, function(err, email) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (email) return cb(); |
|
|
|
|
|
|
|
function(next) { |
|
|
|
async.parallel([ |
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._readTemplate(emailType.filename, next); |
|
|
|
async.parallel([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._readTemplate(emailType.filename, next); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self._getDataForTemplate(notification, next); |
|
|
|
}, |
|
|
|
], function(err, res) { |
|
|
|
next(err, res[0], res[1]); |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self._getDataForTemplate(notification, next); |
|
|
|
function(template, data, next) { |
|
|
|
self._applyTemplate(template, data, next); |
|
|
|
}, |
|
|
|
], function(err, res) { |
|
|
|
next(err, res[0], res[1]); |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(template, data, next) { |
|
|
|
self._applyTemplate(template, data, next); |
|
|
|
}, |
|
|
|
function(content, next) { |
|
|
|
async.map(recipientsList, function(recipient, next) { |
|
|
|
var email = Model.Email.create({ |
|
|
|
walletId: notification.walletId, |
|
|
|
copayerId: recipient.copayerId, |
|
|
|
from: self.from, |
|
|
|
to: recipient.emailAddress, |
|
|
|
subject: content.subject, |
|
|
|
body: content.body, |
|
|
|
}); |
|
|
|
self.storage.storeEmail(email, function(err) { |
|
|
|
return next(err, email); |
|
|
|
}); |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
function(emails, next) { |
|
|
|
async.each(emails, function(email, next) { |
|
|
|
self._send(email, function(err) { |
|
|
|
if (err) { |
|
|
|
email.setFail(); |
|
|
|
} else { |
|
|
|
email.setSent(); |
|
|
|
} |
|
|
|
self.storage.storeEmail(email, next); |
|
|
|
}); |
|
|
|
}, function(err) { |
|
|
|
return next(); |
|
|
|
function(content, next) { |
|
|
|
async.map(recipientsList, function(recipient, next) { |
|
|
|
var email = Model.Email.create({ |
|
|
|
walletId: notification.walletId, |
|
|
|
copayerId: recipient.copayerId, |
|
|
|
from: self.from, |
|
|
|
to: recipient.emailAddress, |
|
|
|
subject: content.subject, |
|
|
|
body: content.body, |
|
|
|
notificationId: notification.id, |
|
|
|
}); |
|
|
|
self.storage.storeEmail(email, function(err) { |
|
|
|
return next(err, email); |
|
|
|
}); |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
function(emails, next) { |
|
|
|
async.each(emails, function(email, next) { |
|
|
|
self._send(email, function(err) { |
|
|
|
if (err) { |
|
|
|
email.setFail(); |
|
|
|
} else { |
|
|
|
email.setSent(); |
|
|
|
} |
|
|
|
self.storage.storeEmail(email, next); |
|
|
|
}); |
|
|
|
}, function(err) { |
|
|
|
return next(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
if (err) { |
|
|
|
log.error('An error ocurred generating email notification', err); |
|
|
|
} |
|
|
|
return cb(err); |
|
|
|
}); |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
if (err) { |
|
|
|
log.error('An error ocurred generating email notification', err); |
|
|
|
} |
|
|
|
return cb(err); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|