Workforce: filter departments by configured location_id

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-07 12:52:34 +00:00
parent 9ea567955d
commit 3fae52c36b
2 changed files with 10 additions and 4 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, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' import { getAllDepartments, getAllLocations, 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) {
@ -300,15 +300,18 @@ 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 let depts, locations, syncConfig
try { try {
;[depts, locations] = await Promise.all([getAllDepartments(), getAllLocations()]) ;[depts, locations, syncConfig] = await Promise.all([getAllDepartments(), getAllLocations(), 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 locationMap = Object.fromEntries(locations.map(l => [l.id, l.name])) const locationMap = Object.fromEntries(locations.map(l => [l.id, l.name]))
if (syncConfig.locationId) {
depts = depts.filter(d => String(d.location_id) === syncConfig.locationId)
}
const { rows: mappings } = await pool.query('SELECT department_id, role_id FROM workforce_department_roles') 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])) const mappingMap = Object.fromEntries(mappings.map(m => [m.department_id, m.role_id]))

View file

@ -79,7 +79,10 @@ export async function getEmployeeDepartments(wfUserId) {
export async function getSyncConfig() { export async function getSyncConfig() {
const creds = await getWorkforceCreds() const creds = await getWorkforceCreds()
return { syncHours: parseFloat(creds.sync_hours ?? '6') } return {
syncHours: parseFloat(creds.sync_hours ?? '6'),
locationId: creds.location_id ? String(creds.location_id) : null,
}
} }
export function invalidateCache() { export function invalidateCache() {