Split getMatchingTime into isTimeMatched for individual testing of timing configs, drop async

This commit is contained in:
2023-11-15 20:55:46 -06:00
parent 18d392fb1f
commit e84730af08
2 changed files with 23 additions and 22 deletions

View File

@@ -50,7 +50,7 @@ export default async function handler(
const now = localNow(); const now = localNow();
const config = await fetchConfiguration(); const config = await fetchConfiguration();
const matching = await getMatchingTime(config, now); const matching = getMatchingTime(config, now);
// No matching time - no notification to send. // No matching time - no notification to send.
if (matching == null) { if (matching == null) {

View File

@@ -222,12 +222,10 @@ export const numberAsDay: Record<DayEnum, number> = {
sunday: 0 sunday: 0
}; };
export async function getMatchingTime( export function isTimeMatched(
config: Configuration, time: TimeConfig,
now: Date = new Date() now: Date = new Date()
): Promise<TimeConfig | null> { ): boolean {
const times = config.times.filter((time) => {
// If the day doesn't match, skip.
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false; if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
const nowTime = { const nowTime = {
hours: now.getHours(), hours: now.getHours(),
@@ -235,15 +233,18 @@ export async function getMatchingTime(
}; };
const startTime = time.time; const startTime = time.time;
const endTime = addTime( const endTime = addTime(time.time, time.maxLate ?? { hours: 0, minutes: 0 });
time.time,
time.maxLate ?? { hours: 0, minutes: 0 }
);
return ( return (
compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0 compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0
); );
}); }
export function getMatchingTime(
config: Configuration,
now: Date = new Date()
): TimeConfig | null {
const times = config.times.filter((time) => isTimeMatched(time, now));
// This shouldn't be thrown, if I did my job right. // This shouldn't be thrown, if I did my job right.
if (times.length > 1) if (times.length > 1)