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:
parent
788628485b
commit
da0f97c47c
2 changed files with 15 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue