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:
parent
fbe913c01c
commit
536bcd3340
1 changed files with 22 additions and 4 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue