Commit 3081c4d9 authored by Johannes Bill's avatar Johannes Bill

quick fix for bodystreet with date-holidays

parent 1cf3c06f
This diff is collapsed.
var holidayMapping = require('./holidayMapping.json'); const holidayMapping = require('./holidayMapping.json');
var easterSunday = require('./easterSunday'); const easterSunday = require('./easterSunday');
const DateHolidays = require('date-holidays');
var requestHandler = require('./regionInfoRequest');
let requestHandler = require('./regionInfoRequest');
var easterSundays = {}; const easterSundays = {};
var fixedHolidays = { const fixedHolidays = {
101: "0101", 101: '0101',
106: "0601", 106: '0601',
501: "0105", 501: '0105',
808: "0808", 808: '0808',
815: "1508", 815: '1508',
103: "0310", 103: '0310',
131: "3110", 131: '3110',
111: "0111", 111: '0111',
121: "2412", 121: '2412',
122: "2512", 122: '2512',
123: "2612" 123: '2612'
}; };
var variableOffsets = { const variableOffsets = {
301: -2, 301: -2,
302: 0, 302: 0,
303: 1, 303: 1,
...@@ -29,48 +29,45 @@ var variableOffsets = { ...@@ -29,48 +29,45 @@ var variableOffsets = {
603: 60 603: 60
}; };
function Holidays(year) { function Holidays(year) {
function getEasterSunday() { function getEasterSunday() {
if (!easterSundays[year]) if (!easterSundays[year]) {
easterSundays[year] = easterSunday.getEasterSunday(year); easterSundays[year] = easterSunday.getEasterSunday(year);
}
return easterSundays[year]; return easterSundays[year];
} }
function getFixedHolidays(fixedHolidays, object) { function getFixedHolidays(fixedHolidays, object) {
var result = object || {}; const result = object || {};
for (var key in fixedHolidays) { for (const key in fixedHolidays) {
var dateString = fixedHolidays[key]; const dateString = fixedHolidays[key];
var date = parseInt(dateString.slice(0, 2)); const date = parseInt(dateString.slice(0, 2));
var month = parseInt(dateString.slice(2, 4)); const month = parseInt(dateString.slice(2, 4));
result[key] = new Date(year, month - 1, date); result[key] = new Date(year, month - 1, date);
} }
return result; return result;
} }
function getVariableHolidaysMap(offsets, object) { function getVariableHolidaysMap(offsets, object) {
var result = object || {}; const result = object || {};
var easterDate = getEasterSunday(); const easterDate = getEasterSunday();
for (var key in offsets) { for (const key in offsets) {
var offset = offsets[key]; const offset = offsets[key];
result[key] = new Date(year, easterDate.getMonth(), easterDate.getDate() + offset); result[key] = new Date(year, easterDate.getMonth(), easterDate.getDate() + offset);
} }
return result; return result;
} }
var holidays = getFixedHolidays(fixedHolidays); let holidays = getFixedHolidays(fixedHolidays);
holidays = getVariableHolidaysMap(variableOffsets, holidays); holidays = getVariableHolidaysMap(variableOffsets, holidays);
function getIsAugsburg(plz) { function getIsAugsburg(plz) {
return !!(86150 <= plz && plz <= 86199); return !!(86150 <= plz && plz <= 86199);
} }
function isInEpochSpan(date, epochSpan) { function isInEpochSpan(date, epochSpan) {
var dateTime = date.getTime(); const dateTime = date.getTime();
return epochSpan[0].getTime() <= dateTime && dateTime <= epochSpan[1].getTime() return epochSpan[0].getTime() <= dateTime && dateTime <= epochSpan[1].getTime();
} }
function getRelevantHolidayKeys(epochSpan) { function getRelevantHolidayKeys(epochSpan) {
...@@ -80,9 +77,9 @@ function Holidays(year) { ...@@ -80,9 +77,9 @@ function Holidays(year) {
} }
function getHolidayKeys(info, epochSpan, cb) { function getHolidayKeys(info, epochSpan, cb) {
var initKeys = getRelevantHolidayKeys(epochSpan); const initKeys = getRelevantHolidayKeys(epochSpan);
var specialKeys = holidayMapping["specificHolidays"].filter(function(key) { const specialKeys = holidayMapping['specificHolidays'].filter(function (key) {
return initKeys.indexOf(key) >= 0; return initKeys.indexOf(key) >= 0;
}); });
...@@ -91,50 +88,53 @@ function Holidays(year) { ...@@ -91,50 +88,53 @@ function Holidays(year) {
} }
requestHandler.getBundesland(info, function (err, blnd) { requestHandler.getBundesland(info, function (err, blnd) {
if (err) return cb(err); if (err) {
var keys = holidayMapping["sureHolidays"].filter(function(key) { return cb(err);
}
const keys = holidayMapping['sureHolidays'].filter(function (key) {
return initKeys.indexOf(key) >= 0; return initKeys.indexOf(key) >= 0;
}); });
var regionMap = holidayMapping["region"][blnd]; const regionMap = holidayMapping['region'][blnd];
if(regionMap) { if (regionMap) {
var regionHolidays = regionMap['default']; const regionHolidays = regionMap['default'];
for (var i = 0; i < regionHolidays.length; i++) { for (let i = 0; i < regionHolidays.length; i++) {
var key = regionHolidays[i]; const key = regionHolidays[i];
if (initKeys.indexOf(key) >= 0) { if (initKeys.indexOf(key) >= 0) {
keys.push(key); keys.push(key);
} }
} }
} else {
console.error(new Error('No Mapping for Bundesland: ' + blnd));
} }
else { if (blnd != 'BY') {
console.error(new Error("No Mapping for Bundesland: " + blnd));
}
if(blnd != "BY") {
cb(null, keys); cb(null, keys);
} } else {
else { if (regionMap['augsburg'] && getIsAugsburg(info.zip)) {
if(regionMap["augsburg"] && getIsAugsburg(info.zip)) { const augsHDs = regionMap['augsburg'];
var augsHDs = regionMap["augsburg"]; for (let i = 0; i < augsHDs.length; i++) {
for (var i = 0; i < augsHDs.length; i++) { const key = augsHDs[i];
var key = augsHDs[i]; if (initKeys.indexOf(key) >= 0) {
if(initKeys.indexOf(key) >= 0)
keys.push(key); keys.push(key);
}
} }
} }
if(regionMap["kath"] && regionMap["kath"].length > 0) { if (regionMap['kath'] && regionMap['kath'].length > 0) {
requestHandler.getKatholisch(info, function(err, kath) { requestHandler.getKatholisch(info, function (err, kath) {
if(err) return cb(err); if (err) {
if(kath) { return cb(err);
var kathHDs = regionMap["kath"]; }
for (var i = 0; i < kathHDs.length; i++) { if (kath) {
var key = kathHDs[i]; const kathHDs = regionMap['kath'];
if(initKeys.indexOf(key) >= 0) for (let i = 0; i < kathHDs.length; i++) {
const key = kathHDs[i];
if (initKeys.indexOf(key) >= 0) {
keys.push(key); keys.push(key);
}
} }
} }
cb(null, keys); cb(null, keys);
}) });
} } else {
else {
cb(null, keys); cb(null, keys);
} }
} }
...@@ -142,30 +142,50 @@ function Holidays(year) { ...@@ -142,30 +142,50 @@ function Holidays(year) {
} }
this.getAllHolidays = function (info, epochSpan, cb) { this.getAllHolidays = function (info, epochSpan, cb) {
if (info.countryCode && ['DE', 'GB', 'US'].includes(info.countryCode)) {
const dateHolidays = new DateHolidays(info.countryCode);
const holidayDays = dateHolidays
.getHolidays(2021)
.filter(x => x.type === 'public')
.map(x => {
const dateDay = x.date.slice(0, 10); // strip hours
const date = new Date(`${dateDay}T00:00`); // converts 2021-05-13 to "2021-05-12T22:00:00.000Z"
return date;
});
return cb(null, holidayDays);
}
getHolidayKeys(info, epochSpan, function (err, keys) { getHolidayKeys(info, epochSpan, function (err, keys) {
if (err) if (err) {
return cb(err); return cb(err);
var result = []; }
for (var i = 0; i < keys.length; i++) { const result = [];
var key = keys[i]; for (let i = 0; i < keys.length; i++) {
var elem = holidays[key]; const key = keys[i];
if (elem) const elem = holidays[key];
if (elem) {
result.push(elem); result.push(elem);
}
} }
cb(err, result); cb(err, result);
}); });
}; };
this.getHoliday = function (id) { this.getHoliday = function (id) {
if(id == 0) { if (id == 0) {
var ret = []; const ret = [];
for(var key in holidays) for (const key in holidays) {
ret.push(holidays[key]); ret.push(holidays[key]);
}
return ret; return ret;
} }
if (id in holidays) if (id in holidays) {
return [holidays[id]]; return [holidays[id]];
console.error("invalid holiday id " + id); }
console.error('invalid holiday id ' + id);
return null; return null;
}; };
} }
...@@ -177,20 +197,19 @@ module.exports = Holidays; ...@@ -177,20 +197,19 @@ module.exports = Holidays;
* for testing reasons * for testing reasons
* @param handler * @param handler
*/ */
module.exports.setRequestHandler = function(handler) { module.exports.setRequestHandler = function (handler) {
requestHandler = handler; requestHandler = handler;
}; };
module.exports.setTestHandler = function() { module.exports.setTestHandler = function () {
requestHandler = { requestHandler = {
getBundesland: function (plz, cb) { getBundesland: function (plz, cb) {
// console.log("bundesland is bayern"); // console.log("bundesland is bayern");
cb(null, "BY"); cb(null, 'BY');
}, },
getKatholisch: function (plz, cb) { getKatholisch: function (plz, cb) {
// console.log("is katholic"); // console.log("is katholic");
cb(null, true); cb(null, true);
} }
}; };
}; };
This diff is collapsed.
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"dependencies": { "dependencies": {
"async": "^2.0.1", "async": "^2.0.1",
"bluebird": "^3.4.6", "bluebird": "^3.4.6",
"date-holidays": "^3.3.0",
"moment": "^2.15.0" "moment": "^2.15.0"
}, },
"devDependencies": { "devDependencies": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment