send email from a separate service
This commit is contained in:
parent
d12c0401dc
commit
5c6b5aeae2
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var _ = require('lodash');
|
||||||
|
var log = require('npmlog');
|
||||||
|
log.debug = log.verbose;
|
||||||
|
|
||||||
|
var config = require('../config');
|
||||||
|
var EmailService = require('../lib/emailservice');
|
||||||
|
|
||||||
|
var emailService = new EmailService();
|
||||||
|
emailService.start(config, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
console.log('Email service started');
|
||||||
|
});
|
|
@ -10,7 +10,6 @@ var BlockchainExplorer = require('./blockchainexplorer');
|
||||||
var Storage = require('./storage');
|
var Storage = require('./storage');
|
||||||
var MessageBroker = require('./messagebroker');
|
var MessageBroker = require('./messagebroker');
|
||||||
var Lock = require('./lock');
|
var Lock = require('./lock');
|
||||||
var EmailService = require('./emailservice');
|
|
||||||
|
|
||||||
var Notification = require('./model/notification');
|
var Notification = require('./model/notification');
|
||||||
|
|
||||||
|
@ -20,8 +19,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
$.checkArgument(opts.blockchainExplorerOpts);
|
$.checkArgument(opts.blockchainExplorerOpts);
|
||||||
$.checkArgument(opts.storageOpts);
|
$.checkArgument(opts.storageOpts);
|
||||||
|
$.checkArgument(opts.messageBrokerOpts);
|
||||||
$.checkArgument(opts.lockOpts);
|
$.checkArgument(opts.lockOpts);
|
||||||
$.checkArgument(opts.emailOpts);
|
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -49,16 +48,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error(err);
|
log.error(err);
|
||||||
return cb(err);
|
|
||||||
}
|
}
|
||||||
|
return cb(err);
|
||||||
self.emailService = new EmailService({
|
|
||||||
lock: self.lock,
|
|
||||||
storage: self.storage,
|
|
||||||
mailer: opts.mailer,
|
|
||||||
emailOpts: opts.emailOpts,
|
|
||||||
});
|
|
||||||
return cb();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -136,11 +127,7 @@ BlockchainMonitor.prototype._createNotification = function(walletId, txid, addre
|
||||||
});
|
});
|
||||||
self.storage.storeNotification(walletId, notification, function() {
|
self.storage.storeNotification(walletId, notification, function() {
|
||||||
self.messageBroker.send(notification)
|
self.messageBroker.send(notification)
|
||||||
if (self.emailService) {
|
return cb();
|
||||||
self.emailService.sendEmail(notification, cb);
|
|
||||||
} else {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,10 @@ log.debug = log.verbose;
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var nodemailer = require('nodemailer');
|
var nodemailer = require('nodemailer');
|
||||||
|
|
||||||
|
var Storage = require('./storage');
|
||||||
|
var MessageBroker = require('./messagebroker');
|
||||||
|
var Lock = require('./lock');
|
||||||
|
|
||||||
var Model = require('./model');
|
var Model = require('./model');
|
||||||
|
|
||||||
var EMAIL_TYPES = {
|
var EMAIL_TYPES = {
|
||||||
|
@ -38,21 +42,47 @@ var EMAIL_TYPES = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function EmailService(opts) {
|
function EmailService() {};
|
||||||
$.checkArgument(opts);
|
|
||||||
|
|
||||||
opts.emailOpts = opts.emailOpts || {};
|
EmailService.prototype.start = function(opts, cb) {
|
||||||
|
opts = opts || {};
|
||||||
|
$.checkArgument(opts.storageOpts);
|
||||||
|
$.checkArgument(opts.messageBrokerOpts);
|
||||||
|
$.checkArgument(opts.lockOpts);
|
||||||
|
$.checkArgument(opts.emailOpts);
|
||||||
|
|
||||||
this.storage = opts.storage;
|
var self = this;
|
||||||
this.lock = opts.lock;
|
|
||||||
this.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts);
|
|
||||||
this.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]';
|
|
||||||
this.from = opts.emailOpts.from;
|
|
||||||
|
|
||||||
$.checkState(this.mailer);
|
async.parallel([
|
||||||
$.checkState(this.from);
|
|
||||||
|
function(done) {
|
||||||
|
self.storage = new Storage();
|
||||||
|
self.storage.connect(opts.storageOpts, done);
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
self.messageBroker = new MessageBroker(opts.messageBrokerOpts);
|
||||||
|
self.messageBroker.onMessage(_.bind(self.sendEmail, self));
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
self.lock = new Lock(opts.lockOpts);
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
self.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts);
|
||||||
|
self.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]';
|
||||||
|
self.from = opts.emailOpts.from;
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
], function(err) {
|
||||||
|
if (err) {
|
||||||
|
log.error(err);
|
||||||
|
}
|
||||||
|
return cb(err);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// TODO: cache for X minutes
|
// TODO: cache for X minutes
|
||||||
EmailService.prototype._readTemplate = function(filename, cb) {
|
EmailService.prototype._readTemplate = function(filename, cb) {
|
||||||
fs.readFile(__dirname + '/templates/' + filename + '.plain', 'utf8', function(err, template) {
|
fs.readFile(__dirname + '/templates/' + filename + '.plain', 'utf8', function(err, template) {
|
||||||
|
@ -144,6 +174,8 @@ EmailService.prototype._send = function(email, cb) {
|
||||||
EmailService.prototype.sendEmail = function(notification, cb) {
|
EmailService.prototype.sendEmail = function(notification, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
cb = cb || function() {};
|
||||||
|
|
||||||
var emailType = EMAIL_TYPES[notification.type];
|
var emailType = EMAIL_TYPES[notification.type];
|
||||||
if (!emailType) return cb();
|
if (!emailType) return cb();
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ var Lock = require('./lock');
|
||||||
var Storage = require('./storage');
|
var Storage = require('./storage');
|
||||||
var MessageBroker = require('./messagebroker');
|
var MessageBroker = require('./messagebroker');
|
||||||
var BlockchainExplorer = require('./blockchainexplorer');
|
var BlockchainExplorer = require('./blockchainexplorer');
|
||||||
var EmailService = require('./emailservice');
|
|
||||||
|
|
||||||
var Model = require('./model');
|
var Model = require('./model');
|
||||||
var Wallet = Model.Wallet;
|
var Wallet = Model.Wallet;
|
||||||
|
@ -31,7 +30,6 @@ var storage;
|
||||||
var blockchainExplorer;
|
var blockchainExplorer;
|
||||||
var blockchainExplorerOpts;
|
var blockchainExplorerOpts;
|
||||||
var messageBroker;
|
var messageBroker;
|
||||||
var emailService;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,7 +46,6 @@ function WalletService() {
|
||||||
this.blockchainExplorerOpts = blockchainExplorerOpts;
|
this.blockchainExplorerOpts = blockchainExplorerOpts;
|
||||||
this.messageBroker = messageBroker;
|
this.messageBroker = messageBroker;
|
||||||
this.notifyTicker = 0;
|
this.notifyTicker = 0;
|
||||||
this.emailService = emailService;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,17 +77,6 @@ WalletService.initialize = function(opts, cb) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function initEmailService(cb) {
|
|
||||||
if (!opts.mailer && !opts.emailOpts) return cb();
|
|
||||||
emailService = new EmailService({
|
|
||||||
lock: lock,
|
|
||||||
storage: storage,
|
|
||||||
mailer: opts.mailer,
|
|
||||||
emailOpts: opts.emailOpts,
|
|
||||||
});
|
|
||||||
return cb();
|
|
||||||
};
|
|
||||||
|
|
||||||
function initMessageBroker(cb) {
|
function initMessageBroker(cb) {
|
||||||
if (opts.messageBroker) {
|
if (opts.messageBroker) {
|
||||||
messageBroker = opts.messageBroker;
|
messageBroker = opts.messageBroker;
|
||||||
|
@ -108,9 +94,6 @@ WalletService.initialize = function(opts, cb) {
|
||||||
function(next) {
|
function(next) {
|
||||||
initMessageBroker(next);
|
initMessageBroker(next);
|
||||||
},
|
},
|
||||||
function(next) {
|
|
||||||
initEmailService(next);
|
|
||||||
},
|
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('Could not initialize', err);
|
log.error('Could not initialize', err);
|
||||||
|
@ -356,11 +339,7 @@ WalletService.prototype._notify = function(type, data, opts, cb) {
|
||||||
|
|
||||||
this.storage.storeNotification(walletId, notification, function() {
|
this.storage.storeNotification(walletId, notification, function() {
|
||||||
self.messageBroker.send(notification);
|
self.messageBroker.send(notification);
|
||||||
if (self.emailService) {
|
return cb();
|
||||||
self.emailService.sendEmail(notification, cb);
|
|
||||||
} else {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3229,51 +3229,4 @@ describe('Wallet service', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Email notifications', function() {
|
|
||||||
var server, wallet, sendMailStub;
|
|
||||||
beforeEach(function(done) {
|
|
||||||
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) {
|
|
||||||
server.savePreferences({
|
|
||||||
email: 'copayer' + (i++) + '@domain.com',
|
|
||||||
}, next);
|
|
||||||
});
|
|
||||||
}, done);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should notify copayers a new tx proposal has been created', function(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 emails = _.map(calls, function(c) {
|
|
||||||
return c.args[0];
|
|
||||||
});
|
|
||||||
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
|
||||||
var one = emails[0];
|
|
||||||
one.from.should.equal('bws@dummy.net');
|
|
||||||
one.subject.should.contain('New payment proposal');
|
|
||||||
one.text.should.contain(wallet.name);
|
|
||||||
one.text.should.contain(wallet.copayers[0].name);
|
|
||||||
server.storage.fetchUnsentEmails(function(err, unsent) {
|
|
||||||
should.not.exist(err);
|
|
||||||
unsent.should.be.empty;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue