From 69720b778ae9abe7583662c68ef6f4f22bc98f79 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Wed, 22 Jul 2026 14:22:32 +0000 Subject: [PATCH] Fix workforce sync: split 35-day window into 7-day API chunks Workforce schedules API enforces a hard 7-day limit per request. Split the rolling window into up to 5 weekly chunks, fetch each sequentially, then merge by staff id before pivoting to per-date rows. Co-Authored-By: Claude Sonnet 4.6 --- backend/src/routes/workforce.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/workforce.js b/backend/src/routes/workforce.js index 70962d1..d43d97f 100644 --- a/backend/src/routes/workforce.js +++ b/backend/src/routes/workforce.js @@ -35,7 +35,27 @@ export async function workforceRoutes(app) { const to = fmtDate(toDate) try { - const staffList = await fetchShifts(from, to, deptIds) + // API limit: max 7 days per request — split window into weekly chunks + const chunks = [] + const cur = new Date(fromDate) + while (cur <= toDate) { + const chunkFrom = fmtDate(cur) + const chunkToDate = new Date(cur) + chunkToDate.setDate(chunkToDate.getDate() + 6) + if (chunkToDate > toDate) chunkToDate.setTime(toDate.getTime()) + chunks.push({ from: chunkFrom, to: fmtDate(chunkToDate) }) + cur.setDate(cur.getDate() + 7) + } + + const staffMap = {} + for (const chunk of chunks) { + const chunkStaff = await fetchShifts(chunk.from, chunk.to, deptIds) + for (const member of chunkStaff) { + if (!staffMap[member.id]) staffMap[member.id] = { id: member.id, name: member.name, days: {} } + Object.assign(staffMap[member.id].days, member.days) + } + } + const staffList = Object.values(staffMap) // Pivot per-member → per-date const byDate = {}