Last active
July 14, 2023 13:04
-
-
Save dhcole/90a79c3f9b9224cc34c189f56ac1900d to your computer and use it in GitHub Desktop.
A Lambda handler to check whether today is a holiday, built to be used with Amazon Connect
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
/* | |
* | |
* Source: https://www.state.nj.us/nj/about/facts/holidays/ | |
* Convert to JSON using ChatGPT prompt: | |
* "Convert this table to JSON, converting dates to ISO format (pasted table)" | |
* | |
*/ | |
const HOLIDAYS = [ | |
{ | |
holiday: "New Year's Day", | |
date: "2023-01-02", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Martin Luther King Jr. Day", | |
date: "2023-01-16", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Presidents Day", | |
date: "2023-02-20", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Good Friday", | |
date: "2023-04-07", | |
day: "Friday", | |
}, | |
{ | |
holiday: "Memorial Day", | |
date: "2023-05-29", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Juneteenth", | |
date: "2023-06-16", | |
day: "Friday", | |
}, | |
{ | |
holiday: "Independence Day", | |
date: "2023-07-04", | |
day: "Tuesday", | |
}, | |
{ | |
holiday: "Labor Day", | |
date: "2023-09-04", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Columbus Day", | |
date: "2023-10-09", | |
day: "Monday", | |
}, | |
{ | |
holiday: "Election Day", | |
date: "2023-11-07", | |
day: "Tuesday", | |
}, | |
{ | |
holiday: "Veteran's Day", | |
date: "2023-11-10", | |
day: "Friday", | |
}, | |
{ | |
holiday: "Thanksgiving Day", | |
date: "2023-11-23", | |
day: "Thursday", | |
}, | |
{ | |
holiday: "Christmas Day", | |
date: "2023-12-25", | |
day: "Monday", | |
}, | |
]; | |
const date = new Date(); | |
const options = { | |
timeZone: "America/New_York", | |
year: "numeric", | |
month: "2-digit", | |
day: "2-digit", | |
}; | |
const usEasternDate = date.toLocaleDateString("en-US", options); | |
// Rearrange the date to ISO format (YYYY-MM-DD) | |
const [month, day, year] = usEasternDate.split("/"); | |
const isoDate = `${year}-${month}-${day}`; | |
const handler = async (event) => { | |
const eventDate = event?.Details?.Parameters?.date; | |
const today = eventDate ? eventDate : isoDate; | |
const holiday = HOLIDAYS.find((day) => day.date === today); | |
return holiday ? holiday : { holiday: "none", date: today }; | |
}; | |
module.exports = { | |
handler, | |
}; |
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
const { handler } = require("./holidayCheck"); | |
describe("Test the holidayCheck function", () => { | |
it("Returns the current date", async () => { | |
const date = new Date(); | |
const options = { | |
timeZone: "America/New_York", | |
year: "numeric", | |
month: "2-digit", | |
day: "2-digit", | |
}; | |
const usEasternDate = date.toLocaleDateString("en-US", options); | |
// Rearrange the date to ISO format (YYYY-MM-DD) | |
const [month, day, year] = usEasternDate.split("/"); | |
const isoDate = `${year}-${month}-${day}`; | |
const res = handler(); | |
expect(res.date).toEqual(isoDate); | |
}); | |
it("Returns the a matching holiday", async () => { | |
const res = handler({ | |
Details: { | |
Parameters: { | |
date: "2023-12-25", | |
}, | |
}, | |
}); | |
expect(res.date).toEqual("2023-12-25"); | |
expect(res.holiday).toEqual("Christmas Day"); | |
}); | |
it("Doesn't return a holiday on an unmatched date", async () => { | |
const res = handler({ | |
Details: { | |
Parameters: { | |
date: "2023-05-01", | |
}, | |
}, | |
}); | |
expect(res.date).toEqual("2023-05-01"); | |
expect(res.holiday).toEqual("none"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment