import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { getCurrentUser } from '@/lib/auth'; import { createNotificationForAllUsers } from '@/lib/notifications'; // GET - Liste tous les trajets avec leurs relations export async function GET(request: NextRequest) { try { const user = await getCurrentUser(); if (!user) { return NextResponse.json({ error: 'Non autorisé' }, { status: 401 }); } const searchParams = request.nextUrl.searchParams; const limit = searchParams.get('limit'); const startDate = searchParams.get('startDate'); const endDate = searchParams.get('endDate'); const where: any = { archived: false, // Exclure les trajets archivés par défaut }; // Filtrer par date si fourni if (startDate || endDate) { where.date = {}; if (startDate) { where.date.gte = new Date(startDate); } if (endDate) { where.date.lte = new Date(endDate); } } // Si limit est fourni sans filtre de date, trier par date de création (derniers créés) // Sinon, trier par date du trajet (pour le calendrier) const orderBy = limit && !startDate && !endDate ? { createdAt: 'desc' as const } : { date: 'asc' as const }; const trajets = await prisma.trajet.findMany({ where, 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, }, }, participations: { select: { id: true }, }, }, orderBy, take: limit ? parseInt(limit) : undefined, }); return NextResponse.json(trajets); } catch (error) { console.error('Erreur lors de la récupération des trajets:', error); return NextResponse.json( { error: 'Erreur serveur' }, { status: 500 } ); } } // POST - Créer un nouveau trajet export async function POST(request: NextRequest) { 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; if (!date || !adresseDepart || !adresseArrivee || !adherentId) { return NextResponse.json( { error: 'Les champs date, adresse de départ, adresse d\'arrivée et adhérent sont requis' }, { status: 400 } ); } const trajet = await prisma.trajet.create({ data: { date: new Date(date), adresseDepart, adresseArrivee, commentaire: commentaire || null, instructions: instructions || null, statut: statut || 'Planifié', adherentId, chauffeurId: chauffeurId || 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 pour tous les utilisateurs sauf celui qui a créé le trajet const dateFormatted = new Date(date).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit', }); await createNotificationForAllUsers( { type: 'trajet_created', title: 'Nouveau trajet programmé', message: `Trajet programmé pour ${trajet.adherent.prenom} ${trajet.adherent.nom} le ${dateFormatted}`, link: '/dashboard/calendrier', }, user.id ); return NextResponse.json(trajet, { status: 201 }); } catch (error) { console.error('Erreur lors de la création du trajet:', error); return NextResponse.json( { error: 'Erreur serveur' }, { status: 500 } ); } }