From 5998d2583ab6ea4cd6c36a0d5f404265db6c83e8 Mon Sep 17 00:00:00 2001 From: mrkmntal Date: Fri, 17 Apr 2026 11:08:47 -0400 Subject: [PATCH] Make Hazard history more robust --- src/hazard-history.mjs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/hazard-history.mjs b/src/hazard-history.mjs index 5c5c659..b99858f 100644 --- a/src/hazard-history.mjs +++ b/src/hazard-history.mjs @@ -72,6 +72,21 @@ const isSameLogicalHazard = (left, right) => left.location === right.location && left.hazardType === right.hazardType && left.source === right.source; +const isSameRequestedLocation = (entry, location, locationKey) => { + if (locationKey && entry.locationKey === locationKey) return true; + return entry.location === location; +}; + +const upgradeEntryLocationKey = (entry, location, locationKey) => { + if (!locationKey || entry.locationKey === locationKey) return entry; + return { + ...entry, + location, + locationKey, + key: generateKey(locationKey, entry.hazardType, entry.source), + }; +}; + const mergeEntries = (existing, incoming) => { const existingEncountered = normalizeTimestamp(existing.encounteredAt, incoming.encounteredAt); const incomingEncountered = normalizeTimestamp(incoming.encounteredAt, existing.encounteredAt); @@ -167,18 +182,19 @@ const updateHistory = async (payload) => { // Mark previously ongoing hazards for this location as ended if no longer active history = history.map((entry) => { - const entryMatchKey = entry.locationKey || entry.location; - if (entryMatchKey !== matchKey) return entry; + if (!isSameRequestedLocation(entry, location, locationKey)) return entry; + + const upgradedEntry = upgradeEntryLocationKey(entry, location, locationKey); // If this entry is ongoing but not in the current active set, mark it as ended - if (entry.ongoing && !activeKeys.has(entry.key)) { + if (upgradedEntry.ongoing && !activeKeys.has(upgradedEntry.key)) { return { - ...entry, + ...upgradedEntry, ongoing: false, lastSeenAt: now, }; } - return entry; + return upgradedEntry; }); // Add or update active hazards