修正競賽團隊編輯、個人賽顯示、團體賽顯示 bug
This commit is contained in:
@@ -10,20 +10,48 @@ export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const competitions = await CompetitionService.getAllCompetitions();
|
||||
|
||||
// 動態計算每個競賽的狀態
|
||||
const now = new Date();
|
||||
const competitionsWithCalculatedStatus = competitions.map(competition => {
|
||||
const startDate = new Date(competition.start_date);
|
||||
const endDate = new Date(competition.end_date);
|
||||
|
||||
let calculatedStatus = competition.status;
|
||||
|
||||
// 確保日期比較的準確性,使用 UTC 時間避免時區問題
|
||||
const nowUTC = new Date(now.getTime() + now.getTimezoneOffset() * 60000);
|
||||
const startDateUTC = new Date(startDate.getTime() + startDate.getTimezoneOffset() * 60000);
|
||||
const endDateUTC = new Date(endDate.getTime() + endDate.getTimezoneOffset() * 60000);
|
||||
|
||||
// 根據實際日期計算狀態
|
||||
if (nowUTC < startDateUTC) {
|
||||
calculatedStatus = 'upcoming'; // 即將開始
|
||||
} else if (nowUTC >= startDateUTC && nowUTC <= endDateUTC) {
|
||||
calculatedStatus = 'active'; // 進行中
|
||||
} else if (nowUTC > endDateUTC) {
|
||||
calculatedStatus = 'completed'; // 已完成
|
||||
}
|
||||
|
||||
return {
|
||||
...competition,
|
||||
status: calculatedStatus
|
||||
};
|
||||
});
|
||||
|
||||
// 計算統計數據
|
||||
const stats = {
|
||||
total: competitions.length,
|
||||
upcoming: competitions.filter(c => c.status === 'upcoming').length,
|
||||
active: competitions.filter(c => c.status === 'active' || c.status === 'ongoing').length,
|
||||
ongoing: competitions.filter(c => c.status === 'ongoing').length,
|
||||
judging: competitions.filter(c => c.status === 'judging').length,
|
||||
completed: competitions.filter(c => c.status === 'completed').length,
|
||||
individual: competitions.filter(c => c.type === 'individual').length,
|
||||
team: competitions.filter(c => c.type === 'team').length,
|
||||
mixed: competitions.filter(c => c.type === 'mixed').length,
|
||||
proposal: competitions.filter(c => c.type === 'proposal').length,
|
||||
currentYear: competitions.filter(c => c.year === new Date().getFullYear()).length,
|
||||
thisMonth: competitions.filter(c =>
|
||||
total: competitionsWithCalculatedStatus.length,
|
||||
upcoming: competitionsWithCalculatedStatus.filter(c => c.status === 'upcoming').length,
|
||||
active: competitionsWithCalculatedStatus.filter(c => c.status === 'active' || c.status === 'ongoing').length,
|
||||
ongoing: competitionsWithCalculatedStatus.filter(c => c.status === 'ongoing').length,
|
||||
judging: competitionsWithCalculatedStatus.filter(c => c.status === 'judging').length,
|
||||
completed: competitionsWithCalculatedStatus.filter(c => c.status === 'completed').length,
|
||||
individual: competitionsWithCalculatedStatus.filter(c => c.type === 'individual').length,
|
||||
team: competitionsWithCalculatedStatus.filter(c => c.type === 'team').length,
|
||||
mixed: competitionsWithCalculatedStatus.filter(c => c.type === 'mixed').length,
|
||||
proposal: competitionsWithCalculatedStatus.filter(c => c.type === 'proposal').length,
|
||||
currentYear: competitionsWithCalculatedStatus.filter(c => c.year === new Date().getFullYear()).length,
|
||||
thisMonth: competitionsWithCalculatedStatus.filter(c =>
|
||||
c.year === new Date().getFullYear() &&
|
||||
c.month === new Date().getMonth() + 1
|
||||
).length
|
||||
|
Reference in New Issue
Block a user