2026-01-21 17:34:48 +01:00
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
|
import { getCurrentUser } from '@/lib/auth';
|
|
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
|
|
2026-01-21 18:13:35 +01:00
|
|
|
const where: any = {
|
|
|
|
|
archived: false, // Exclure les trajets archivés par défaut
|
|
|
|
|
};
|
2026-01-21 17:34:48 +01:00
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
email: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
chauffeur: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
nom: true,
|
|
|
|
|
prenom: true,
|
|
|
|
|
telephone: 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, 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,
|
|
|
|
|
statut: statut || 'Planifié',
|
|
|
|
|
adherentId,
|
|
|
|
|
chauffeurId: chauffeurId || null,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
adherent: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
nom: true,
|
|
|
|
|
prenom: true,
|
|
|
|
|
telephone: true,
|
|
|
|
|
email: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
chauffeur: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
nom: true,
|
|
|
|
|
prenom: true,
|
|
|
|
|
telephone: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|