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:
@@ -1,7 +1,8 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { Role, EventType, EventStatus } from "@/prisma/generated/prisma/client";
|
||||
import { Role, EventType } from "@/prisma/generated/prisma/client";
|
||||
import { calculateEventStatus } from "@/lib/eventStatus";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
@@ -15,18 +16,7 @@ export async function GET() {
|
||||
orderBy: {
|
||||
date: "desc",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
date: true,
|
||||
name: true,
|
||||
description: true,
|
||||
type: true,
|
||||
status: true,
|
||||
room: true,
|
||||
time: true,
|
||||
maxPlaces: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
registrations: true,
|
||||
@@ -36,13 +26,14 @@ export async function GET() {
|
||||
});
|
||||
|
||||
// Transformer les données pour inclure le nombre d'inscriptions
|
||||
// Le statut est calculé automatiquement en fonction de la date
|
||||
const eventsWithCount = events.map((event) => ({
|
||||
id: event.id,
|
||||
date: event.date.toISOString(),
|
||||
name: event.name,
|
||||
description: event.description,
|
||||
type: event.type,
|
||||
status: event.status,
|
||||
status: calculateEventStatus(event.date),
|
||||
room: event.room,
|
||||
time: event.time,
|
||||
maxPlaces: event.maxPlaces,
|
||||
@@ -70,15 +61,24 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { date, name, description, type, status, room, time, maxPlaces } = body;
|
||||
const { date, name, description, type, room, time, maxPlaces } = body;
|
||||
|
||||
if (!date || !name || !description || !type || !status) {
|
||||
if (!date || !name || !description || !type) {
|
||||
return NextResponse.json(
|
||||
{ error: "Tous les champs sont requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Convertir la date string en Date object
|
||||
const eventDate = new Date(date);
|
||||
if (isNaN(eventDate.getTime())) {
|
||||
return NextResponse.json(
|
||||
{ error: "Format de date invalide" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Valider les enums
|
||||
if (!Object.values(EventType).includes(type)) {
|
||||
return NextResponse.json(
|
||||
@@ -87,20 +87,12 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
if (!Object.values(EventStatus).includes(status)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Statut d'événement invalide" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const event = await prisma.event.create({
|
||||
data: {
|
||||
date: eventDate,
|
||||
name,
|
||||
description,
|
||||
type: type as EventType,
|
||||
status: status as EventStatus,
|
||||
room: room || null,
|
||||
time: time || null,
|
||||
maxPlaces: maxPlaces ? parseInt(maxPlaces) : null,
|
||||
@@ -116,4 +108,3 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user