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