From 3f6d2cee1ad71340bb4a04e9964d95f38adf16b1 Mon Sep 17 00:00:00 2001 From: Andres Date: Tue, 15 Jul 2025 12:57:48 +0200 Subject: [PATCH] refactor refresh-history-by-date route to handle missing request body with default date range --- src/app/api/refresh-history-by-date/route.ts | 38 +++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/app/api/refresh-history-by-date/route.ts b/src/app/api/refresh-history-by-date/route.ts index 8cea12c..8125c04 100644 --- a/src/app/api/refresh-history-by-date/route.ts +++ b/src/app/api/refresh-history-by-date/route.ts @@ -13,10 +13,40 @@ export async function POST(request: Request) { } try { - // Parse request body to get date range parameters - const body = await request.json().catch((e) => console.log(e)) - const startDate = body.start_date - const endDate = body.end_date + // Check if request has a body + let startDate: string | undefined + let endDate: string | undefined + + try { + // Try to parse request body if it exists + const body = await request.json().catch(() => null) + if (body) { + startDate = body.start_date + endDate = body.end_date + } + } catch (e) { + // If no body or parsing fails, we'll use default dates + console.log('No request body or empty body, using default date (yesterday)') + } + + // If no dates provided, default to a 3-day range (yesterday, today, and tomorrow) + if (!startDate && !endDate) { + const today = new Date() + + // Calculate yesterday + const yesterday = new Date(today) + yesterday.setDate(yesterday.getDate() - 1) + + // Calculate tomorrow + const tomorrow = new Date(today) + tomorrow.setDate(tomorrow.getDate() + 1) + + // Format dates as YYYY-MM-DD + startDate = yesterday.toISOString().split('T')[0] + endDate = tomorrow.toISOString().split('T')[0] + + console.log(`No dates provided, defaulting to 3-day range: ${startDate} to ${endDate}`) + } try { console.log('refreshing history by date range...')