Files
worldmonitor/scripts/lib/quiet-hours.cjs
Sebastien Melki 3314db2664 fix(relay): treat quiet hours start === end as disabled, not 24/7 (#3061) (#3066)
* fix(relay): treat quiet hours start === end as disabled, not 24/7 (#3061)

When quietHoursStart equalled quietHoursEnd, the midnight-spanning
branch evaluated `hour >= N || hour < N` which is true for all hours,
silently suppressing all non-critical alerts permanently.

Add an early return for start === end in the relay and reject the
combination in Convex validation.

Closes #3061

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: cross-check quiet hours start/end against persisted value on single-field updates

Addresses Greptile review: validateQuietHoursArgs only caught start===end
when both arrived in the same call. Now the mutation handlers also check
against the DB record to prevent sequential single-field updates from
creating a start===end state.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: gate quiet hours start===end check on effectiveEnabled

Only enforce the start !== end invariant when quiet hours are effectively
enabled. This allows users with legacy start===end records to disable
quiet hours, change timezone/override, or recover from old bad state
without getting locked out.

Addresses koala73's P1 review feedback on #3066.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* refactor(relay): extract quiet-hours + consolidate equality check, add tests

- Move isInQuietHours/toLocalHour to scripts/lib/quiet-hours.cjs so they
  are testable without importing the full relay (which has top-level
  side effects and env requirements).
- Drop the unconditional start===end check from validateQuietHoursArgs;
  the effectiveEnabled-guarded check in setQuietHours /
  setQuietHoursForUser is now the single source of truth. Previously a
  user disabling quiet hours with start===end would be rejected even
  though the values are irrelevant when disabled.
- Add tests/quiet-hours.test.mjs covering: disabled, start===end
  regression (#3061), midnight-spanning window, same-day window,
  inclusive/exclusive bounds, invalid timezone, timezone handling,
  defaults.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Elie Habib <elie.habib@gmail.com>
2026-04-14 13:14:53 +04:00

32 lines
946 B
JavaScript

'use strict';
function toLocalHour(nowMs, timezone) {
try {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone: timezone,
hour: 'numeric',
hour12: false,
}).formatToParts(new Date(nowMs));
const h = parts.find(p => p.type === 'hour');
return h ? parseInt(h.value, 10) : -1;
} catch {
return -1;
}
}
function isInQuietHours(rule, nowMs = Date.now()) {
if (!rule.quietHoursEnabled) return false;
const start = rule.quietHoursStart ?? 22;
const end = rule.quietHoursEnd ?? 7;
if (start === end) return false; // same hour = no quiet window
const tz = rule.quietHoursTimezone ?? 'UTC';
const localHour = toLocalHour(nowMs, tz);
if (localHour === -1) return false;
// spans midnight when start > end (e.g. 23:00-07:00)
return start < end
? localHour >= start && localHour < end
: localHour >= start || localHour < end;
}
module.exports = { toLocalHour, isInQuietHours };