notification-processor
Advanced tools
Comparing version
(function() { | ||
var EventEmitter, MAX_DEQUEUE_COUNT, NotificationsApi, Promise, _, _cleanOptions, _getHeaders, _notifyFail, _notifySuccess, emitter, notificationsApi, request; | ||
var MAX_DEQUEUE_COUNT, NotificationsApi, _, _cleanOptions, _getHeaders, notificationsApi, request; | ||
@@ -10,8 +10,4 @@ MAX_DEQUEUE_COUNT = process.env.MAX_DEQUEUE_COUNT; | ||
Promise = require("bluebird"); | ||
_ = require("lodash"); | ||
EventEmitter = require("events").EventEmitter; | ||
notificationsApi = null; | ||
@@ -38,16 +34,2 @@ | ||
emitter = new EventEmitter; | ||
_notifySuccess = function(arg) { | ||
var JobId, ref, statusCode; | ||
(ref = arg.message, JobId = ref.JobId), statusCode = arg.statusCode; | ||
return notificationsApi.success(JobId, statusCode); | ||
}; | ||
_notifyFail = function(arg) { | ||
var JobId, error, ref, statusCode; | ||
(ref = arg.message, JobId = ref.JobId), statusCode = arg.statusCode, error = arg.error; | ||
return notificationsApi.fail(JobId, statusCode, error); | ||
}; | ||
module.exports = function(generateOptions) { | ||
@@ -65,3 +47,3 @@ return function(arg) { | ||
statusCode = arg1.statusCode; | ||
return _notifySuccess({ | ||
return notificationsApi.success({ | ||
message: message, | ||
@@ -79,6 +61,9 @@ statusCode: statusCode | ||
} | ||
return _notifyFail({ | ||
return notificationsApi.fail({ | ||
message: message, | ||
statusCode: statusCode, | ||
error: error | ||
error: error, | ||
request: { | ||
options: options | ||
} | ||
}); | ||
@@ -85,0 +70,0 @@ }); |
(function() { | ||
var API_URL, JOB_ID, JobsProcessor, NOTIFICATIONS_URL, OptionsGenerator, _createJobNotification, _nockAPI, _processJob, message, nock, should; | ||
var API_URL, JOB_ID, JobsProcessor, NOTIFICATIONS_URL, OptionsGenerator, _, _createJobNotification, _nockAPI, _processJob, message, nock, should; | ||
@@ -16,2 +16,4 @@ NOTIFICATIONS_URL = "http://notifications-api-development.azurewebsites.net/api"; | ||
_ = require("lodash"); | ||
nock = require("nock"); | ||
@@ -43,3 +45,3 @@ | ||
}); | ||
return it("and dequeue counter is greater than MAX_DEQUEUE_COUNT, should notify for fail to notificationsApi", function(done) { | ||
return it("and dequeue counter is greater than MAX_DEQUEUE_COUNT, should notify for fail to notificationsApi", function() { | ||
var badStatusCode, bodyExpected, errorMessage, notificationsApiNock; | ||
@@ -55,23 +57,19 @@ errorMessage = "it's a trap!"; | ||
notificationsApiNock = nock(NOTIFICATIONS_URL).post("/jobs/" + JOB_ID + "/operations", function(body) { | ||
return body.should.be.eql(bodyExpected); | ||
return _.omit(body, "request").should.be.eql(bodyExpected); | ||
}).reply(200); | ||
_processJob({ | ||
return _processJob({ | ||
dequeueCount: 6 | ||
}).tap(function() { | ||
return notificationsApiNock.done(); | ||
}); | ||
return setTimeout((function() { | ||
notificationsApiNock.done(); | ||
return done(); | ||
}), 100); | ||
}); | ||
}); | ||
return context("when API response with good status code", function() { | ||
return it("and dequeue counter is lower than MAX_DEQUEUE_COUNT, should notify for success to notificationsApi", function(done) { | ||
return it("and dequeue counter is lower than MAX_DEQUEUE_COUNT, should notify for success to notificationsApi", function() { | ||
var notificationsApiNock; | ||
_nockAPI(); | ||
notificationsApiNock = nock(NOTIFICATIONS_URL).post("/jobs/" + JOB_ID + "/operations").reply(200); | ||
_processJob(); | ||
return setTimeout((function() { | ||
notificationsApiNock.done(); | ||
return done(); | ||
}), 100); | ||
return _processJob().tap(function() { | ||
return notificationsApiNock.done(); | ||
}); | ||
}); | ||
@@ -78,0 +76,0 @@ }); |
(function() { | ||
var NOTIFICATIONS_URL, NotificationsApi, request, retry, | ||
var NOTIFICATIONS_URL, NotificationsApi, _, requestPromise, retry, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
@@ -7,4 +7,6 @@ | ||
request = require("request-promise"); | ||
_ = require("lodash"); | ||
requestPromise = require("request-promise"); | ||
retry = require("bluebird-retry"); | ||
@@ -20,6 +22,8 @@ | ||
NotificationsApi.prototype.success = function(jobId, statusCode) { | ||
NotificationsApi.prototype.success = function(arg) { | ||
var JobId, ref, statusCode; | ||
(ref = arg.message, JobId = ref.JobId), statusCode = arg.statusCode; | ||
return retry(((function(_this) { | ||
return function() { | ||
return request(_this._makeRequest(jobId, { | ||
return requestPromise(_this._makeRequest(JobId, { | ||
statusCode: statusCode, | ||
@@ -31,14 +35,15 @@ success: true | ||
max_tries: 3 | ||
}); | ||
}).catchReturn(); | ||
}; | ||
NotificationsApi.prototype.fail = function(jobId, statusCode, arg) { | ||
var message; | ||
message = arg.message; | ||
NotificationsApi.prototype.fail = function(arg) { | ||
var error, message, request, statusCode; | ||
message = arg.message, statusCode = arg.statusCode, error = arg.error, request = arg.request; | ||
return retry(((function(_this) { | ||
return function() { | ||
return request(_this._makeRequest(jobId, { | ||
return requestPromise(_this._makeRequest(message.JobId, { | ||
statusCode: statusCode, | ||
success: false, | ||
message: message | ||
message: error.message, | ||
request: request | ||
})); | ||
@@ -48,3 +53,3 @@ }; | ||
max_tries: 3 | ||
}); | ||
}).catchReturn(); | ||
}; | ||
@@ -51,0 +56,0 @@ |
@@ -16,2 +16,4 @@ (function() { | ||
require("should-sinon"); | ||
NotificationsApi = require("./notification.api"); | ||
@@ -33,13 +35,25 @@ | ||
}).reply(200); | ||
return notificationsApi.success(jobId, statusCode); | ||
return notificationsApi.success({ | ||
message: { | ||
JobId: jobId | ||
}, | ||
statusCode: statusCode | ||
}); | ||
}); | ||
return it("on fail: should send success: false with error message to notificationsApi", function() { | ||
var bodyExpected, jobId, message, statusCode; | ||
it("on fail: should send success: false with error message to notificationsApi", function() { | ||
var bodyExpected, jobId, message, request, statusCode; | ||
jobId = 1; | ||
statusCode = 400; | ||
message = "Opps!, Something went wrong"; | ||
request = { | ||
request: { | ||
method: "POST", | ||
url: "/items" | ||
} | ||
}; | ||
bodyExpected = { | ||
statusCode: statusCode, | ||
message: message, | ||
success: false | ||
success: false, | ||
request: request | ||
}; | ||
@@ -49,8 +63,27 @@ nock(NOTIFICATIONS_URL).post("/jobs/" + jobId + "/operations", function(body) { | ||
}).reply(200); | ||
return notificationsApi.fail(jobId, statusCode, { | ||
message: message | ||
return notificationsApi.fail({ | ||
message: { | ||
JobId: jobId | ||
}, | ||
statusCode: statusCode, | ||
error: { | ||
message: message | ||
}, | ||
request: request | ||
}); | ||
}); | ||
return it("ignore error if its has ocurred when call to notifications-api", function() { | ||
var jobId; | ||
this.timeout(4000); | ||
jobId = "123"; | ||
nock(NOTIFICATIONS_URL).post("/jobs/" + jobId + "/operations").times(3).reply(500); | ||
return notificationsApi.success({ | ||
message: { | ||
JobId: jobId | ||
}, | ||
statusCode: 200 | ||
}); | ||
}); | ||
}); | ||
}).call(this); |
{ | ||
"name": "notification-processor", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "notification-processor", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
83393
81.83%36
12.5%1167
2.01%