Refactor event status handling: Remove EventStatus enum from the Prisma schema and update related API routes and UI components to calculate event status dynamically based on event date. This change simplifies event management and enhances data integrity by ensuring status is always derived from the date.
This commit is contained in:
28
lib/eventStatus.ts
Normal file
28
lib/eventStatus.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Calcule le statut d'un événement en fonction de sa date
|
||||
* @param eventDate La date de l'événement
|
||||
* @returns Le statut calculé (PAST, LIVE, ou UPCOMING)
|
||||
*/
|
||||
export function calculateEventStatus(
|
||||
eventDate: Date | string
|
||||
): "UPCOMING" | "LIVE" | "PAST" {
|
||||
const date = typeof eventDate === "string" ? new Date(eventDate) : eventDate;
|
||||
const now = new Date();
|
||||
|
||||
// Normaliser les dates à minuit UTC pour comparer uniquement les jours
|
||||
const eventDateOnly = new Date(
|
||||
Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
|
||||
);
|
||||
|
||||
const todayOnly = new Date(
|
||||
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())
|
||||
);
|
||||
|
||||
if (eventDateOnly < todayOnly) {
|
||||
return "PAST";
|
||||
} else if (eventDateOnly.getTime() === todayOnly.getTime()) {
|
||||
return "LIVE";
|
||||
} else {
|
||||
return "UPCOMING";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user