-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (131 loc) · 4.17 KB
/
app.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const cron = require("node-cron");
const fs = require("fs");
const dotenv = require("dotenv");
const axios = require("axios");
//const nodemailer = require("nodemailer");
const nodemailer = require("nodemailer");
const moment = require("moment-timezone");
const { SendAlert } = require("./SendMail");
const ConnectDB = require("./config/DB");
ConnectDB();
const CowinDB = require("./Modules/CowinDBCenters");
dotenv.config({ path: "./config/config.env" });
const CallCowin = () => {
var Today = moment().tz("Asia/Kolkata").format("DD-MM-YYYY");
//APICall(Today);
for (i = 0; i <= 3; i++) {
var DinamicDate = moment()
.add(+i, "days")
.tz("Asia/Kolkata")
.format("DD-MM-YYYY");
APICall(DinamicDate);
}
console.log("Calling Every 10 Sec");
};
setInterval(CallCowin, 12000);
const APICall = async (date) => {
//console.log(date);
const response = await axios({
method: "GET",
url: `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=395&date=${date}`,
headers: {
"Accept-Language": "IN",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
},
});
const ResponseData = response.data.sessions;
var result = ResponseData.filter(function (data) {
return data.min_age_limit == 18 && data.available_capacity_dose1 > 0;
});
var CenterArray = [];
for (i = 0; i < result.length; i++) {
if (result) {
const Center = {
Name: result[i].name,
Address: result[i].address,
Pincode: result[i].pincode,
Fees: result[i].fee,
Dose1: result[i].available_capacity_dose1,
Date: date,
};
CenterArray.push(Center);
//console.log(Center)
} else {
//console.log(" Dose for 18+ Not Avialiable Now");
}
}
//console.log(CenterArray);
console.log(`Total Avilabale Centers Date ${date} => ` + result.length);
if (result.length > 0) {
const PushDetails = {
Date: date,
Centers: result.length,
};
const CowinDateCenter = await CowinDB.findOne({
Date: date,
});
if (CowinDateCenter && CowinDateCenter.Centers < result.length) {
//Update DB //Send SMS and Emails
const UpdateCowinDB = await CowinDB.findOneAndUpdate(
{ Date: date },
{
$set: {
Date: date,
Centers: result.length,
},
},
{ new: true }
).select("-__v");
SendAlert(CenterArray, date);
var CenterArray = [];
const SMSresponse = await axios({
method: "POST",
url: "https://www.fast2sms.com/dev/bulkV2",
headers: {
"Content-Type": "application/json",
authorization:
"CS5gsTjlIjVLJ2Pmm5R8QniIoaiKwB5QbHSLnehqDtORVGxpb4IjsKJTe6Em",
},
data: {
route : "v3",
sender_id : "TXTIND",
message: `Slots are avilabale Date ${date},Please check your mail for more information`,
language: "english",
flash: 0,
numbers: "9987242234",
},
});
console.log(SMSresponse.data.message);
console.log("Updated and Send");
} else if (CowinDateCenter && CowinDateCenter.Centers == result.length) {
// DO Nothing
console.log("Doing Nothing");
}
if (!CowinDateCenter) {
//Add New entry in DB // Send Emial SMS
const NewCoinDateEntry = await CowinDB.create(PushDetails);
SendAlert(CenterArray, date);
var CenterArray = [];
const SMSresponse = await axios({
method: "POST",
url: "https://www.fast2sms.com/dev/bulkV2",
headers: {
"Content-Type": "application/json",
authorization:
"CS5gsTjlIjVLJ2Pmm5R8QniIoaiKwB5QbHSLnehqDtORVGxpb4IjsKJTe6Em",
},
data: {
route : "v3",
sender_id : "TXTIND",
message: `Slots are avilabale Date ${date},Please check your mail for more information`,
language: "english",
flash: 0,
numbers: "9987242234",
},
});
console.log("Created and Send");
console.log(SMSresponse.data.message);
}
}
};