more robust send loop

This commit is contained in:
Ivan Socolsky 2015-04-29 21:03:47 -03:00
parent 8efc0065e6
commit 3d901852ab
3 changed files with 29 additions and 10 deletions

View File

@ -31,10 +31,17 @@ var EMAIL_TYPES = {
function EmailService(opts) { function EmailService(opts) {
$.checkArgument(opts);
opts.email = opts.email || {};
this.storage = opts.storage; this.storage = opts.storage;
this.lock = opts.lock; this.lock = opts.lock;
this.mailer = opts.mailer || nodemailer.createTransport(opts.email); this.mailer = opts.mailer || nodemailer.createTransport(opts.email);
this.from = opts.email.from;
$.checkState(this.mailer); $.checkState(this.mailer);
$.checkState(this.from);
}; };
// TODO: cache for X minutes // TODO: cache for X minutes
@ -95,7 +102,7 @@ EmailService.prototype._getDataForTemplate = function(notification, cb) {
data.walletM = wallet.m; data.walletM = wallet.m;
data.walletN = wallet.n; data.walletN = wallet.n;
var copayer = _.find(wallet.copayers, { var copayer = _.find(wallet.copayers, {
copayerId: notification.creatorId id: notification.creatorId
}); });
if (copayer) { if (copayer) {
data.creatorId = copayer.id; data.creatorId = copayer.id;
@ -162,6 +169,7 @@ EmailService.prototype.sendEmail = function(notification, cb) {
var email = Model.Email.create({ var email = Model.Email.create({
walletId: notification.walletId, walletId: notification.walletId,
copayerId: recipient.copayerId, copayerId: recipient.copayerId,
from: self.from,
to: recipient.emailAddress, to: recipient.emailAddress,
subject: content.subject, subject: content.subject,
body: content.body, body: content.body,
@ -174,7 +182,9 @@ EmailService.prototype.sendEmail = function(notification, cb) {
function(emails, next) { function(emails, next) {
async.each(emails, function(email, next) { async.each(emails, function(email, next) {
self._send(email, next); self._send(email, next);
}, next); }, function(err) {
return next();
});
}, },
], cb); ], cb);
}; };

View File

@ -1,5 +1,8 @@
'use strict'; 'use strict';
var _ = require('lodash');
var Uuid = require('uuid');
function Email() { function Email() {
this.version = '1.0.0'; this.version = '1.0.0';
}; };
@ -9,7 +12,9 @@ Email.create = function(opts) {
var x = new Email(); var x = new Email();
x.createdOn = Math.floor(Date.now() / 1000); var now = Date.now();
x.createdOn = Math.floor(now / 1000);
x.id = _.padLeft(now, 14, '0') + Uuid.v4();
x.walletId = opts.walletId; x.walletId = opts.walletId;
x.copayerId = opts.copayerId; x.copayerId = opts.copayerId;
x.from = opts.from; x.from = opts.from;
@ -26,6 +31,7 @@ Email.fromObj = function(obj) {
var x = new Email(); var x = new Email();
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId; x.walletId = obj.walletId;
x.copayerId = obj.copayerId; x.copayerId = obj.copayerId;
x.from = obj.from; x.from = obj.from;

View File

@ -245,7 +245,7 @@ describe('Wallet service', function() {
// uri: 'mongodb://localhost:27017/bws_test' // uri: 'mongodb://localhost:27017/bws_test'
// } // }
// }, done); // }, done);
}); });
beforeEach(function(done) { beforeEach(function(done) {
resetDb(function() { resetDb(function() {
blockchainExplorer = sinon.stub(); blockchainExplorer = sinon.stub();
@ -254,6 +254,9 @@ describe('Wallet service', function() {
storage: storage, storage: storage,
blockchainExplorer: blockchainExplorer, blockchainExplorer: blockchainExplorer,
mailer: mailer, mailer: mailer,
email: {
from: 'bws@dummy.net',
}
}, done); }, done);
}); });
}); });