diff --git a/src/routes/admin.js b/src/routes/admin.js index a0e5bcd..b49ed32 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, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' +import { getAllDepartments, getAllLocations, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' import { syncAllWorkforceUsers } from '../sync.js' async function requireAdmin(request, reply) { @@ -300,13 +300,15 @@ export async function adminRoutes(app) { // ── Workforce department mappings ────────────────────────────────────────── app.get('/workforce/departments', async (request, reply) => { - let depts - try { depts = await getAllDepartments() } - catch (err) { + let depts, locations + try { + ;[depts, locations] = await Promise.all([getAllDepartments(), getAllLocations()]) + } catch (err) { request.log.error({ err }, 'Workforce departments fetch failed') return reply.status(502).send({ error: `Workforce API error: ${err.message}` }) } + const locationMap = Object.fromEntries(locations.map(l => [l.id, l.name])) const { rows: mappings } = await pool.query('SELECT department_id, role_id FROM workforce_department_roles') const mappingMap = Object.fromEntries(mappings.map(m => [m.department_id, m.role_id])) @@ -316,7 +318,7 @@ export async function adminRoutes(app) { name: d.name, staff_count: (d.staff ?? []).length, location_id: d.location_id ? String(d.location_id) : null, - location_name: d.location ?? d.location_name ?? null, + location_name: d.location_id ? (locationMap[d.location_id] ?? null) : null, mapped_role_id: mappingMap[String(d.id)] ?? null, })) .sort((a, b) => { diff --git a/src/workforce.js b/src/workforce.js index 3442019..0415cc1 100644 --- a/src/workforce.js +++ b/src/workforce.js @@ -36,7 +36,7 @@ async function wfFetchPaged(path) { while (true) { const sep = path.includes('?') ? '&' : '?' const data = await wfFetch(`${path}${sep}page=${page}&page_size=100`) - const items = Array.isArray(data) ? data : (data.users ?? data.departments ?? data.teams ?? []) + const items = Array.isArray(data) ? data : (data.users ?? data.departments ?? data.teams ?? data.locations ?? []) results.push(...items) if (items.length < 100) break page++ @@ -59,6 +59,10 @@ export async function findEmployeeById(wfUserId) { } } +export async function getAllLocations() { + return wfFetchPaged('/api/v2/locations') +} + export async function getAllDepartments() { return wfFetchPaged('/api/v2/departments') }