Any send email if first incident of month

New db function for getting all events for this month that also have
isOk as false. We use this to resolve into notifyIfDown function if this
is first incident this period.
This commit is contained in:
2020-06-08 22:18:10 +02:00
committed by KevinMidboe
parent 18f996b35f
commit c5815e931f
2 changed files with 25 additions and 3 deletions

View File

@@ -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
}

View File

@@ -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();