Files
MAD-Platform/app/api/trajets/[id]/route.ts

192 lines
5.2 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 - 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,
2026-01-22 19:25:25 +01:00
telephoneSecondaire: true,
2026-01-21 17:34:48 +01:00
email: true,
adresse: 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,
email: 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();
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
const trajet = await prisma.trajet.update({
where: { id: params.id },
data: {
...(date && { date: new Date(date) }),
...(adresseDepart && { adresseDepart }),
...(adresseArrivee && { adresseArrivee }),
...(commentaire !== undefined && { commentaire }),
2026-01-22 19:25:25 +01:00
...(instructions !== undefined && { instructions }),
2026-01-21 17:34:48 +01:00
...(statut && { statut }),
...(adherentId && { adherentId }),
...(chauffeurId !== undefined && { chauffeurId }),
},
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 si le statut a changé
if (statut) {
const oldTrajet = await prisma.trajet.findUnique({
where: { id: params.id },
include: {
adherent: {
select: {
nom: true,
prenom: true,
},
},
},
});
if (oldTrajet && oldTrajet.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
);
}
}
}
2026-01-21 17:34:48 +01:00
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 }
);
}
}