Fix rolling sync: batch actuals in 7-day chunks (API limit 31 days)
This commit is contained in:
parent
46201c6592
commit
4d6e53a1f8
1 changed files with 20 additions and 10 deletions
|
|
@ -178,19 +178,29 @@ export async function syncScheduled(from, to) {
|
||||||
|
|
||||||
export async function runRollingSync() {
|
export async function runRollingSync() {
|
||||||
const today = new Date()
|
const today = new Date()
|
||||||
const to = today.toISOString().slice(0, 10)
|
const todayStr = today.toISOString().slice(0, 10)
|
||||||
const fromDate = new Date(today)
|
|
||||||
fromDate.setDate(fromDate.getDate() - 35)
|
|
||||||
const from = fromDate.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)
|
const fwdDate = new Date(today)
|
||||||
fwdDate.setDate(fwdDate.getDate() + 14)
|
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 }
|
return { actualRows, scheduledRows }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue