From e84730af08425dc0ce709908acca69a04bb556d7 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 15 Nov 2023 20:55:46 -0600 Subject: [PATCH] Split getMatchingTime into isTimeMatched for individual testing of timing configs, drop async --- src/pages/api/cron.ts | 4 ++-- src/timing.ts | 41 +++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/pages/api/cron.ts b/src/pages/api/cron.ts index c44c201..9dba6b2 100644 --- a/src/pages/api/cron.ts +++ b/src/pages/api/cron.ts @@ -50,7 +50,7 @@ export default async function handler( const now = localNow(); const config = await fetchConfiguration(); - const matching = await getMatchingTime(config, now); + const matching = getMatchingTime(config, now); // No matching time - no notification to send. if (matching == null) { @@ -94,7 +94,7 @@ export default async function handler( : 'Dry run, not sending notification or marking identifier.', { identifier } ); - + // Send notification, mark (expire in 1 month) if (!isDry) { await sendNotification(`${matching.message} (${matching.name})`); diff --git a/src/timing.ts b/src/timing.ts index 2eca5f7..0377aec 100644 --- a/src/timing.ts +++ b/src/timing.ts @@ -222,28 +222,29 @@ export const numberAsDay: Record = { sunday: 0 }; -export async function getMatchingTime( +export function isTimeMatched( + time: TimeConfig, + now: Date = new Date() +): boolean { + if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false; + const nowTime = { + hours: now.getHours(), + minutes: now.getMinutes() + }; + + const startTime = time.time; + const endTime = addTime(time.time, time.maxLate ?? { hours: 0, minutes: 0 }); + + return ( + compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0 + ); +} + +export function getMatchingTime( config: Configuration, now: Date = new Date() -): Promise { - const times = config.times.filter((time) => { - // If the day doesn't match, skip. - if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false; - const nowTime = { - hours: now.getHours(), - minutes: now.getMinutes() - }; - - const startTime = time.time; - const endTime = addTime( - time.time, - time.maxLate ?? { hours: 0, minutes: 0 } - ); - - return ( - compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0 - ); - }); +): TimeConfig | null { + const times = config.times.filter((time) => isTimeMatched(time, now)); // This shouldn't be thrown, if I did my job right. if (times.length > 1)