forked from ayu-ayushisood/SlotScheduler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronjob.js
91 lines (70 loc) · 2.75 KB
/
cronjob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const https = require('https');
const moment = require('moment');
const _ = require('underscore');
var twilio = require('twilio');
require('dotenv').config();
const requirementData = require('./cronScheduleRequirements.js');
const calendarByDistrictUrl = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?';
const cronjobFunc = () => {
apiCalls();
};
const apiCalls = () => {
const requirementObject = requirementData.requirementData;
requirementObject.forEach(reqObj => {
let dataForUrl = `district_id=${reqObj.district_id}&date=${moment().format('DD-MM-YYYY')}`;
const finalUrl = `${calendarByDistrictUrl}${dataForUrl}`;
https.get(finalUrl, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
// console.log(data);
let availableData = parseData(JSON.parse(data), reqObj.name, reqObj.phone);
// console.log('available Data - ' + JSON.stringify(availableData));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
});
};
const sendSms = async (smsText, toPhoneNum) => {
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var authToken = process.env.TWILIO_AUTH_TOKEN;
var fromPhoneNum = process.env.FROM_PHONE_NUMBER;
var client = new twilio(accountSid, authToken);
client.messages.create({
body: smsText,
to: toPhoneNum, // Text this number
from: fromPhoneNum // From a valid Twilio number
})
.then((message) => console.log(message.sid))
.catch( error => console.log("Error in sending message", error))
};
const parseData = (data, name, phoneNum) => {
let centers = data.centers;
let availableCenterCapabilities = centers.map(center => {
const centerName = center.name;
const address = center.address;
const sessions = center.sessions;
let availableCapacities = sessions.map(session => {
const date = session.date;
const availableCapacity = session.available_capacity;
const minAgeLimit = session.min_age_limit;
if (availableCapacity > 0 && minAgeLimit !== 45) {
const sms_message = `${availableCapacity} slots available at ${centerName} - ${address}. Min Age - ${minAgeLimit}. Dt: ${date}`;
sendSms(sms_message, phoneNum);
return {
name: centerName,
address: address,
available: availableCapacity,
date: date,
};
}
});
return _.compact(availableCapacities);
});
console.log('parsed Data for ' + name + ' - ' + '\n' + JSON.stringify(_.compact(availableCenterCapabilities)));
};
module.exports = ({ cronjobFunc });