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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-22 14:22:32 +00:00
parent 09a8158a1e
commit 69720b778a

View file

@ -35,7 +35,27 @@ export async function workforceRoutes(app) {
const to = fmtDate(toDate) const to = fmtDate(toDate)
try { 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 // Pivot per-member → per-date
const byDate = {} const byDate = {}