mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-06 07:14:30 -06:00
Split getMatchingTime into isTimeMatched for individual testing of timing configs, drop async
This commit is contained in:
@@ -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})`);
|
||||
|
||||
@@ -222,28 +222,29 @@ export const numberAsDay: Record<DayEnum, number> = {
|
||||
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<TimeConfig | null> {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user