Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //API to get slot for a particular date range
- app.get('/listcapacity/:ticketid/:fromdate/:todate', function(req, res) {
- var id = req.params.ticketid;
- var fromdate = req.params.fromdate;
- var todate = req.params.todate;
- var key = null;
- var username = 'foo';
- var password = 'foobar';
- var result = {};
- var data_output = [];
- var currentDate = new Date(fromdate);
- var between = [];
- var end = new Date(todate);
- while (currentDate <= end) {
- var tempdate = new Date(currentDate).toISOString();
- var dump = tempdate.toString().split("T");
- between.push(dump[0]);
- currentDate.setDate(currentDate.getDate() + 1);
- }
- between.forEach(function(entry) {
- key = id+entry+"list";
- console.log("Keys: " + key);
- client.exists(key, function(err, reply) {
- if (reply === 1) {
- console.log("Key : " + key + " Found");
- client.get(key, function(err, reply) {
- var output = JSON.parse(reply);
- data_output = data_output.concat(output);
- });
- } else {
- console.log("Key : " + key + " Not Found");
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
- var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&fromDate=" + entry + "&toDate=" + entry;
- request({
- url: slot_url,
- json: true,
- headers: headers
- }, function(error, response, body) {
- if (!error && response.statusCode === 200) {
- var data = [];
- try {
- var temp = {};
- body.data.forEach(function(tempslots) {
- temp['date'] = tempslots['date'];
- temp['timeslots'] = tempslots['timeslots'];
- data = data.concat(temp);
- });
- client.set(key, JSON.stringify(data));
- data_output = data_output.concat(data);
- } catch (err) {
- console.log(err.message);
- }
- } else {
- console.log("Something went wrong!! " + error.message);
- }
- })
- }
- });
- });
- result['data'] = data_output;
- result['response'] = 1;
- result['message'] = 'Capacity list fetched successfully!';
- res.json(result);
- });
- Keys: 5212016-10-01list
- Keys: 5212016-10-02list
- Keys: 5212016-10-03list
- Keys: 5212016-10-04list
- Keys: 5212016-10-05list
- Key : 5212016-10-05list Not Found
- Key : 5212016-10-05list Not Found
- Key : 5212016-10-05list Not Found
- Key : 5212016-10-05list Not Found
- Key : 5212016-10-05list Found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement