Fix Rooms Accessible View missing yesterday's still-in-house bookings

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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-09-03 09:26:14 +00:00
parent 2d2541576d
commit 96674c58c8

View file

@ -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}` })
}