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:
Julien Froidefond
2025-12-10 05:45:25 +01:00
parent fb830c6fcc
commit a69613a232
15 changed files with 167 additions and 298 deletions

View File

@@ -1,7 +1,7 @@
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";
export async function PUT(
request: Request,
@@ -16,7 +16,8 @@ export async function PUT(
const { id } = await params;
const body = await request.json();
const { date, name, description, type, status, room, time, maxPlaces } = body;
const { date, name, description, type, room, time, maxPlaces } = body;
// Le statut est ignoré s'il est fourni, il sera calculé automatiquement
// Vérifier que l'événement existe
const existingEvent = await prisma.event.findUnique({
@@ -35,7 +36,6 @@ export async function PUT(
name?: string;
description?: string;
type?: EventType;
status?: EventStatus;
room?: string | null;
time?: string | null;
maxPlaces?: number | null;
@@ -62,18 +62,11 @@ export async function PUT(
}
updateData.type = type as EventType;
}
if (status !== undefined) {
if (!Object.values(EventStatus).includes(status)) {
return NextResponse.json(
{ error: "Statut d'événement invalide" },
{ status: 400 }
);
}
updateData.status = status as EventStatus;
}
// Le statut est toujours calculé automatiquement, on ignore s'il est fourni
if (room !== undefined) updateData.room = room || null;
if (time !== undefined) updateData.time = time || null;
if (maxPlaces !== undefined) updateData.maxPlaces = maxPlaces ? parseInt(maxPlaces) : null;
if (maxPlaces !== undefined)
updateData.maxPlaces = maxPlaces ? parseInt(maxPlaces) : null;
const event = await prisma.event.update({
where: { id },
@@ -128,4 +121,3 @@ export async function DELETE(
);
}
}

View File

@@ -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) {
);
}
}