import { NextRequest, NextResponse } from "next/server"; import { getServerSession } from "next-auth/next"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { prisma } from "@/lib/prisma"; export async function DELETE( req: NextRequest, { params }: { params: { id: string } } ) { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); } try { // First, find the event and its associated calendar const event = await prisma.event.findUnique({ where: { id: params.id }, include: { calendar: true }, }); if (!event) { return NextResponse.json( { error: "Événement non trouvé" }, { status: 404 } ); } // Verify that the user owns the calendar if (event.calendar.userId !== session.user.id) { return NextResponse.json( { error: "Non autorisé" }, { status: 403 } ); } // Delete the event await prisma.event.delete({ where: { id: params.id }, }); return new NextResponse(null, { status: 204 }); } catch (error) { console.error("Erreur lors de la suppression de l'événement:", error); return NextResponse.json( { error: "Erreur serveur" }, { status: 500 } ); } }