diff --git a/TODO.md b/TODO.md index 1c6a17be..604c6f7e 100644 --- a/TODO.md +++ b/TODO.md @@ -16,9 +16,10 @@ | controllers/Users.js | 297 | Test that any deleted data is backed up | routes/index.js | 214 | Implement API Generator | routes/initialize.js | 10 | Test initialize route -| services/encryption/index.js | 40 | Generate checksum here | services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs | services/queue/jobs.js | 80 | Test saveToTrash job | services/queue/jobs.js | 93 | Test Webhook Event | services/queue/jobs.js | 137 | Test Secure Webhooks -| services/queue/jobs.js | 144 | Test Unsecure Webhooks \ No newline at end of file +| services/queue/jobs.js | 144 | Test Unsecure Webhooks +| services/queue/jobs.js | 169 | Test sendHTTPRequest Job +| services/encryption/index.js | 40 | Generate checksum here \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 0c590a38..2b659fad 100644 --- a/routes/index.js +++ b/routes/index.js @@ -177,7 +177,7 @@ router.use(expressValidator()); if(config.noFrontendCaching === 'yes'){ router.use(helmet.noCache()); }else{ - router.use(router._APICache); + router.use(router._APICache); } router.get('/', function (req, res) { diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 00000000..3918c74e --- /dev/null +++ b/routes/users.js @@ -0,0 +1 @@ +"use strict"; diff --git a/services/queue/jobs.js b/services/queue/jobs.js index 38272cc5..4260c460 100644 --- a/services/queue/jobs.js +++ b/services/queue/jobs.js @@ -162,4 +162,50 @@ jobs.sendWebhook = function(data, done){ }); }; +// Send HTTP Request +// This is for jobs that can be configured from an admin dashboard. So an admin an configure the system to all an api at a particular time daily. +// This can be used within the code too, to do some jobs. +// Supports POST or GET +// ToDo: Test sendHTTPRequest Job +jobs.sendHTTPRequest = function(data, done){ + log.info('Sending HTTP' +data.method+' request to '+data.url+' with data => '+data.data+' and headers => '+data.headers); + // Expected data + // { + // url: 'http://string.com', + // method: 'POST', // or any http method + // headers: { + // 'User-Agent': 'Request-Promise' + // }, + // data: { + // someData: 'this', + // someOtherData: 'and this' + // } + // } + // + + var options = { + method: data.method, + uri: data.url, + body: data.data, + headers: data.headers, + json: true // Automatically parses the JSON string in the response + }; + + if(data.method === 'GET'){ + options.qs = data.data; + }else if(data.method === 'POST'){ + options.body = data.data; + }else{ + options.qs = data.data; + options.body = data.data; + } + request(options) + .then(function(resp){ + done(false, resp); + }) + .catch(function(err){ + done(new Error(err.message)); + }); +}; + module.exports = jobs; diff --git a/services/queue/workers.js b/services/queue/workers.js index ebd673ab..599e402b 100644 --- a/services/queue/workers.js +++ b/services/queue/workers.js @@ -23,4 +23,8 @@ queue.process('sendWebhook', 2, function(job,done){ jobs.sendWebhook(job.data, done); }); +queue.process('sendHTTPRequest', 2, function(job,done){ + jobs.sendHTTPRequest(job.data, done); +}); + module.exports = queue; diff --git a/test/services/queue.js b/test/services/queue.js index c7395899..a1e4ba12 100644 --- a/test/services/queue.js +++ b/test/services/queue.js @@ -47,7 +47,7 @@ describe('#Queue service', function(){ it('should load processes', function() { var process = require('../../services/queue/workers'); // We have configured queue to create 2 workers per job making a total of 6 workers for 3 jobs that we currently have - process.workers.length.should.equal(10); + process.workers.length.should.equal(12); });