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 }
);
}
}