Files
MAD-Platform/app/api/trajets/route.ts

160 lines
4.3 KiB
TypeScript
Raw Normal View History

2026-01-21 17:34:48 +01:00
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getCurrentUser } from '@/lib/auth';
2026-02-08 14:16:55 +01:00
import { createNotificationForAllUsers } from '@/lib/notifications';
2026-01-21 17:34:48 +01:00
// 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,
2026-01-22 19:25:25 +01:00
telephoneSecondaire: true,
2026-01-21 17:34:48 +01:00
email: true,
2026-01-22 19:25:25 +01:00
forfait: true,
2026-01-21 17:34:48 +01:00
},
},
chauffeur: {
select: {
id: true,
nom: true,
prenom: true,
telephone: true,
},
},
2026-02-16 14:43:02 +01:00
participations: {
select: { id: true },
},
2026-01-21 17:34:48 +01:00
},
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();
2026-01-22 19:25:25 +01:00
const { date, adresseDepart, adresseArrivee, commentaire, instructions, statut, adherentId, chauffeurId } = body;
2026-01-21 17:34:48 +01:00
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,
2026-01-22 19:25:25 +01:00
instructions: instructions || null,
2026-01-21 17:34:48 +01:00
statut: statut || 'Planifié',
adherentId,
chauffeurId: chauffeurId || null,
},
include: {
adherent: {
select: {
id: true,
nom: true,
prenom: true,
telephone: true,
2026-01-22 19:25:25 +01:00
telephoneSecondaire: true,
2026-01-21 17:34:48 +01:00
email: true,
2026-01-22 19:25:25 +01:00
forfait: true,
2026-01-21 17:34:48 +01:00
},
},
chauffeur: {
select: {
id: true,
nom: true,
prenom: true,
telephone: true,
},
},
},
});
2026-02-08 14:16:55 +01:00
// 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
);
2026-01-21 17:34:48 +01:00
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 }
);
}
}