diff --git a/src/db.js b/src/db.js index e55f2f4..aca0a17 100644 --- a/src/db.js +++ b/src/db.js @@ -42,10 +42,25 @@ const getAlternatingEventStatuses = () => Event.find().exec() const getEventStatus = () => Event.find().select('date isOk').exec() .then(events => events.reverse()) +const anyDowntimeThisCalendarMonth = async () => { + const today = new Date(); + const year = today.getFullYear() + const month = today.getMonth(); + + const query = { + "date": { "$gte": new Date(year, month, 1) }, + "isOk": false + } + + return Event.find(query) + .then(errorsThisMonth => errorsThisMonth.length > 0) +} + module.exports = { commitServiceEventToDatabase, getAllEvents, getEventById, getAlternatingEventStatuses, - getEventStatus + getEventStatus, + anyDowntimeThisCalendarMonth } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 33ea2cc..37f9d99 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,11 @@ const config = require('../config') const Mail = require( './mail.js'); const mail = new Mail(); -const { commitServiceEventToDatabase, getAllEvents } = require('./db.js'); +const { + commitServiceEventToDatabase, + getAllEvents, + anyDowntimeThisCalendarMonth +} = require('./db.js'); let browser = undefined; if (config.debug == false) @@ -118,6 +122,8 @@ function closeBrowserAndExit(status=0) { process.exit(status); } +const resolveIfNoDowntimeYetThisMonth = msg => anyDowntimeThisCalendarMonth() + .then(anyDowntime => anyDowntime == false ? Promise.resolve(msg) : Promise.reject()) function run() { webscraper(config.url) @@ -125,8 +131,9 @@ function run() { .then(page => savePageToPDF(page)) .then(page => getServiceMessages(page)) .then(serviceMessages => commitServiceEventToDatabase(serviceMessages, pdfFilename)) + .then(serviceMessages => resolveIfNoDowntimeYetThisMonth(serviceMessages)) .then(serviceMessages => notifyIfDown(serviceMessages)) - .then(closeBrowserAndExit) + .finally(closeBrowserAndExit) } run();