mirror of
https://github.com/ershisan99/www.git
synced 2025-12-17 05:19:23 +00:00
refactor refresh-history-by-date route to handle missing request body with default date range
This commit is contained in:
@@ -13,10 +13,40 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse request body to get date range parameters
|
// Check if request has a body
|
||||||
const body = await request.json().catch((e) => console.log(e))
|
let startDate: string | undefined
|
||||||
const startDate = body.start_date
|
let endDate: string | undefined
|
||||||
const endDate = body.end_date
|
|
||||||
|
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 {
|
try {
|
||||||
console.log('refreshing history by date range...')
|
console.log('refreshing history by date range...')
|
||||||
|
|||||||
Reference in New Issue
Block a user