-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifier.js
123 lines (110 loc) · 3.18 KB
/
notifier.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
const notifier = require("node-notifier");
const request = require("./util/request");
const API_V2 = "https://cdn-api.co-vin.in/api/v2";
/* ----------------------- HELPERS ------------------------ */
function checkSlots(type, values, options) {
return getSlotsBy(type, values, options)
.then((data) => {
const slots = isSlotAvailable(data, options);
if (slots && Object.entries(slots).length > 0) {
notify("found", options.value, options.dose, slots);
} else {
notify("not_found", options.value);
}
})
.catch((e) => {
console.log("e", e);
notify("error");
});
}
function isSlotAvailable(data, options) {
try {
const centers = data.centers;
const availableSlots = {};
if (centers && centers.length > 0) {
console.log("\n\nData successfully fetched for: ", options.value);
centers.forEach((center) => {
const sessions = center.sessions.filter(
(session) =>
session.min_age_limit === options.age &&
(options.dose === 1
? session.available_capacity_dose1 > 0
: session.available_capacity_dose2 > 0)
);
if (sessions.length > 0) availableSlots[center.pincode] = center;
});
return availableSlots;
}
return null;
} catch (e) {
console.log("Error :", e);
notify("error");
}
}
const notify = (type, value, dose, data = []) => {
const cDate = new Date().toLocaleString();
switch (type) {
case "error":
notifier.notify({
title: "Something is wrong!",
message:
"Some unexpected happened during running a scheduler job. see terminal for more detail!",
sound: SOUND_ALERT,
});
break;
case "found":
console.log(
`Last checked on: ${cDate}
___SLOTS_FOUND___
${JSON.stringify(data, null, 4)}`
);
notifier.notify({
title: `Slots Available for Dose: ${dose} in below Pincodes`,
message: JSON.stringify(Object.keys(data), null, 4),
sound: SOUND_ALERT,
});
break;
case "not_found":
console.log(
`Last checked on: ${cDate}
___NO_SLOTS_FOUND___ for ${value}`
);
break;
default:
notifier.notify({
title: "Something is wrong!",
message:
"Some unexpected happened during running a scheduler job. see terminal for more detail!",
sound: SOUND_ALERT,
});
}
};
function getSlotsByPincode(pincode, options) {
const payload = {
url: `${API_V2}/appointment/sessions/public/calendarByPin?pincode=${pincode}&date=${options.date}`,
};
return request.get(payload);
}
function getSlotsByDist(distId, options) {
const payload = {
url: `${API_V2}/appointment/sessions/public/calendarByDistrict?district_id=${distId}&date=${options.date}`,
};
return request.get(payload);
}
function getSlotsBy(type, value, options) {
switch (type) {
case "dist": {
return getSlotsByDist(value, options);
}
case "pincode": {
return getSlotsByPincode(value, options);
}
// Control should not come here
default: {
return getSlotsByPincode(value, options);
}
}
}
module.exports = {
checkSlots,
};