fix: default daily card to next available recommendations
This commit is contained in:
@@ -17,9 +17,47 @@ function getTaipeiDate(): string {
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
async function resolveDefaultDate(today: string): Promise<string> {
|
||||
try {
|
||||
const calendarResponse = await fetch(
|
||||
`${ANALYTICS_BACKEND}/analytics/daily-card-calendar?start_date=${encodeURIComponent(today)}&days=14`,
|
||||
{
|
||||
method: 'GET',
|
||||
cache: 'no-store',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!calendarResponse.ok) {
|
||||
return today;
|
||||
}
|
||||
|
||||
const calendar = (await calendarResponse.json()) as {
|
||||
dates?: Array<{
|
||||
date?: string;
|
||||
recommendation_count?: number;
|
||||
market_data_status?: string;
|
||||
}>;
|
||||
};
|
||||
const resolved = calendar.dates?.find((entry) => {
|
||||
const recommendationCount = Number(entry.recommendation_count || 0);
|
||||
return Boolean(entry.date)
|
||||
&& recommendationCount > 0
|
||||
&& entry.market_data_status !== 'snapshot_missing_after_kickoff';
|
||||
});
|
||||
|
||||
return resolved?.date || today;
|
||||
} catch {
|
||||
return today;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const targetDate = searchParams.get('date') || getTaipeiDate();
|
||||
const requestedDate = searchParams.get('date');
|
||||
const targetDate = requestedDate || await resolveDefaultDate(getTaipeiDate());
|
||||
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(targetDate)) {
|
||||
return NextResponse.json({ message: '日期格式必須為 YYYY-MM-DD' }, { status: 400 });
|
||||
@@ -42,6 +80,7 @@ export async function GET(request: Request) {
|
||||
const data = (await response.json()) as Record<string, unknown>;
|
||||
return NextResponse.json({
|
||||
generated_at: new Date().toISOString(),
|
||||
requested_date: requestedDate || null,
|
||||
target_date: targetDate,
|
||||
...data,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user