From c864675b201a8b91922a3a24a4929bf92b3ab602 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 11 May 2015 15:03:17 -0300 Subject: [PATCH] send email from blockchain monitor --- lib/blockchainmonitor.js | 30 ++++++++++++++++++++++++------ lib/server.js | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index 787c7fd..f89edb6 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -18,6 +18,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) { opts = opts || {}; $.checkArgument(opts.blockchainExplorerOpts); $.checkArgument(opts.storageOpts); + $.checkArgument(opts.lockOpts); + $.checkArgument(opts.emailOpts); var self = this; @@ -38,8 +40,20 @@ BlockchainMonitor.prototype.start = function(opts, cb) { self.messageBroker = new MessageBroker(opts.messageBrokerOpts); done(); }, - ], cb); - + function(done) { + self.lock = new Lock(opts.lockOpts); + }, + ], function(err) { + if (err) return cb(err); + + self.emailService = new EmailService({ + lock: self.lock, + storage: self.storage, + mailer: opts.mailer, + emailOpts: opts.emailOpts, + }); + return cb(); + }); }; BlockchainMonitor.prototype._initExplorer = function(provider, network, url) { @@ -106,7 +120,7 @@ BlockchainMonitor.prototype._handleIncommingTx = function(data) { BlockchainMonitor.prototype._createNotification = function(walletId, txid, address, amount, cb) { var self = this; - var n = Notification.create({ + var notification = Notification.create({ type: 'NewIncomingTx', data: { txid: txid, @@ -115,9 +129,13 @@ BlockchainMonitor.prototype._createNotification = function(walletId, txid, addre }, walletId: walletId, }); - self.storage.storeNotification(walletId, n, function() { - self.messageBroker.send(n) - return cb(); + self.storage.storeNotification(walletId, notification, function() { + self.messageBroker.send(notification) + if (self.emailService) { + self.emailService.sendEmail(notification, function() { + if (cb) return cb(); + }); + } }); }; diff --git a/lib/server.js b/lib/server.js index 6eae584..1daad58 100644 --- a/lib/server.js +++ b/lib/server.js @@ -82,7 +82,7 @@ WalletService.initialize = function(opts, cb) { }; function initEmailService(cb) { - if (!opts.mailer && !opts.email) return cb(); + if (!opts.mailer && !opts.emailOpts) return cb(); emailService = new EmailService({ lock: lock, storage: storage,