From 96674c58c8e9f1b639b1dfbd62a7130330953b87 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Thu, 3 Sep 2026 09:26:14 +0000 Subject: [PATCH] Fix Rooms Accessible View missing yesterday's still-in-house bookings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fetchRoomOccupancy() only queried NewBook for today..today, trusting its period_from/period_to filter to include a booking that arrived yesterday and hasn't checked out. Per hvac's scheduler (which learned this the hard way), that overlap isn't reliable server-side — widen the fetch window to yesterday..today and filter locally against each booking's own arrival/departure dates instead. A room with a guest still in "arrived" status from yesterday was showing as free/available today. Also fixed the unrelated `?unoccupied=true` task filter, which still called fetchOccupiedSiteIds() — a function renamed to fetchRoomOccupancy() during the July occupied/arriving split (25d6323) but never updated at this second call site, so it would throw if ever exercised. Co-Authored-By: Claude Sonnet 5 --- backend/src/routes/tasks.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/src/routes/tasks.js b/backend/src/routes/tasks.js index 76795a7..2e5ce88 100644 --- a/backend/src/routes/tasks.js +++ b/backend/src/routes/tasks.js @@ -44,12 +44,24 @@ function classifyBookingStatus(rawStatus) { async function fetchRoomOccupancy() { const today = new Date().toISOString().slice(0, 10) - const bookings = await fetchBookings(today, today) + const yesterday = new Date(Date.now() - 86400000).toISOString().slice(0, 10) + // Query yesterday..today, not just today: a booking that arrived yesterday and + // hasn't checked out yet must still show as occupied. Don't trust NewBook's + // period_from/period_to filter to do that overlap server-side (hvac's scheduler + // learned the same lesson — see its lib/scheduler.js) — fetch the wider window and + // filter locally against each booking's own arrival/departure dates instead. + const bookings = await fetchBookings(yesterday, today) const occupied = new Set() const arriving = new Set() for (const b of bookings) { const siteId = b.booking_site_id ?? b.site_id if (siteId == null) continue + const arrival = String(b.booking_arrival ?? '').slice(0, 10) + const departure = String(b.booking_departure ?? '').slice(0, 10) + // Only bookings whose stay actually covers today belong in either bucket — the + // wider window also pulls back yesterday's now-departed booking, which shouldn't + // count towards either set. + if (arrival > today || departure < today) continue const id = String(siteId) if (classifyBookingStatus(b.booking_status) === 'occupied') occupied.add(id) else arriving.add(id) @@ -96,7 +108,7 @@ export async function taskRoutes(app) { if (q.unoccupied === 'true') { let occupied try { - occupied = await fetchOccupiedSiteIds() + ({ occupied } = await fetchRoomOccupancy()) } catch (err) { return reply.status(502).send({ error: `NewBook error: ${err.message}` }) }