From 3fae52c36b0d762b9ff4f2f9372b0e1300433083 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Tue, 7 Jul 2026 12:52:34 +0000 Subject: [PATCH] Workforce: filter departments by configured location_id Co-Authored-By: Claude Sonnet 4.6 --- src/routes/admin.js | 9 ++++++--- src/workforce.js | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/routes/admin.js b/src/routes/admin.js index b49ed32..4f016c9 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, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' +import { getAllDepartments, getAllLocations, getSyncConfig, findEmployeeByEmail, getEmployeeDepartments } from '../workforce.js' import { syncAllWorkforceUsers } from '../sync.js' async function requireAdmin(request, reply) { @@ -300,15 +300,18 @@ export async function adminRoutes(app) { // ── Workforce department mappings ────────────────────────────────────────── app.get('/workforce/departments', async (request, reply) => { - let depts, locations + let depts, locations, syncConfig try { - ;[depts, locations] = await Promise.all([getAllDepartments(), getAllLocations()]) + ;[depts, locations, syncConfig] = await Promise.all([getAllDepartments(), getAllLocations(), getSyncConfig()]) } 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])) + 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 mappingMap = Object.fromEntries(mappings.map(m => [m.department_id, m.role_id])) diff --git a/src/workforce.js b/src/workforce.js index 0415cc1..a4271b1 100644 --- a/src/workforce.js +++ b/src/workforce.js @@ -79,7 +79,10 @@ export async function getEmployeeDepartments(wfUserId) { export async function getSyncConfig() { 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() {