Skip to content

Commit

Permalink
add delay to daily digest
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Sinha committed May 12, 2021
1 parent 6eb455a commit d8c2bf6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# COVID-19 Vaccine Alerts - COWIN
s# COVID-19 Vaccine Alerts - COWIN

This repository is an alerting application that sends email notifications to beneficiaries in India using [COWIN platform](https://www.cowin.gov.in/home) for vaccine availability. The application interacts with the [COWIN API](https://apisetu.gov.in/public/marketplace/api/cowin) at regular intervals to find vaccination slots available at your pin code location(s) or entire district along with a minimum age limit. So, if you are currently waiting to find slots in your region and do not see any slots for your age range then, you can utilise this application to receive alerts on your email address when there are slots available for you. This way you will be able to book your appointments on time. Remember, vaccination is highly beneficial for you in this horrific time of crisis. Get your jab and protect yourself from serious illness.

Expand Down Expand Up @@ -63,10 +63,10 @@ const SERVICE_PROVIDER = 'Gmail';
const RECIPIENT = 'mail1@gmail.com,mail2@gmail.com,mail3@gmail.com';
```

Finally, you can also alter the date range with which the application will fetch vaccination slots by customising **DATE_RANGE** value in [`src/configs/schedulerConfig.js`](https://github.com/sinhadotabhinav/covid-19-vaccine-alerts-cowin/blob/master/src/configs/schedulerConfig.js) file. By default it is set to **7** but, you can change it to 10 or 15 for example, based on your need. The config file also allows changes in the periodic schedule with which the application runs. By default, **SCHEDULE** value depicts a cron schedule **every hour at minute 15 and second 0**. To alter this schedule, you need to be familiar with the [cron scheduler](https://linuxhint.com/cron_jobs_complete_beginners_tutorial/#:~:text=The%20scheduled%20commands%20and%20scripts,Task%20Scheduler%20in%20Windows%20OS). I use [Crontab Guru](https://crontab.guru) website to test my cron schedules.
Finally, you can also alter the date range with which the application will fetch vaccination slots by customising **DATE_RANGE** value in [`src/configs/schedulerConfig.js`](https://github.com/sinhadotabhinav/covid-19-vaccine-alerts-cowin/blob/master/src/configs/schedulerConfig.js) file. By default it is set to **7** but, you can change it to 10 or 15 for example, based on your need. The config file also allows changes in the periodic schedule with which the application runs. By default, **SCHEDULE** value depicts a cron schedule **every hour at minute 5 and second 0**. To alter this schedule, you need to be familiar with the [cron scheduler](https://linuxhint.com/cron_jobs_complete_beginners_tutorial/#:~:text=The%20scheduled%20commands%20and%20scripts,Task%20Scheduler%20in%20Windows%20OS). I use [Crontab Guru](https://crontab.guru) website to test my cron schedules.

```
const SCHEDULE = '0 15 * * * *';
const SCHEDULE = '0 5 * * * *';
const DATE_RANGE = 7;
```

Expand Down
24 changes: 16 additions & 8 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function main() {
await fetchVaccinationSlots();
});
} catch (error) {
console.log(logger.getLog('There was an error fetching vaccine slots: ' + JSON.stringify(error, null, 2)));
console.log(logger.getLog(`There was an error fetching vaccine slots: ${JSON.stringify(error, null, 2)}.`));
}
}

Expand All @@ -36,7 +36,7 @@ async function fetchVaccinationSlots() {
let pincodeArray = appConfig.PINCODE.split(',');
for( counter = 0; counter < pincodeArray.length; counter++) {
if (pincodeArray[counter].toString().length < 6) {
console.log(logger.getLog('Invalid pincode ' + pincodeArray[counter] + ' provided in config file: (src/config/appConfig.js).'));
console.log(logger.getLog(`Invalid pincode ${pincodeArray[counter]} provided in config file: (src/config/appConfig.js).`));
pincodeArray.splice(counter, 1);
}
}
Expand Down Expand Up @@ -78,8 +78,11 @@ async function getAppointmentsByPincode(pincodeArray, dates) {
return appointments.getFilteredSlots(date, result.data.sessions);
})
.catch(function (error) {
console.log(logger.getLog('Unable to get appointment slots at pincode: ' + pin + ' for the date: ' + date +
', ' + error.response.statusText));
if (error.response.statusText) {
console.log(logger.getLog(`Unable to get appointment slots at pincode: ${pin} for the date: ${date}, ${error.response.statusText}.`));
} else {
console.log(logger.getLog(`Unable to get appointment slots at pincode: ${pin} for the date: ${date}, ${error}.`));
}
});
slotsArray.push(slots);
};
Expand All @@ -103,8 +106,11 @@ async function getAppointmentsByDistrict(dates) {
return appointments.getFilteredSlots(date, result.data.sessions);
})
.catch(function (error) {
console.log(logger.getLog('Unable to get appointment slots at district: ' + districtId + ' for the date: ' + date +
', ' + error.response.statusText));
if (error.response.statusText) {
console.log(logger.getLog(`Unable to get appointment slots at district: ${districtId} for the date: ${date}, ${error.response.statusText}.`));
} else {
console.log(logger.getLog(`Unable to get appointment slots at district: ${districtId} for the date: ${date}, ${error}.`));
}
});
slotsArray.push(slots);
};
Expand Down Expand Up @@ -152,9 +158,11 @@ async function sendEmailAlert(slotsArray) {

async function resetDailyCounter() {
const interval = parser.parseExpression(schedulerConfig.SCHEDULE);
let lastRun = moment().date() == moment(new Date(interval.next().toString())).date() ? false : true;
let nextRun = interval.next().toString();
let lastRun = moment().date() == moment(new Date(nextRun)).date() &&
moment().month() == moment(new Date(nextRun)).month() ? false : true;
if (Boolean(lastRun)) {
await dailyDigest.prepareReport();
await setTimeout(() => { dailyDigest.prepareReport(); }, schedulerConfig.DELAY);
runCounter = 0;
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/configs/schedulerConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const SCHEDULE = '0 15 * * * *';
const SCHEDULE = '0 5 * * * *';
const DATE_RANGE = 7;
const DATE_FORMAT = 'DD-MM-YYYY';
const DELAY = 60000;

module.exports = { SCHEDULE, DATE_RANGE, DATE_FORMAT };
module.exports = { SCHEDULE, DATE_RANGE, DATE_FORMAT, DELAY };

0 comments on commit d8c2bf6

Please sign in to comment.