return after email

This commit is contained in:
Ivan Socolsky 2015-04-30 14:50:48 -03:00
parent 3d901852ab
commit 17d97430ad
4 changed files with 80 additions and 82 deletions

View File

@ -45,7 +45,7 @@ var config = {
user: '',
pass: '',
},
from: '',
from: 'wallet-service@bitcore.io',
},
};
module.exports = config;

View File

@ -137,56 +137,51 @@ EmailService.prototype.sendEmail = function(notification, cb) {
var emailType = EMAIL_TYPES[notification.type];
if (!emailType) return cb();
var recipientsList;
self._getRecipientsList(notification, emailType, function(err, recipientsList) {
if (_.isEmpty(recipientsList)) return cb();
async.waterfall([
async.waterfall([
function(next) {
self._getRecipientsList(notification, emailType, function(err, list) {
if (_.isEmpty(list)) return cb();
recipientsList = list;
next();
});
},
function(next) {
async.parallel([
function(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(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,
function(next) {
self._readTemplate(emailType.filename, next);
},
function(next) {
self._getDataForTemplate(notification, next);
},
], function(err, res) {
next(err, res[0], res[1]);
});
self.storage.storeEmail(email, function(err) {
return next(err, email);
},
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, next);
}, function(err) {
return next();
});
}, next);
},
function(emails, next) {
async.each(emails, function(email, next) {
self._send(email, next);
}, function(err) {
return next();
});
},
], cb);
},
], cb);
});
};
module.exports = EmailService;

View File

@ -340,6 +340,8 @@ WalletService.prototype._notify = function(type, data, opts, cb) {
log.debug('Notification', type, data);
cb = cb || function() {};
var walletId = self.walletId || data.walletId;
var copayerId = self.copayerId || data.copayerId;

View File

@ -210,20 +210,32 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
});
};
var db, storage, blockchainExplorer, mailer;
var storage, blockchainExplorer, mailer;
function openDb(cb) {
db = new tingodb.Db('./db/test', {});
// HACK: There appears to be a bug in TingoDB's close function where the callback is not being executed
db.__close = db.close;
db.close = function(force, cb) {
this.__close(force, cb);
var useMongo = false;
function initStorage(cb) {
function getDb(cb) {
if (useMongo) {
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost:27017/bws_test', function(err, db) {
if (err) throw err;
return cb(db);
});
} else {
var db = new tingodb.Db('./db/test', {});
return cb(db);
}
}
getDb(function(db) {
storage = new Storage({
db: db
});
return cb();
};
return cb();
});
};
function resetDb(cb) {
function resetStorage(cb) {
if (!storage.db) return cb();
storage.db.dropDatabase(function(err) {
return cb();
@ -233,21 +245,10 @@ function resetDb(cb) {
describe('Wallet service', function() {
before(function(done) {
openDb(function() {
storage = new Storage({
db: db
});
done();
});
// storage = new Storage();
// storage.connect({
// mongoDb: {
// uri: 'mongodb://localhost:27017/bws_test'
// }
// }, done);
initStorage(done);
});
beforeEach(function(done) {
resetDb(function() {
resetStorage(function() {
blockchainExplorer = sinon.stub();
mailer = sinon.stub();
WalletService.initialize({
@ -1566,7 +1567,7 @@ describe('Wallet service', function() {
});
});
it('should brodcast a tx', function(done) {
it('should broadcast a tx', function(done) {
var clock = sinon.useFakeTimers(1234000);
helpers.stubBroadcast('999');
server.broadcastTx({
@ -3083,12 +3084,15 @@ describe('Wallet service', function() {
});
});
describe('Email notifications', function() {
var server, wallet;
var server, wallet, sendMailStub;
beforeEach(function(done) {
mailer.sendMail = sinon.stub().yields();
helpers.createAndJoinWallet(2, 3, function(s, w) {
server = s;
wallet = w;
sendMailStub = sinon.stub();
sendMailStub.yields();
server.emailService.mailer.sendMail = sendMailStub;
var i = 0;
async.eachSeries(w.copayers, function(copayer, next) {
helpers.getAuthServer(copayer.id, function(server) {
@ -3104,20 +3108,17 @@ describe('Wallet service', function() {
});
it('should notify copayers a new tx proposal has been created', function(done) {
WalletService.onNotification(function(n) {
if (n.type != 'NewTxProposal') return;
var calls = mailer.sendMail.getCalls();
calls.length.should.equal(2);
var recipients = _.pluck(_.map(calls, function(c) {
return c.args[0];
}), 'to');
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], recipients).should.be.empty;
done();
});
helpers.stubUtxos(server, wallet, [1, 1], function() {
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, 'some message', TestData.copayers[0].privKey_1H_0);
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
var calls = sendMailStub.getCalls();
calls.length.should.equal(2);
var recipients = _.pluck(_.map(calls, function(c) {
return c.args[0];
}), 'to');
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], recipients).should.be.empty;
done();
});
});
});