diff --git a/src/routes/admin.js b/src/routes/admin.js index 3ab29ca..3c060bb 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -1,6 +1,6 @@ import { pool } from '../db.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' async function requireAdmin(request, reply) { @@ -304,14 +304,16 @@ export async function adminRoutes(app) { // ── Workforce department mappings ────────────────────────────────────────── app.get('/workforce/departments', async (request, reply) => { - let depts, locations, syncConfig + let depts, syncConfig try { - ;[depts, locations, syncConfig] = await Promise.all([getAllDepartments(), getAllLocations(), getSyncConfig()]) + ;[depts, syncConfig] = await Promise.all([getAllDepartments(), getSyncConfig()]) } catch (err) { request.log.error({ err }, 'Workforce departments fetch failed') 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])) if (syncConfig.locationId) { depts = depts.filter(d => String(d.location_id) === syncConfig.locationId) diff --git a/src/workforce.js b/src/workforce.js index a4271b1..241a27f 100644 --- a/src/workforce.js +++ b/src/workforce.js @@ -59,8 +59,16 @@ export async function findEmployeeById(wfUserId) { } } -export async function getAllLocations() { - return wfFetchPaged('/api/v2/locations') +export async function getLocationsByIds(ids) { + 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() {