From 85196852f4e7cb0ea0ad67ad2bd2df2ab6405db5 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 21 May 2026 10:02:39 -0700 Subject: [PATCH] extract active maintenance window check --- server/src/utils/maintenanceWindow.ts | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 server/src/utils/maintenanceWindow.ts diff --git a/server/src/utils/maintenanceWindow.ts b/server/src/utils/maintenanceWindow.ts new file mode 100644 index 000000000..aab4efd64 --- /dev/null +++ b/server/src/utils/maintenanceWindow.ts @@ -0,0 +1,28 @@ +import type { MaintenanceWindow } from "@/types/index.js"; + +export const isWindowActive = (window: MaintenanceWindow, now: Date = new Date()): boolean => { + if (!window.active) { + return false; + } + + const start = new Date(window.start); + const end = new Date(window.end); + const repeatInterval = window.repeat || 0; + + if (start <= now && end >= now) { + return true; + } + + // For a recurring window whose first occurrence is in the past, + // advance start/end by the repeat interval until we either catch + // an occurrence covering `now` or move past it. + while (start < now && repeatInterval !== 0) { + start.setTime(start.getTime() + repeatInterval); + end.setTime(end.getTime() + repeatInterval); + if (start <= now && end >= now) { + return true; + } + } + + return false; +};