diff --git a/config.js b/config.js index c197cda..f5f944f 100644 --- a/config.js +++ b/config.js @@ -54,7 +54,7 @@ var config = { livenet: 'https://insight.bitpay.com/tx/{{txid}}', testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', }, - pushServerUrl: 'http://192.168.1.143:8000/send', + pushServerUrl: 'http://192.168.1.111:8000/send', }, // To use email notifications uncomment this: // emailOpts: { diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index 8b1c695..d680612 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -3,7 +3,7 @@ var _ = require('lodash'); var async = require('async'); var Mustache = require('mustache'); -var request = require('request'); +var defaultRequest = require('request'); var MessageBroker = require('./messagebroker'); var Storage = require('./storage'); var fs = require('fs'); @@ -39,6 +39,7 @@ function PushNotificationsService() {}; PushNotificationsService.prototype.start = function(opts, cb) { var self = this; opts = opts || {}; + self.request = opts.request || defaultRequest; function _readDirectories(basePath, cb) { fs.readdir(basePath, function(err, files) { @@ -53,14 +54,14 @@ PushNotificationsService.prototype.start = function(opts, cb) { }); }; - self.templatePath = opts.pushNotificationsOpts.templatePath || templatePathpath.normalize(((__dirname + '/templates')) + '/'); + self.templatePath = path.normalize((opts.pushNotificationsOpts.templatePath || (__dirname + '/templates')) + '/'); self.defaultLanguage = opts.pushNotificationsOpts.defaultLanguage || 'en'; self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc'; self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || ''; self.publicTxUrlTemplate = opts.pushNotificationsOpts.publicTxUrlTemplate || {}; self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl; - async.parallel([ + function(done) { _readDirectories(self.templatePath, function(err, res) { self.availableLanguages = res; @@ -68,8 +69,13 @@ PushNotificationsService.prototype.start = function(opts, cb) { }); }, function(done) { - self.storage = new Storage(); - self.storage.connect(opts.storageOpts, done); + if (opts.storage) { + self.storage = opts.storage; + done(); + } else { + self.storage = new Storage(); + self.storage.connect(opts.storageOpts, done); + } }, function(done) { self.messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts); @@ -82,6 +88,7 @@ PushNotificationsService.prototype.start = function(opts, cb) { } return cb(err); }); + }; PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) { @@ -91,12 +98,13 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio var notifType = PUSHNOTIFICATIONS_TYPES[notification.type]; if (!notifType) return cb(); - console.log(notification); + // console.log(notification); self._getRecipientsList(notification, function(err, recipientsList) { if (err) return cb(err); async.waterfall([ + function(next) { self._readAndApplyTemplates(notification, notifType, recipientsList, next); }, @@ -124,7 +132,8 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio self._makeRequest(opts, next()); }, function(err) { - log.error(err); + if (err) + log.error(err); return cb(err); } ); @@ -145,6 +154,7 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c if (err) return cb(err); self.storage.fetchPreferences(notification.walletId, null, function(err, preferences) { + if (err) log.error(err); if (_.isEmpty(preferences)) preferences = []; @@ -202,6 +212,7 @@ PushNotificationsService.prototype._readAndApplyTemplates = function(notificatio async.map(recipientsList, function(recipient, next) { async.waterfall([ + function(next) { self._getDataForTemplate(notification, recipient, next); }, @@ -340,7 +351,7 @@ PushNotificationsService.prototype._compileTemplate = function(template, extensi PushNotificationsService.prototype._makeRequest = function(opts, cb) { var self = this; - request({ + self.request({ url: self.pushServerUrl, method: 'POST', json: true,