Workforce: filter departments by configured location_id
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
9ea567955d
commit
3fae52c36b
2 changed files with 10 additions and 4 deletions
|
|
@ -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]))
|
||||
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue