import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { getCurrentUser } from '@/lib/auth'; import { createNotificationForAllUsers } from '@/lib/notifications'; import { createParticipationForTrajet } from '@/lib/participation-financiere'; // GET - Récupérer un trajet spécifique export async function GET( request: NextRequest, { params }: { params: { id: string } } ) { try { const user = await getCurrentUser(); if (!user) { return NextResponse.json({ error: 'Non autorisé' }, { status: 401 }); } const trajet = await prisma.trajet.findUnique({ where: { id: params.id }, include: { adherent: { select: { id: true, nom: true, prenom: true, telephone: true, telephoneSecondaire: true, email: true, adresse: true, forfait: true, }, }, chauffeur: { select: { id: true, nom: true, prenom: true, telephone: true, email: true, }, }, participations: { select: { id: true }, }, }, }); if (!trajet) { return NextResponse.json({ error: 'Trajet non trouvé' }, { status: 404 }); } return NextResponse.json(trajet); } catch (error) { console.error('Erreur lors de la récupération du trajet:', error); return NextResponse.json( { error: 'Erreur serveur' }, { status: 500 } ); } } // PUT - Mettre à jour un trajet export async function PUT( request: NextRequest, { params }: { params: { id: string } } ) { try { const user = await getCurrentUser(); if (!user) { return NextResponse.json({ error: 'Non autorisé' }, { status: 401 }); } const body = await request.json(); const { date, adresseDepart, adresseArrivee, commentaire, instructions, statut, adherentId, chauffeurId } = body; const previousTrajet = statut ? await prisma.trajet.findUnique({ where: { id: params.id }, select: { statut: true }, }) : null; const trajet = await prisma.trajet.update({ where: { id: params.id }, data: { ...(date && { date: new Date(date) }), ...(adresseDepart && { adresseDepart }), ...(adresseArrivee && { adresseArrivee }), ...(commentaire !== undefined && { commentaire }), ...(instructions !== undefined && { instructions }), ...(statut && { statut }), ...(adherentId && { adherentId }), ...(chauffeurId !== undefined && { chauffeurId }), ...(body.universProId !== undefined && { universProId: body.universProId || null }), }, include: { adherent: { select: { id: true, nom: true, prenom: true, telephone: true, telephoneSecondaire: true, email: true, forfait: true, }, }, chauffeur: { select: { id: true, nom: true, prenom: true, telephone: true, }, }, }, }); // Créer une notification si le statut a changé if (statut && previousTrajet && previousTrajet.statut !== statut) { const dateFormatted = new Date(trajet.date).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit', }); let notificationType: 'trajet_cancelled' | 'trajet_completed' | null = null; let notificationTitle = ''; let notificationMessage = ''; if (statut === 'Annulé') { notificationType = 'trajet_cancelled'; notificationTitle = 'Trajet annulé'; notificationMessage = `Le trajet pour ${trajet.adherent.prenom} ${trajet.adherent.nom} du ${dateFormatted} a été annulé`; } else if (statut === 'Terminé') { notificationType = 'trajet_completed'; notificationTitle = 'Trajet terminé'; notificationMessage = `Le trajet pour ${trajet.adherent.prenom} ${trajet.adherent.nom} du ${dateFormatted} est terminé`; } if (notificationType) { await createNotificationForAllUsers( { type: notificationType, title: notificationTitle, message: notificationMessage, link: '/dashboard/calendrier', }, user.id ); } // Créer la participation financière quand le trajet est terminé ou validé if (statut === 'Terminé' || statut === 'Validé') { try { await createParticipationForTrajet(params.id); } catch (err) { console.error('Erreur création participation:', err); } } } return NextResponse.json(trajet); } catch (error) { console.error('Erreur lors de la mise à jour du trajet:', error); return NextResponse.json( { error: 'Erreur serveur' }, { status: 500 } ); } } // DELETE - Supprimer un trajet export async function DELETE( request: NextRequest, { params }: { params: { id: string } } ) { try { const user = await getCurrentUser(); if (!user) { return NextResponse.json({ error: 'Non autorisé' }, { status: 401 }); } await prisma.trajet.delete({ where: { id: params.id }, }); return NextResponse.json({ message: 'Trajet supprimé' }); } catch (error) { console.error('Erreur lors de la suppression du trajet:', error); return NextResponse.json( { error: 'Erreur serveur' }, { status: 500 } ); } }