Workforce locations: fetch each location by ID for name, fall back gracefully

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-07 13:14:11 +00:00
parent c862c1f9e3
commit b70491dea2

View file

@ -120,7 +120,7 @@ export async function integrationRoutes(app) {
if (!creds.bearer_token) return reply.status(400).send({ error: 'Workforce bearer token not configured' }) if (!creds.bearer_token) return reply.status(400).send({ error: 'Workforce bearer token not configured' })
const baseUrl = creds.base_url || 'https://my.tanda.co' const baseUrl = creds.base_url || 'https://my.tanda.co'
// /api/v2/locations may be locked by billing tier — derive from teams instead // /api/v2/locations list may be billing-locked — get unique IDs from teams then fetch each individually
const teams = [] const teams = []
let page = 1 let page = 1
while (true) { while (true) {
@ -140,20 +140,24 @@ export async function integrationRoutes(app) {
page++ page++
} }
// Group team names by location_id const locationIds = [...new Set(teams.map(t => t.location_id).filter(Boolean).map(String))]
const locMap = {}
for (const t of teams) {
if (!t.location_id) continue
const id = String(t.location_id)
if (!locMap[id]) locMap[id] = []
locMap[id].push(t.name)
}
return Object.entries(locMap).map(([id, teamNames]) => ({ // Fetch each location individually in parallel
id, const locations = await Promise.all(locationIds.map(async id => {
name: `Location ${id}`, try {
team_names: teamNames.sort(), const res = await fetch(`${baseUrl}/api/v2/locations/${id}`, {
headers: { Authorization: `Bearer ${creds.bearer_token}` },
signal: AbortSignal.timeout(8000),
})
if (!res.ok) return { id, name: `Location ${id}`, short_name: null }
const loc = await res.json()
return { id, name: loc.name ?? `Location ${id}`, short_name: loc.short_name ?? null }
} catch {
return { id, name: `Location ${id}`, short_name: null }
}
})) }))
return locations.sort((a, b) => a.name.localeCompare(b.name))
}) })
app.post('/integrations/newbook/sync-rooms', { preHandler: requireAdmin }, async (req, reply) => { app.post('/integrations/newbook/sync-rooms', { preHandler: requireAdmin }, async (req, reply) => {