Workforce: fetch locations individually by ID instead of list endpoint

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 788628485b
commit da0f97c47c
2 changed files with 15 additions and 5 deletions

View file

@ -1,6 +1,6 @@
import { pool } from '../db.js' import { pool } from '../db.js'
import { hashPassword, verifyToken } from '../jwt.js' import { hashPassword, verifyToken } from '../jwt.js'
import { getAllDepartments, getAllLocations, getSyncConfig, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' import { getAllDepartments, getLocationsByIds, getSyncConfig, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js'
import { syncAllWorkforceUsers } from '../sync.js' import { syncAllWorkforceUsers } from '../sync.js'
async function requireAdmin(request, reply) { async function requireAdmin(request, reply) {
@ -304,14 +304,16 @@ export async function adminRoutes(app) {
// ── Workforce department mappings ────────────────────────────────────────── // ── Workforce department mappings ──────────────────────────────────────────
app.get('/workforce/departments', async (request, reply) => { app.get('/workforce/departments', async (request, reply) => {
let depts, locations, syncConfig let depts, syncConfig
try { try {
;[depts, locations, syncConfig] = await Promise.all([getAllDepartments(), getAllLocations(), getSyncConfig()]) ;[depts, syncConfig] = await Promise.all([getAllDepartments(), getSyncConfig()])
} catch (err) { } catch (err) {
request.log.error({ err }, 'Workforce departments fetch failed') request.log.error({ err }, 'Workforce departments fetch failed')
return reply.status(502).send({ error: `Workforce API error: ${err.message}` }) return reply.status(502).send({ error: `Workforce API error: ${err.message}` })
} }
const locationIds = [...new Set(depts.map(d => d.location_id).filter(Boolean).map(String))]
const locations = await getLocationsByIds(locationIds)
const locationMap = Object.fromEntries(locations.map(l => [l.id, l.name])) const locationMap = Object.fromEntries(locations.map(l => [l.id, l.name]))
if (syncConfig.locationId) { if (syncConfig.locationId) {
depts = depts.filter(d => String(d.location_id) === syncConfig.locationId) depts = depts.filter(d => String(d.location_id) === syncConfig.locationId)

View file

@ -59,8 +59,16 @@ export async function findEmployeeById(wfUserId) {
} }
} }
export async function getAllLocations() { export async function getLocationsByIds(ids) {
return wfFetchPaged('/api/v2/locations') const results = await Promise.all(ids.map(async id => {
try {
const data = await wfFetch(`/api/v2/locations/${id}`)
return { id: String(id), name: data.name ?? `Location ${id}`, short_name: data.short_name ?? null }
} catch {
return { id: String(id), name: `Location ${id}`, short_name: null }
}
}))
return results
} }
export async function getAllDepartments() { export async function getAllDepartments() {