Room planner: add debug-room endpoint, fix next_status null check
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
916dc50390
commit
0d14473daf
2 changed files with 79 additions and 2 deletions
|
|
@ -47,6 +47,83 @@ function filterBookingData(booking, canSeeGuest, canSeeRate, canSeeAllNotes, vis
|
|||
export async function roomRoutes(app) {
|
||||
app.addHook('preHandler', requireAuth)
|
||||
|
||||
// GET /api/rooms/debug-room?room=101&date=YYYY-MM-DD
|
||||
// Returns raw NewBook data for a single room — use to diagnose bracket/booking issues.
|
||||
app.get('/api/rooms/debug-room', async (req, reply) => {
|
||||
if (!hasCap(req, 'settings')) return reply.status(403).send({ error: 'Forbidden' })
|
||||
|
||||
const viewDate = req.query.date || new Date().toISOString().slice(0, 10)
|
||||
const roomName = req.query.room
|
||||
const yesterday = dateOffset(viewDate, -1)
|
||||
const tomorrow = dateOffset(viewDate, +1)
|
||||
const dayAfterTomorrow = dateOffset(viewDate, +2)
|
||||
|
||||
const [sites, bookings] = await Promise.all([
|
||||
fetchSites(),
|
||||
fetchBookings(yesterday, dayAfterTomorrow),
|
||||
])
|
||||
|
||||
if (!roomName) {
|
||||
const sample = bookings[0] || {}
|
||||
return {
|
||||
hint: 'Pass ?room=<site_name> to inspect a specific room',
|
||||
view_date: viewDate,
|
||||
total_sites: sites.length,
|
||||
total_bookings: bookings.length,
|
||||
booking_field_names: Object.keys(sample),
|
||||
site_field_names: sites.length ? Object.keys(sites[0]) : [],
|
||||
site_list: sites.map(s => ({ id: s.site_id, name: s.site_name })),
|
||||
}
|
||||
}
|
||||
|
||||
const site = sites.find(s => s.site_name === roomName)
|
||||
if (!site) return { error: `Room "${roomName}" not found`, available: sites.map(s => s.site_name) }
|
||||
|
||||
const siteId = String(site.site_id)
|
||||
const idFields = bookings.length
|
||||
? Object.keys(bookings[0]).filter(k =>
|
||||
k.toLowerCase().includes('site') || k.toLowerCase().includes('room') ||
|
||||
k.toLowerCase().includes('unit') || k.toLowerCase().includes('location')
|
||||
)
|
||||
: []
|
||||
|
||||
const matchFn = b =>
|
||||
idFields.some(f => String(b[f] || '') === siteId) ||
|
||||
String(b.site_id || '') === siteId ||
|
||||
String(b.booking_site_id || '') === siteId ||
|
||||
(b.site_name && b.site_name === roomName)
|
||||
|
||||
const siteBookings = bookings.filter(matchFn)
|
||||
|
||||
const classified = classifyRoom(site, bookings, viewDate, yesterday, tomorrow)
|
||||
|
||||
return {
|
||||
view_date: viewDate, yesterday, tomorrow, day_after_tomorrow: dayAfterTomorrow,
|
||||
site: { id: siteId, name: site.site_name, fields: Object.keys(site) },
|
||||
booking_id_fields: idFields,
|
||||
total_bookings_fetched: bookings.length,
|
||||
matching_bookings: siteBookings.map(b => {
|
||||
const out = {}
|
||||
for (const f of ['booking_id', 'booking_status', 'booking_arrival', 'booking_departure', ...idFields]) {
|
||||
out[f] = b[f]
|
||||
}
|
||||
return out
|
||||
}),
|
||||
classified: {
|
||||
flow_type: classified.flow_type,
|
||||
spans_previous: classified.spans_previous,
|
||||
spans_next: classified.spans_next,
|
||||
previous_status: classified.previous_status,
|
||||
next_status: classified.next_status,
|
||||
booking_id: classified.booking?.booking_id,
|
||||
booking_arrival: classified.booking?.booking_arrival,
|
||||
booking_departure: classified.booking?.booking_departure,
|
||||
next_booking_id: classified.next_booking?.booking_id,
|
||||
next_booking_arrival: classified.next_booking?.booking_arrival,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// GET /api/rooms?date=YYYY-MM-DD
|
||||
app.get('/api/rooms', async (req, reply) => {
|
||||
if (!hasCap(req, 'view')) return reply.status(403).send({ error: 'Missing capability: view' })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue