Last active
September 30, 2019 17:14
-
-
Save asebastian-aras/c99c923ab1e56af4450360642f35c453 to your computer and use it in GitHub Desktop.
Code added as an onBeforeAdd to the business calendar year ItemType, which gathers a list of holidays for the given year/country and creates exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* There is a publicly available API that can be used to get a list of holidays for a given country | |
* in a given year. The goal of this method is to help automate the process of creating a business calendar | |
* by creating the Business Calendar Exceptions for an admin. | |
* | |
* Documentation for this API can be found at https://date.nager.at/Api | |
*/ | |
Innovator inn = this.getInnovator(); | |
// The URL we will be pinging to get the list of public holidays for our country | |
string dateApiUrl = "https://date.nager.at/api/v2"; | |
// Validate that we have the data needed to request the holidays | |
string year = this.getProperty("year", ""); | |
if (string.IsNullOrEmpty(year)) return inn.newError("Cannot run without 'year' property"); | |
string countryCode = this.getProperty("country_code", ""); | |
if (string.IsNullOrEmpty(year)) return inn.newError("Cannot run without 'country_code' property"); | |
// Make our request to the publicly available API | |
string fullApiRequestUrl = String.Format("{0}/publicholidays/{1}/{2}", dateApiUrl, year, countryCode); | |
using (WebClient wc = new WebClient()) | |
{ | |
string responseString = wc.DownloadString(fullApiRequestUrl); | |
// Since the response is a JSON object, we'll turn it into an array so it's easier to work with | |
Newtonsoft.Json.Linq.JArray responseArray = Newtonsoft.Json.Linq.JArray.Parse(responseString); | |
// Loop through the list of holidays returned by the API and create an Exception to represent each one | |
foreach (var dateObject in responseArray) | |
{ | |
var newCalendarException = this.createRelationship("Business Calendar Exception", "add"); | |
newCalendarException.setProperty("day_date", inn.getI18NSessionContext().ConvertToNeutral((string)dateObject["date"], "string", "")); | |
newCalendarException.setProperty("description", (string)dateObject["name"]); | |
newCalendarException.setProperty("day_off", "1"); | |
} | |
// Since this is an OnBeforeAdd event, we will return the context item without applying. Relationships will be saved during the “add” action. | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment