Added Calendar Page

This commit is contained in:
2026-01-21 17:34:48 +01:00
parent 3a8a6d1576
commit c9f6b53c13
14 changed files with 2188 additions and 9 deletions

View File

@@ -0,0 +1,134 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getCurrentUser } from '@/lib/auth';
// 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,
email: true,
adresse: true,
},
},
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();
const { date, adresseDepart, adresseArrivee, commentaire, statut, adherentId, chauffeurId } = body;
const trajet = await prisma.trajet.update({
where: { id: params.id },
data: {
...(date && { date: new Date(date) }),
...(adresseDepart && { adresseDepart }),
...(adresseArrivee && { adresseArrivee }),
...(commentaire !== undefined && { commentaire }),
...(statut && { statut }),
...(adherentId && { adherentId }),
...(chauffeurId !== undefined && { chauffeurId }),
},
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);
} 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 }
);
}
}

129
app/api/trajets/route.ts Normal file
View File

@@ -0,0 +1,129 @@
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');
const where: any = {};
// 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 }
);
}
}

View File

@@ -0,0 +1,27 @@
import { redirect } from 'next/navigation';
import { getCurrentUser } from '@/lib/auth';
import DashboardLayout from '@/components/DashboardLayout';
import CalendrierPageContent from '@/components/CalendrierPageContent';
export default async function CalendrierPage() {
const user = await getCurrentUser();
if (!user) {
redirect('/login');
}
return (
<DashboardLayout user={user}>
<div className="p-8">
<h1 className="text-3xl font-semibold text-cblack mb-2">
Calendrier
</h1>
<p className="text-sm text-cgray mb-8">
Gestion des trajets et planning des chauffeurs
</p>
<CalendrierPageContent />
</div>
</DashboardLayout>
);
}

View File

@@ -2,6 +2,59 @@
@tailwind components;
@tailwind utilities;
@import 'leaflet/dist/leaflet.css';
/* Styles personnalisés pour Leaflet */
.leaflet-container {
font-family: inherit;
}
.leaflet-popup-content-wrapper {
border-radius: 12px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
.leaflet-popup-tip {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.leaflet-control-zoom {
border: none !important;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important;
border-radius: 8px !important;
overflow: hidden;
}
.leaflet-control-zoom a {
background-color: white !important;
color: #374151 !important;
border: none !important;
width: 36px !important;
height: 36px !important;
line-height: 36px !important;
font-size: 18px !important;
transition: all 0.2s !important;
}
.leaflet-control-zoom a:hover {
background-color: #f3f4f6 !important;
color: #6B46C1 !important;
}
.leaflet-control-zoom-in {
border-bottom: 1px solid #e5e7eb !important;
}
.custom-marker-depart,
.custom-marker-arrivee {
background: transparent !important;
border: none !important;
}
.custom-popup .leaflet-popup-content {
margin: 0 !important;
}
:root {
--background: #ffffff;
--foreground: #171717;