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');
var easterSunday = require('./easterSunday');
var requestHandler = require('./regionInfoRequest');
var easterSundays = {};
var fixedHolidays = {
101: "0101",
106: "0601",
501: "0105",
808: "0808",
815: "1508",
103: "0310",
131: "3110",
111: "0111",
121: "2412",
122: "2512",
123: "2612"
const holidayMapping = require('./holidayMapping.json');
const easterSunday = require('./easterSunday');
const DateHolidays = require('date-holidays');
let requestHandler = require('./regionInfoRequest');
const easterSundays = {};
const fixedHolidays = {
101: '0101',
106: '0601',
501: '0105',
808: '0808',
815: '1508',
103: '0310',
131: '3110',
111: '0111',
121: '2412',
122: '2512',
123: '2612'
};
var variableOffsets = {
const variableOffsets = {
301: -2,
302: 0,
303: 1,
......@@ -29,48 +29,45 @@ var variableOffsets = {
603: 60
};
function Holidays(year) {
function getEasterSunday() {
if (!easterSundays[year])
if (!easterSundays[year]) {
easterSundays[year] = easterSunday.getEasterSunday(year);
}
return easterSundays[year];
}
function getFixedHolidays(fixedHolidays, object) {
var result = object || {};
for (var key in fixedHolidays) {
var dateString = fixedHolidays[key];
var date = parseInt(dateString.slice(0, 2));
var month = parseInt(dateString.slice(2, 4));
const result = object || {};
for (const key in fixedHolidays) {
const dateString = fixedHolidays[key];
const date = parseInt(dateString.slice(0, 2));
const month = parseInt(dateString.slice(2, 4));
result[key] = new Date(year, month - 1, date);
}
return result;
}
function getVariableHolidaysMap(offsets, object) {
var result = object || {};
var easterDate = getEasterSunday();
for (var key in offsets) {
var offset = offsets[key];
const result = object || {};
const easterDate = getEasterSunday();
for (const key in offsets) {
const offset = offsets[key];
result[key] = new Date(year, easterDate.getMonth(), easterDate.getDate() + offset);
}
return result;
}
var holidays = getFixedHolidays(fixedHolidays);
let holidays = getFixedHolidays(fixedHolidays);
holidays = getVariableHolidaysMap(variableOffsets, holidays);
function getIsAugsburg(plz) {
return !!(86150 <= plz && plz <= 86199);
}
function isInEpochSpan(date, epochSpan) {
var dateTime = date.getTime();
return epochSpan[0].getTime() <= dateTime && dateTime <= epochSpan[1].getTime()
const dateTime = date.getTime();
return epochSpan[0].getTime() <= dateTime && dateTime <= epochSpan[1].getTime();
}
function getRelevantHolidayKeys(epochSpan) {
......@@ -80,9 +77,9 @@ function Holidays(year) {
}
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;
});
......@@ -91,50 +88,53 @@ function Holidays(year) {
}
requestHandler.getBundesland(info, function (err, blnd) {
if (err) return cb(err);
var keys = holidayMapping["sureHolidays"].filter(function(key) {
if (err) {
return cb(err);
}
const keys = holidayMapping['sureHolidays'].filter(function (key) {
return initKeys.indexOf(key) >= 0;
});
var regionMap = holidayMapping["region"][blnd];
if(regionMap) {
var regionHolidays = regionMap['default'];
for (var i = 0; i < regionHolidays.length; i++) {
var key = regionHolidays[i];
const regionMap = holidayMapping['region'][blnd];
if (regionMap) {
const regionHolidays = regionMap['default'];
for (let i = 0; i < regionHolidays.length; i++) {
const key = regionHolidays[i];
if (initKeys.indexOf(key) >= 0) {
keys.push(key);
}
}
} else {
console.error(new Error('No Mapping for Bundesland: ' + blnd));
}
else {
console.error(new Error("No Mapping for Bundesland: " + blnd));
}
if(blnd != "BY") {
if (blnd != 'BY') {
cb(null, keys);
}
else {
if(regionMap["augsburg"] && getIsAugsburg(info.zip)) {
var augsHDs = regionMap["augsburg"];
for (var i = 0; i < augsHDs.length; i++) {
var key = augsHDs[i];
if(initKeys.indexOf(key) >= 0)
} else {
if (regionMap['augsburg'] && getIsAugsburg(info.zip)) {
const augsHDs = regionMap['augsburg'];
for (let i = 0; i < augsHDs.length; i++) {
const key = augsHDs[i];
if (initKeys.indexOf(key) >= 0) {
keys.push(key);
}
}
}
if(regionMap["kath"] && regionMap["kath"].length > 0) {
requestHandler.getKatholisch(info, function(err, kath) {
if(err) return cb(err);
if(kath) {
var kathHDs = regionMap["kath"];
for (var i = 0; i < kathHDs.length; i++) {
var key = kathHDs[i];
if(initKeys.indexOf(key) >= 0)
if (regionMap['kath'] && regionMap['kath'].length > 0) {
requestHandler.getKatholisch(info, function (err, kath) {
if (err) {
return cb(err);
}
if (kath) {
const kathHDs = regionMap['kath'];
for (let i = 0; i < kathHDs.length; i++) {
const key = kathHDs[i];
if (initKeys.indexOf(key) >= 0) {
keys.push(key);
}
}
}
cb(null, keys);
})
}
else {
});
} else {
cb(null, keys);
}
}
......@@ -142,30 +142,50 @@ function Holidays(year) {
}
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) {
if (err)
if (err) {
return cb(err);
var result = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var elem = holidays[key];
if (elem)
}
const result = [];
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const elem = holidays[key];
if (elem) {
result.push(elem);
}
}
cb(err, result);
});
};
this.getHoliday = function (id) {
if(id == 0) {
var ret = [];
for(var key in holidays)
if (id == 0) {
const ret = [];
for (const key in holidays) {
ret.push(holidays[key]);
}
return ret;
}
if (id in holidays)
if (id in holidays) {
return [holidays[id]];
console.error("invalid holiday id " + id);
}
console.error('invalid holiday id ' + id);
return null;
};
}
......@@ -177,20 +197,19 @@ module.exports = Holidays;
* for testing reasons
* @param handler
*/
module.exports.setRequestHandler = function(handler) {
module.exports.setRequestHandler = function (handler) {
requestHandler = handler;
};
module.exports.setTestHandler = function() {
module.exports.setTestHandler = function () {
requestHandler = {
getBundesland: function (plz, cb) {
// console.log("bundesland is bayern");
cb(null, "BY");
// console.log("bundesland is bayern");
cb(null, 'BY');
},
getKatholisch: function (plz, cb) {
// console.log("is katholic");
// console.log("is katholic");
cb(null, true);
}
};
};
This diff is collapsed.
......@@ -5,6 +5,7 @@
"dependencies": {
"async": "^2.0.1",
"bluebird": "^3.4.6",
"date-holidays": "^3.3.0",
"moment": "^2.15.0"
},
"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