Batch net-sales requests in ≤365-day chunks — forecasting API 422s at 396 days
This commit is contained in:
parent
92400dbcec
commit
c6166f31d0
1 changed files with 25 additions and 12 deletions
|
|
@ -27,10 +27,20 @@ export async function netSalesRoutes(fastify) {
|
|||
const { from, to } = request.query
|
||||
if (!from || !to) return reply.status(400).send({ error: 'from and to required' })
|
||||
|
||||
const days = daysBetween(from, to)
|
||||
const data = await fcFetch(`/forecast/revenue?start_date=${from}&days=${days}&type=all&dow_align=true`)
|
||||
const MAX_DAYS = 365
|
||||
const totalDays = daysBetween(from, to)
|
||||
const allDays = []
|
||||
|
||||
const result = (data?.data ?? []).map(d => ({
|
||||
// Batch into ≤365-day chunks if range exceeds API limit
|
||||
let batchStart = new Date(from + 'T00:00:00')
|
||||
const end = new Date(to + 'T00:00:00')
|
||||
while (batchStart <= end) {
|
||||
const remaining = Math.ceil((end - batchStart) / 86_400_000) + 1
|
||||
const batchDays = Math.min(remaining, MAX_DAYS)
|
||||
const startStr = batchStart.toISOString().slice(0, 10)
|
||||
const data = await fcFetch(`/forecast/revenue?start_date=${startStr}&days=${batchDays}&type=all&dow_align=true`)
|
||||
for (const d of (data?.data ?? [])) {
|
||||
allDays.push({
|
||||
date: d.date,
|
||||
net_sales: parseFloat(d.total?.otb ?? 0),
|
||||
py_sales: parseFloat(d.total?.prior_final ?? 0),
|
||||
|
|
@ -38,8 +48,11 @@ export async function netSalesRoutes(fastify) {
|
|||
dry: parseFloat(d.dry?.otb ?? 0),
|
||||
wet: parseFloat(d.wet?.otb ?? 0),
|
||||
is_past: d.is_past ?? true,
|
||||
}))
|
||||
})
|
||||
}
|
||||
batchStart.setDate(batchStart.getDate() + batchDays)
|
||||
}
|
||||
|
||||
return { days: result }
|
||||
return { days: allDays }
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue