Added Participation Page
This commit is contained in:
57
app/api/participations/[id]/pdf/route.ts
Normal file
57
app/api/participations/[id]/pdf/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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 le PDF d'une participation (régénéré à chaque vue pour le design à jour)
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return new NextResponse('Non autorisé', { status: 401 });
|
||||
}
|
||||
|
||||
const participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
adherent: true,
|
||||
trajet: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!participation) {
|
||||
return new NextResponse('Participation non trouvée', { status: 404 });
|
||||
}
|
||||
|
||||
const filePath = getParticipationStoragePath(participation.id);
|
||||
const pdfBuffer = await generateParticipationPDF(
|
||||
{
|
||||
adherentNom: participation.adherent.nom,
|
||||
adherentPrenom: participation.adherent.prenom,
|
||||
adherentAdresse: participation.adherent.adresse,
|
||||
destinataireEmail: participation.destinataireEmail,
|
||||
destinataireNom: participation.destinataireNom,
|
||||
dateTrajet: participation.trajet.date,
|
||||
adresseDepart: participation.trajet.adresseDepart,
|
||||
adresseArrivee: participation.trajet.adresseArrivee,
|
||||
montant: participation.montant ?? undefined,
|
||||
complement: participation.complement ?? undefined,
|
||||
participationId: participation.id,
|
||||
},
|
||||
filePath
|
||||
);
|
||||
|
||||
return new NextResponse(pdfBuffer, {
|
||||
headers: {
|
||||
'Content-Type': 'application/pdf',
|
||||
'Content-Disposition': `inline; filename="participation-${params.id}.pdf"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération du PDF:', error);
|
||||
return new NextResponse('Erreur serveur', { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user