-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/joeyhage/thinq-notifier
- Loading branch information
Showing
5 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
export function formatDate(date: Date, timezoneCode?: string): string { | ||
console.log(date, timezoneCode); | ||
return new Intl.DateTimeFormat("default", { | ||
month: "short", | ||
day: "numeric", | ||
hour: "numeric", | ||
minute: "numeric", | ||
hour12: true, | ||
timeZone: timezoneCode, | ||
timeZoneName: "short", | ||
}).format(date); | ||
} | ||
|
||
export function determineThresholdDatetime(eventDate: Date): Date { | ||
const thresholdHours = Number(process.env.NOTIFICATION_THRESHOLD_HRS); | ||
const thresholdDatetime = new Date(eventDate.getTime()); | ||
thresholdDatetime.setHours(eventDate.getHours() + thresholdHours); | ||
return thresholdDatetime; | ||
} | ||
|
||
export function hasThresholdTimePassed(thresholdDatetime: Date): boolean { | ||
return Date.now() > thresholdDatetime.getTime(); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { determineThresholdDatetime } from "../lambda/datetime-util"; | ||
|
||
describe("determineThresholdDatetime", () => { | ||
test("should return `NOTIFICATION_THRESHOLD_HRS` after event date", async () => { | ||
// given | ||
process.env.NOTIFICATION_THRESHOLD_HRS = "3"; | ||
const eventDate = new Date("2021-11-01T12:00:00.000Z"); | ||
|
||
// when | ||
const result = determineThresholdDatetime(eventDate); | ||
|
||
// then | ||
expect(result).toEqual(new Date("2021-11-01T15:00:00.000Z")); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { shouldSendRepeatNotification } from "../lambda/index"; | ||
|
||
describe("test shouldSendRepeatNotification given `NOTIFICATION_FREQ_HRS`=3 and `MAX_NOTIFICATIONS`=2", () => { | ||
|
||
beforeEach(() => { | ||
process.env.NOTIFICATION_FREQ_HRS = "3"; | ||
process.env.MAX_NOTIFICATIONS = "2"; | ||
}); | ||
|
||
test("should send repeat notification given washer finished 3 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 3); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test("should send repeat notification given washer finished 6 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 6); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test("should not send repeat notification given washer finished 9 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 9); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should not send repeat notification given washer finished 9 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 9); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should not send repeat notification given washer finished 4 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 4); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should not send repeat notification given washer finished 7 hours ago", async () => { | ||
// given | ||
const thresholdTime = new Date(); | ||
thresholdTime.setHours(thresholdTime.getHours() - 7); | ||
thresholdTime.setMinutes(thresholdTime.getMinutes() - 20); | ||
|
||
// when | ||
const result = shouldSendRepeatNotification(thresholdTime); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
}); |
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