Workforce: fetch locations to resolve location names on departments
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
5d6a9ea3cd
commit
9ea567955d
2 changed files with 12 additions and 6 deletions
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue