Added Participation Page
This commit is contained in:
203
app/api/participations/[id]/route.ts
Normal file
203
app/api/participations/[id]/route.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
import { generateParticipationPDF, getParticipationStoragePath } from '@/lib/participation-pdf';
|
||||
|
||||
// GET - Récupérer une participation
|
||||
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 participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
adherent: true,
|
||||
trajet: {
|
||||
include: {
|
||||
chauffeur: { select: { nom: true, prenom: true } },
|
||||
universPro: { select: { nom: true, prenom: true, nomEntreprise: true, email: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!participation) {
|
||||
return NextResponse.json({ error: 'Participation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(participation);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération de la participation:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Modifier une participation
|
||||
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 participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
adherent: true,
|
||||
trajet: { include: { adherent: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!participation) {
|
||||
return NextResponse.json({ error: 'Participation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { destinataireEmail, destinataireNom, destinataireType, montant, complement } = body;
|
||||
|
||||
const updated = await prisma.participationFinanciere.update({
|
||||
where: { id: params.id },
|
||||
data: {
|
||||
...(destinataireEmail && { destinataireEmail }),
|
||||
...(destinataireNom && { destinataireNom }),
|
||||
...(destinataireType && { destinataireType }),
|
||||
...(montant !== undefined && { montant }),
|
||||
...(complement !== undefined && { complement }),
|
||||
},
|
||||
});
|
||||
|
||||
// Régénérer le PDF si les données ont changé
|
||||
const dataToUpdate =
|
||||
destinataireEmail || destinataireNom || montant !== undefined || complement !== undefined;
|
||||
if (dataToUpdate) {
|
||||
const filePath = getParticipationStoragePath(participation.id);
|
||||
await generateParticipationPDF(
|
||||
{
|
||||
adherentNom: participation.adherent.nom,
|
||||
adherentPrenom: participation.adherent.prenom,
|
||||
adherentAdresse: participation.adherent.adresse,
|
||||
destinataireEmail: updated.destinataireEmail,
|
||||
destinataireNom: updated.destinataireNom,
|
||||
dateTrajet: participation.trajet.date,
|
||||
adresseDepart: participation.trajet.adresseDepart,
|
||||
adresseArrivee: participation.trajet.adresseArrivee,
|
||||
montant: updated.montant ?? undefined,
|
||||
complement: updated.complement ?? undefined,
|
||||
participationId: participation.id,
|
||||
},
|
||||
filePath
|
||||
);
|
||||
await prisma.participationFinanciere.update({
|
||||
where: { id: params.id },
|
||||
data: { filePath },
|
||||
});
|
||||
}
|
||||
|
||||
const fullUpdated = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
adherent: { select: { id: true, nom: true, prenom: true, email: true } },
|
||||
trajet: { select: { id: true, date: true, adresseDepart: true, adresseArrivee: true, statut: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(fullUpdated);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour de la participation:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH - Changer le statut d'une participation
|
||||
export async function PATCH(
|
||||
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 { statut } = body;
|
||||
|
||||
const validStatuts = ['en_attente', 'envoye', 'paye', 'archive'];
|
||||
if (!statut || !validStatuts.includes(statut)) {
|
||||
return NextResponse.json({ error: 'Statut invalide' }, { status: 400 });
|
||||
}
|
||||
|
||||
const participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
if (!participation) {
|
||||
return NextResponse.json({ error: 'Participation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
const updated = await prisma.participationFinanciere.update({
|
||||
where: { id: params.id },
|
||||
data: { statut },
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du changement de statut:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer une participation
|
||||
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 });
|
||||
}
|
||||
|
||||
const participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
if (!participation) {
|
||||
return NextResponse.json({ error: 'Participation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const filePath = getParticipationStoragePath(participation.id);
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
await prisma.participationFinanciere.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: 'Participation supprimée' });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression de la participation:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user