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