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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-04 13:13:13 +00:00
parent fbe913c01c
commit 536bcd3340

View file

@ -3,6 +3,24 @@ import { pool } from '../db.js'
import { fetchSites, fetchBookings, fetchTasks } from '../lib/newbook.js' import { fetchSites, fetchBookings, fetchTasks } from '../lib/newbook.js'
import { classifyRoom, toDateStr } from '../lib/booking-flow.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) { function dateOffset(dateStr, days) {
const d = new Date(dateStr + 'T00:00:00Z') const d = new Date(dateStr + 'T00:00:00Z')
d.setUTCDate(d.getUTCDate() + days) d.setUTCDate(d.getUTCDate() + days)
@ -135,11 +153,11 @@ export async function roomRoutes(app) {
const canSeeRate = hasCap(req, 'rate_details') const canSeeRate = hasCap(req, 'rate_details')
const canSeeAllNotes = hasCap(req, 'view_all_notes') const canSeeAllNotes = hasCap(req, 'view_all_notes')
// Load config in parallel: note visibility, exclusions, settings category order // Load local config + settings category order in parallel
const [cfgRow, excRow, settingsRow] = await Promise.all([ const [cfgRow, excRow, settingsRooms] = await Promise.all([
pool.query(`SELECT value FROM config WHERE key = 'visible_note_types'`), 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 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 visibleNoteTypes = cfgRow.rows[0]?.value || []
const cfgMap = Object.fromEntries(excRow.rows.map(r => [r.key, r.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 // Build category sort-order map from settings (admin-configured) — keyed by category id
const settingsCatOrder = {} 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 settingsCatOrder[String(cat.id)] = cat.sort_order ?? 999
} }