Added ability to pass specific date as the holiday

Signed-off-by: James Ketrenos <james_gitlab@ketrenos.com>
This commit is contained in:
James Ketrenos 2020-01-02 19:46:36 -08:00
parent 594a2bdc68
commit 8c7a9dbcf4

View File

@ -542,58 +542,87 @@ router.delete("/:id?", function(req, res/*, next*/) {
router.get("/holiday/:holiday", function(req, res/*, next*/) {
let startYear = 1990,
endYear = moment().year(),
dayIsHoliday = "",
holidayName;
holidayName,
date = undefined;
/* Find the holiday in the list of holidays */
let lookup = moment().holidays([req.params.holiday]);
if (!lookup) {
return res.status(404).send(req.params.holiday + " holiday not found.");
date = req.params.holiday.match(/^((\d{4})-)?(\d{2})-(\d{2})$/);
if (!date) {
return res.status(404).send(req.params.holiday + " holiday not found.");
}
date = {
year: date[1],
month: date[3],
day: date[4]
};
if (date.year) {
startYear = date.year;
endYear = date.year;
}
}
holidayName = Object.getOwnPropertyNames(lookup)[0];
holidayName = date ? req.params.holiday : Object.getOwnPropertyNames(lookup)[0];
console.log("Searching for holiday: " + holidayName);
/* Lookup the date for the holiday on every year from 'startYear' (1990) to today */
for (let year = startYear; year <= moment().year(); year++) {
for (let year = startYear; year <= endYear; year++) {
console.log("Getting year: " + year);
let holiday = moment(year + "-01-01", "YYYY-MM-DD").holiday(req.params.holiday);
if (!holiday) {
/* 'Leap Year' doesn't exist every year... */
continue;
}
let holiday;
/*
* NOTE: Memorial Day and Labor Day are two special cases -- the holiday is a Monday,
* however the entire weekend typically is holidy-esque. Account for that below.
*
* For those that have 'eve' celebrations, include those too.
*
* We (should) could expand this to account for Fri or Mon on the 4th of July or the
* entire weekend if it occurs on a Thu or Tues */
if (!date) {
holiday = moment(year + "-01-01", "YYYY-MM-DD").holiday(req.params.holiday);
if (!holiday) {
/* 'Leap Year' doesn't exist every year... */
continue;
}
let extraDays = 0;
switch (req.params.holiday.toLowerCase()) {
case 'labor day':
case 'memorial day':
extraDays = -2; /* Include two days prior */
break;
case 'christmas day':
case 'new year\'s day':
extraDays = -1; /* Include 'Eve' */
break;
}
let direction = extraDays < 0 ? -1 : 1;
/*
* NOTE: Memorial Day and Labor Day are two special cases -- the holiday is a Monday,
* however the entire weekend typically is holidy-esque. Account for that below.
*
* For those that have 'eve' celebrations, include those too.
*
* We (should) could expand this to account for Fri or Mon on the 4th of July or the
* entire weekend if it occurs on a Thu or Tues */
for (let i = 0; i <= Math.abs(extraDays); i++) {
let comparison = "strftime('%Y-%m-%d',taken)='" + holiday.format("YYYY-MM-DD") + "'";
/* If no holiday has been set yet, start the comparison function passed to WHERE
* otherwise append it with OR. */
let extraDays = 0;
switch (req.params.holiday.toLowerCase()) {
case 'labor day':
case 'memorial day':
extraDays = -2; /* Include two days prior */
break;
case 'christmas day':
case 'new year\'s day':
extraDays = -1; /* Include 'Eve' */
break;
}
let direction = extraDays < 0 ? -1 : 1;
for (let i = 0; i <= Math.abs(extraDays); i++) {
let comparison = "strftime('%Y-%m-%d',taken)='"
+ holiday.format("YYYY-MM-DD") +
+ "'";
/* If no holiday has been set yet, start the comparison function passed to WHERE
* otherwise append it with OR. */
if (!dayIsHoliday) {
dayIsHoliday = comparison;
} else {
dayIsHoliday += " OR " + comparison;
}
holiday.date(holiday.date() + direction);
}
} else {
let comparison = "strftime('%Y-%m-%d',taken)='"
+ moment(year + "-" + date.month + "-" + date.day, "YYYY-MM-DD").format("YYYY-MM-DD")
+ "'";
if (!dayIsHoliday) {
dayIsHoliday = comparison;
} else {
dayIsHoliday += " OR " + comparison;
}
holiday.date(holiday.date() + direction);
}
}