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

View File

@ -1,5 +1,8 @@
'use strict';
var _ = require('lodash');
var Uuid = require('uuid');
function Email() {
this.version = '1.0.0';
};
@ -9,7 +12,9 @@ Email.create = function(opts) {
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.copayerId = opts.copayerId;
x.from = opts.from;
@ -26,6 +31,7 @@ Email.fromObj = function(obj) {
var x = new Email();
x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId;
x.copayerId = obj.copayerId;
x.from = obj.from;

View File

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