Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. //API to get slot for a particular date range
  2. app.get('/listcapacity/:ticketid/:fromdate/:todate', function(req, res) {
  3. var id = req.params.ticketid;
  4. var fromdate = req.params.fromdate;
  5. var todate = req.params.todate;
  6. var key = null;
  7. var username = 'foo';
  8. var password = 'foobar';
  9. var result = {};
  10. var data_output = [];
  11. var currentDate = new Date(fromdate);
  12. var between = [];
  13. var end = new Date(todate);
  14.  
  15. while (currentDate <= end) {
  16. var tempdate = new Date(currentDate).toISOString();
  17. var dump = tempdate.toString().split("T");
  18. between.push(dump[0]);
  19. currentDate.setDate(currentDate.getDate() + 1);
  20. }
  21.  
  22. between.forEach(function(entry) {
  23. key = id+entry+"list";
  24. console.log("Keys: " + key);
  25. client.exists(key, function(err, reply) {
  26. if (reply === 1) {
  27. console.log("Key : " + key + " Found");
  28. client.get(key, function(err, reply) {
  29. var output = JSON.parse(reply);
  30. data_output = data_output.concat(output);
  31. });
  32. } else {
  33. console.log("Key : " + key + " Not Found");
  34.  
  35. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  36. var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&fromDate=" + entry + "&toDate=" + entry;
  37. request({
  38.  
  39. url: slot_url,
  40. json: true,
  41. headers: headers
  42. }, function(error, response, body) {
  43. if (!error && response.statusCode === 200) {
  44. var data = [];
  45. try {
  46. var temp = {};
  47. body.data.forEach(function(tempslots) {
  48. temp['date'] = tempslots['date'];
  49. temp['timeslots'] = tempslots['timeslots'];
  50. data = data.concat(temp);
  51. });
  52. client.set(key, JSON.stringify(data));
  53. data_output = data_output.concat(data);
  54. } catch (err) {
  55.  
  56. console.log(err.message);
  57.  
  58. }
  59. } else {
  60. console.log("Something went wrong!! " + error.message);
  61. }
  62. })
  63. }
  64. });
  65. });
  66. result['data'] = data_output;
  67. result['response'] = 1;
  68. result['message'] = 'Capacity list fetched successfully!';
  69. res.json(result);
  70.  
  71. });
  72.  
  73. Keys: 5212016-10-01list
  74. Keys: 5212016-10-02list
  75. Keys: 5212016-10-03list
  76. Keys: 5212016-10-04list
  77. Keys: 5212016-10-05list
  78. Key : 5212016-10-05list Not Found
  79. Key : 5212016-10-05list Not Found
  80. Key : 5212016-10-05list Not Found
  81. Key : 5212016-10-05list Not Found
  82. Key : 5212016-10-05list Found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement