From 5c6b5aeae235a88f27d13189a1012807b12a070f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 29 May 2015 10:30:59 -0300 Subject: [PATCH] send email from a separate service --- emailservice/emailservice.js | 17 ++++++++++++ lib/blockchainmonitor.js | 19 +++---------- lib/emailservice.js | 52 +++++++++++++++++++++++++++++------- lib/server.js | 23 +--------------- test/integration/server.js | 47 -------------------------------- 5 files changed, 63 insertions(+), 95 deletions(-) create mode 100644 emailservice/emailservice.js diff --git a/emailservice/emailservice.js b/emailservice/emailservice.js new file mode 100644 index 0000000..fbb7e38 --- /dev/null +++ b/emailservice/emailservice.js @@ -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'); +}); diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index 8c4af51..812f386 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -10,7 +10,6 @@ var BlockchainExplorer = require('./blockchainexplorer'); var Storage = require('./storage'); var MessageBroker = require('./messagebroker'); var Lock = require('./lock'); -var EmailService = require('./emailservice'); var Notification = require('./model/notification'); @@ -20,8 +19,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) { opts = opts || {}; $.checkArgument(opts.blockchainExplorerOpts); $.checkArgument(opts.storageOpts); + $.checkArgument(opts.messageBrokerOpts); $.checkArgument(opts.lockOpts); - $.checkArgument(opts.emailOpts); var self = this; @@ -49,16 +48,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) { ], function(err) { if (err) { log.error(err); - return cb(err); } - - self.emailService = new EmailService({ - lock: self.lock, - storage: self.storage, - mailer: opts.mailer, - emailOpts: opts.emailOpts, - }); - return cb(); + return cb(err); }); }; @@ -136,11 +127,7 @@ BlockchainMonitor.prototype._createNotification = function(walletId, txid, addre }); self.storage.storeNotification(walletId, notification, function() { self.messageBroker.send(notification) - if (self.emailService) { - self.emailService.sendEmail(notification, cb); - } else { - return cb(); - } + return cb(); }); }; diff --git a/lib/emailservice.js b/lib/emailservice.js index e5f8d43..deda15e 100644 --- a/lib/emailservice.js +++ b/lib/emailservice.js @@ -8,6 +8,10 @@ log.debug = log.verbose; var fs = require('fs'); var nodemailer = require('nodemailer'); +var Storage = require('./storage'); +var MessageBroker = require('./messagebroker'); +var Lock = require('./lock'); + var Model = require('./model'); var EMAIL_TYPES = { @@ -38,21 +42,47 @@ var EMAIL_TYPES = { }; -function EmailService(opts) { - $.checkArgument(opts); +function EmailService() {}; - 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; - this.lock = opts.lock; - this.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts); - this.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]'; - this.from = opts.emailOpts.from; + var self = this; - $.checkState(this.mailer); - $.checkState(this.from); + async.parallel([ + + 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 EmailService.prototype._readTemplate = function(filename, cb) { 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) { var self = this; + cb = cb || function() {}; + var emailType = EMAIL_TYPES[notification.type]; if (!emailType) return cb(); diff --git a/lib/server.js b/lib/server.js index 1951b36..9f0c4f5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -19,7 +19,6 @@ var Lock = require('./lock'); var Storage = require('./storage'); var MessageBroker = require('./messagebroker'); var BlockchainExplorer = require('./blockchainexplorer'); -var EmailService = require('./emailservice'); var Model = require('./model'); var Wallet = Model.Wallet; @@ -31,7 +30,6 @@ var storage; var blockchainExplorer; var blockchainExplorerOpts; var messageBroker; -var emailService; /** @@ -48,7 +46,6 @@ function WalletService() { this.blockchainExplorerOpts = blockchainExplorerOpts; this.messageBroker = messageBroker; 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) { if (opts.messageBroker) { messageBroker = opts.messageBroker; @@ -108,9 +94,6 @@ WalletService.initialize = function(opts, cb) { function(next) { initMessageBroker(next); }, - function(next) { - initEmailService(next); - }, ], function(err) { if (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() { self.messageBroker.send(notification); - if (self.emailService) { - self.emailService.sendEmail(notification, cb); - } else { - return cb(); - } + return cb(); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index 0675559..e02923c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -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(); - }); - }); - }); - }); - }); });