Integray endpoint returning public holidays for Czech Republic

Here is one of the possible solution how to build simple endpoint in Integray that returns the name of public holidays based on the provided date in format DD-MM-YYYY as string. The biggest challenge was to calculate Easter Friday and Monday as it’s not based on a specific data but rather weeks shift from a specific data in a year and you have to count with leap years plus it have to Friday and Monday. However with a bit of help from ChatGPT I was able to put it together - maybe not the best solution but it’s working and is fast.

First I create endpoint (or if you want to test it then read this article on how to tune the script: Chicken-egg problem: should I create task first or endpoint first

In my case I used input schema with one string field where will be date in required form. Returning JSON with element “Result” what is required for our application where we want this endpoint to use.

Task is rather simple with step including JS Mapper connector:

Here the script with a bit of explanation below:

// calculate easter monday
function calculateEasterMonday(year) {
  const a = year % 19;
  const b = Math.floor(year / 100);
  const c = year % 100;
  const d = Math.floor(b / 4);
  const e = b % 4;
  const f = Math.floor((b + 8) / 25);
  const g = Math.floor((b - f + 1) / 3);
  const h = (19 * a + b - d - g + 15) % 30;
  const i = Math.floor(c / 4);
  const k = c % 4;
  const L = (32 + 2 * e + 2 * i - h - k) % 7;
  const m = Math.floor((a + 11 * h + 22 * L) / 451);
  const n = Math.floor((h + L - 7 * m + 114) / 31);
  const p = (h + L - 7 * m + 114) % 31;
  
  const easterMonth = n;
  const easterDay = p + 1;
  
  const easterDate = new Date(year, easterMonth - 1, easterDay);
  const easterMondayDate = new Date(easterDate.getTime());

  // get ester Monday date (sunday + 1)
  easterMondayDate.setDate(easterDate.getDate() + 1);

  const easterMondayYear = easterMondayDate.getFullYear();
  const easterMondayMonth = easterMondayDate.getMonth() + 1;
  const easterMondayDay = easterMondayDate.getDate();
  
  const easterMondayString = `${easterMondayDay < 10 ? '0' + easterMondayDay : easterMondayDay}-${easterMondayMonth < 10 ? '0' + easterMondayMonth : easterMondayMonth}-${easterMondayYear}`

  return easterMondayString;
};

// calculate ester friday
function calculateEasterFriday(year) {
  const a = year % 19;
  const b = Math.floor(year / 100);
  const c = year % 100;
  const d = Math.floor(b / 4);
  const e = b % 4;
  const f = Math.floor((b + 8) / 25);
  const g = Math.floor((b - f + 1) / 3);
  const h = (19 * a + b - d - g + 15) % 30;
  const i = Math.floor(c / 4);
  const k = c % 4;
  const L = (32 + 2 * e + 2 * i - h - k) % 7;
  const m = Math.floor((a + 11 * h + 22 * L) / 451);
  const n = Math.floor((h + L - 7 * m + 114) / 31);
  const p = (h + L - 7 * m + 114) % 31;
  
  const easterMonth = n;
  const easterDay = p + 1;
  
  const easterDate = new Date(year, easterMonth - 1, easterDay);
  const easterFridayDate = new Date(easterDate.getTime());

  // get ester Friday date (sunday - 2)
  easterFridayDate.setDate(easterDate.getDate() - 2);

  const easterFridayYear = easterFridayDate.getFullYear();
  const easterFridayMonth = easterFridayDate.getMonth() + 1;
  const easterFridayDay = easterFridayDate.getDate();
  
  const easterFridayString = `${easterFridayDay < 10 ? '0' + easterFridayDay : easterFridayDay}-${easterFridayMonth < 10 ? '0' + easterFridayMonth : easterFridayMonth}-${easterFridayYear}`

  return easterFridayString;
};

// validate date format using regex
const regex = /^(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-(\d{4})$/;
const dateIn = inputData[0]['Date']
var nameOut = "";

if(dateIn && dateIn != ""){

  // raise error if date is not in valid format
  if (!regex.test(dateIn)) {
    log.error("Invalid date format");
  };

  // extract year value and convert to number for easter calculation
  const yearIn = Number(dateIn.substring(6, 10));

  // 1.1.		  Nový rok
 	if(dateIn.substring(0, 5) == '01-01'){nameOut = "Nový rok"}

  // 1.5.		  Svátek práce
 	else if(dateIn.substring(0, 5) == '01-05'){nameOut = "Svátek práce"}

  // 8.5.		  Den vítězství
 	else if(dateIn.substring(0, 5) == '08-05'){nameOut = "Den vítězství"}

  // 5.7.		  Den slovanských věrozvěstů Cyrila a Metoděje
 	else if(dateIn.substring(0, 5) == '05-07'){nameOut = "Den slovanských věrozvěstů Cyrila a Metoděje"}

  // 6.7.		  Den upálení mistra Jana Husa
 	else if(dateIn.substring(0, 5) == '06-07'){nameOut = "Den upálení mistra Jana Husa"}

  // 28.9.	  Den české státnosti
 	else if(dateIn.substring(0, 5) == '28-09'){nameOut = "Den české státnosti"}

  // 28.10.	  Den vzniku samostatného československého státu
 	else if(dateIn.substring(0, 5) == '28-10'){nameOut = "Den vzniku samostatného československého státu"}

  // 17.11.	  Den boje za svobodu a demokracii
 	else if(dateIn.substring(0, 5) == '17-11'){nameOut = "Den boje za svobodu a demokracii"}

  // 24.12.	  Štědrý den
 	else if(dateIn.substring(0, 5) == '24-12'){nameOut = "Štědrý den"}

  // 25.12.	  1. svátek vánoční
 	else if(dateIn.substring(0, 5) == '25-12'){nameOut = "1. svátek vánoční"}

  // 26.12.	  2. svátek vánoční
	else if(dateIn.substring(0, 5) == '26-12'){nameOut = "2. svátek vánoční"}

  // function	Velký pátek
	else if(dateIn == calculateEasterFriday(yearIn)){nameOut = "Velký pátek"}

  // function Velikonoční pondělí
	else if(dateIn == calculateEasterMonday(yearIn)){nameOut = "Velikonoční pondělí"}
  
};

return [{"Result": nameOut}];

In the beginning I have two function that can return the Easter Friday and Monday in format DD-MM-YYYY. Next is the script that is first checking if string is in proper format. If not I raise error using log.error(<string>) and endpoint returns 400 - bad request response. Then I simply check the date value with fixed public holidays in CZ, here I check only DD-MM part of it but for Easter I do check the whole day including year.

Now you have to map the output from task to endpoint like this:

You can adjust the script to your needs. It can return public holidays from your country or even some special company events. Endpoint can be used within your company applications or even on web as you do not rely on any other information source so all is in your control. The only thing is that you need to maintain any changes within the list of public holidays.