Fix rolling sync: batch actuals in 7-day chunks (API limit 31 days)

This commit is contained in:
jtricerolph 2026-07-23 09:18:15 +00:00
parent 46201c6592
commit 4d6e53a1f8

View file

@ -177,20 +177,30 @@ export async function syncScheduled(from, to) {
}
export async function runRollingSync() {
const today = new Date()
const to = today.toISOString().slice(0, 10)
const fromDate = new Date(today)
fromDate.setDate(fromDate.getDate() - 35)
const from = fromDate.toISOString().slice(0, 10)
const today = new Date()
const todayStr = today.toISOString().slice(0, 10)
// Actuals: 35 days back in 7-day batches (API limit is 31 days per request)
const startDate = new Date(today)
startDate.setDate(startDate.getDate() - 35)
let actualRows = 0
let current = new Date(startDate)
while (current <= today) {
const batchEnd = new Date(current)
batchEnd.setDate(batchEnd.getDate() + 6)
if (batchEnd > today) batchEnd.setTime(today.getTime())
actualRows += await syncActuals(
current.toISOString().slice(0, 10),
batchEnd.toISOString().slice(0, 10)
)
current.setDate(current.getDate() + 7)
}
// Scheduled: next 14 days (well within 31-day limit)
const fwdDate = new Date(today)
fwdDate.setDate(fwdDate.getDate() + 14)
const fwd = fwdDate.toISOString().slice(0, 10)
const scheduledRows = await syncScheduled(todayStr, fwdDate.toISOString().slice(0, 10))
const [actualRows, scheduledRows] = await Promise.all([
syncActuals(from, to),
syncScheduled(to, fwd),
])
return { actualRows, scheduledRows }
}