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,19 +27,32 @@ export async function netSalesRoutes(fastify) {
|
||||||
const { from, to } = request.query
|
const { from, to } = request.query
|
||||||
if (!from || !to) return reply.status(400).send({ error: 'from and to required' })
|
if (!from || !to) return reply.status(400).send({ error: 'from and to required' })
|
||||||
|
|
||||||
const days = daysBetween(from, to)
|
const MAX_DAYS = 365
|
||||||
const data = await fcFetch(`/forecast/revenue?start_date=${from}&days=${days}&type=all&dow_align=true`)
|
const totalDays = daysBetween(from, to)
|
||||||
|
const allDays = []
|
||||||
|
|
||||||
const result = (data?.data ?? []).map(d => ({
|
// Batch into ≤365-day chunks if range exceeds API limit
|
||||||
date: d.date,
|
let batchStart = new Date(from + 'T00:00:00')
|
||||||
net_sales: parseFloat(d.total?.otb ?? 0),
|
const end = new Date(to + 'T00:00:00')
|
||||||
py_sales: parseFloat(d.total?.prior_final ?? 0),
|
while (batchStart <= end) {
|
||||||
accom: parseFloat(d.accom?.otb ?? 0),
|
const remaining = Math.ceil((end - batchStart) / 86_400_000) + 1
|
||||||
dry: parseFloat(d.dry?.otb ?? 0),
|
const batchDays = Math.min(remaining, MAX_DAYS)
|
||||||
wet: parseFloat(d.wet?.otb ?? 0),
|
const startStr = batchStart.toISOString().slice(0, 10)
|
||||||
is_past: d.is_past ?? true,
|
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),
|
||||||
|
accom: parseFloat(d.accom?.otb ?? 0),
|
||||||
|
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