58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
|
|
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 });
|
||
|
|
}
|
||
|
|
}
|