-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (63 loc) · 1.89 KB
/
index.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
const cowin = require("cowin-api-wrapper");
const config = require("./config");
const {
prettyCenter,
prettySession,
prettyTitle,
get3Dates,
} = require("./util");
async function main() {
console.log("");
const next3Dates = get3Dates();
await Promise.all(next3Dates.map(findAppointmentsForDate));
}
async function findAppointmentsForDate(date) {
const responses = await Promise.all(
config.locations.map(async (loc) => {
let response;
if (loc.type === "pincode") {
response = await cowin.findAppointmentsByPin(loc.pincode, { date });
} else {
response = await cowin.findAppointmentsByDistrict(loc.districtId, {
date,
});
}
const { error } = checkResponseForErrors(response);
if (error) return { title: loc.title };
return { data: response, title: loc.title, filters: loc.filters };
})
);
responses.forEach(({ data, filters, title }) => {
console.log(prettyTitle(title, date));
if (data) listAvailableSlots(data.appointments, filters);
console.log("\n");
});
}
function listAvailableSlots(appointments, filters) {
appointments.forEach((center) => {
let sessions = [...center.sessions];
if (!sessions || sessions.length === 0) {
console.error(`No sessions available at ${center.name}`);
} else {
filters.forEach(
(filterMethod) => (sessions = sessions.filter(filterMethod))
);
sessions.forEach((session) => {
const _center = prettyCenter(center);
const _session = prettySession(session);
console.log(`${_center} - ${_session}`);
});
}
});
}
function checkResponseForErrors(response) {
if (response.error) {
return { error: response.error };
}
const { appointments } = response;
if (!appointments || appointments.length === 0) {
return { error: "No centres available" };
}
return { error: false };
}
main();