From d25e3421a8225186031874cdea54a214bb1b34c4 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Thu, 2 Jul 2026 20:51:09 +0000 Subject: [PATCH] Sort rooms by Newbook category order, numeric room name collation Co-Authored-By: Claude Sonnet 4.6 --- backend/src/lib/newbook.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/backend/src/lib/newbook.js b/backend/src/lib/newbook.js index efd94a4..9f3d575 100644 --- a/backend/src/lib/newbook.js +++ b/backend/src/lib/newbook.js @@ -167,22 +167,22 @@ export async function fetchGridData(startDate, days, settings, forceRefresh = fa const dates = [] for (let i = 0; i < days; i++) dates.push(offsetDate(startDate, i)) - const grid = {} - const rooms = [] + const grid = {} + const rooms = [] + const categoryOrder = [] for (const booking of resp.data) { const siteId = booking.site_id || '' const siteName = booking.site_name || '' if (!siteId || !siteName) continue + const category = (booking.category_name || 'Uncategorized').trim() + if (!grid[siteId]) { - grid[siteId] = { - site_name: siteName, - category: (booking.category_name || 'Uncategorized').trim(), - cells: {}, - } + grid[siteId] = { site_name: siteName, category, cells: {} } rooms.push(siteId) } + if (!categoryOrder.includes(category)) categoryOrder.push(category) const checkin = (booking.booking_arrival || '').slice(0, 10) const checkout = (booking.booking_departure || '').slice(0, 10) @@ -207,11 +207,13 @@ export async function fetchGridData(startDate, days, settings, forceRefresh = fa } } - // Sort by category then room name + // Sort by Newbook's natural category order, then room name numerically + const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }) rooms.sort((a, b) => { - const ac = grid[a].category, bc = grid[b].category - if (ac !== bc) return ac.localeCompare(bc) - return grid[a].site_name.localeCompare(grid[b].site_name) + const ai = categoryOrder.indexOf(grid[a].category) + const bi = categoryOrder.indexOf(grid[b].category) + if (ai !== bi) return ai - bi + return collator.compare(grid[a].site_name, grid[b].site_name) }) const result = { dates, rooms, grid }