From 536bcd3340a68b0960eb7b03f54f881fb267ec75 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Sat, 4 Jul 2026 13:13:13 +0000 Subject: [PATCH] Fix 500: fetch category order from settings service, not direct DB query global_config lives in settings_db, not room_planner_db. Replace the broken cross-DB pool query with an HTTP call to the new internal settings endpoint. Falls back to NewBook order gracefully if settings service is unavailable or rooms haven't been synced yet. Co-Authored-By: Claude Sonnet 4.6 --- backend/src/routes/rooms.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/rooms.js b/backend/src/routes/rooms.js index 85b5735..3c9037d 100644 --- a/backend/src/routes/rooms.js +++ b/backend/src/routes/rooms.js @@ -3,6 +3,24 @@ import { pool } from '../db.js' import { fetchSites, fetchBookings, fetchTasks } from '../lib/newbook.js' import { classifyRoom, toDateStr } from '../lib/booking-flow.js' +// Fetch the newbook.rooms config from the settings service (different DB). +// Returns the parsed value object, or null if unavailable (settings not deployed, not synced yet, etc.). +async function fetchSettingsRoomsConfig() { + const url = process.env.SETTINGS_URL + const secret = process.env.SETTINGS_SECRET + if (!url || !secret) return null + try { + const res = await fetch(`${url}/api/internal/global-config/newbook.rooms`, { + headers: { Authorization: `Bearer ${secret}` }, + }) + if (!res.ok) return null + const body = await res.json() + return body.value ?? null + } catch { + return null + } +} + function dateOffset(dateStr, days) { const d = new Date(dateStr + 'T00:00:00Z') d.setUTCDate(d.getUTCDate() + days) @@ -135,11 +153,11 @@ export async function roomRoutes(app) { const canSeeRate = hasCap(req, 'rate_details') const canSeeAllNotes = hasCap(req, 'view_all_notes') - // Load config in parallel: note visibility, exclusions, settings category order - const [cfgRow, excRow, settingsRow] = await Promise.all([ + // Load local config + settings category order in parallel + const [cfgRow, excRow, settingsRooms] = await Promise.all([ pool.query(`SELECT value FROM config WHERE key = 'visible_note_types'`), pool.query(`SELECT key, value FROM config WHERE key IN ('excluded_categories','hide_excluded_categories')`), - pool.query(`SELECT value FROM global_config WHERE key = 'newbook.rooms'`), + fetchSettingsRoomsConfig(), ]) const visibleNoteTypes = cfgRow.rows[0]?.value || [] const cfgMap = Object.fromEntries(excRow.rows.map(r => [r.key, r.value])) @@ -148,7 +166,7 @@ export async function roomRoutes(app) { // Build category sort-order map from settings (admin-configured) — keyed by category id const settingsCatOrder = {} - for (const cat of settingsRow.rows[0]?.value?.categories ?? []) { + for (const cat of settingsRooms?.categories ?? []) { settingsCatOrder[String(cat.id)] = cat.sort_order ?? 999 }