74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
|
|
import { prisma } from '@/lib/prisma';
|
||
|
|
import { generateParticipationPDF, getParticipationStoragePath } from './participation-pdf';
|
||
|
|
|
||
|
|
const MONTANT_MOYEN = 6.8;
|
||
|
|
|
||
|
|
export async function createParticipationForTrajet(trajetId: string): Promise<boolean> {
|
||
|
|
const trajet = await prisma.trajet.findUnique({
|
||
|
|
where: { id: trajetId },
|
||
|
|
include: {
|
||
|
|
adherent: true,
|
||
|
|
universPro: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!trajet || !['Terminé', 'Validé'].includes(trajet.statut)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const existing = await prisma.participationFinanciere.findUnique({
|
||
|
|
where: { trajetId },
|
||
|
|
});
|
||
|
|
if (existing) return true;
|
||
|
|
|
||
|
|
// Destinataire: univers pro si lié, sinon adhérent
|
||
|
|
let destinataireEmail: string;
|
||
|
|
let destinataireNom: string;
|
||
|
|
let destinataireType: 'adherent' | 'univers_pro';
|
||
|
|
|
||
|
|
if (trajet.universProId && trajet.universPro) {
|
||
|
|
destinataireEmail = trajet.universPro.email;
|
||
|
|
destinataireNom = `${trajet.universPro.prenom} ${trajet.universPro.nom} - ${trajet.universPro.nomEntreprise}`;
|
||
|
|
destinataireType = 'univers_pro';
|
||
|
|
} else {
|
||
|
|
destinataireEmail = trajet.adherent.email;
|
||
|
|
destinataireNom = `${trajet.adherent.prenom} ${trajet.adherent.nom}`;
|
||
|
|
destinataireType = 'adherent';
|
||
|
|
}
|
||
|
|
|
||
|
|
const participation = await prisma.participationFinanciere.create({
|
||
|
|
data: {
|
||
|
|
trajetId,
|
||
|
|
adherentId: trajet.adherentId,
|
||
|
|
destinataireEmail,
|
||
|
|
destinataireNom,
|
||
|
|
destinataireType,
|
||
|
|
montant: MONTANT_MOYEN,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const filePath = getParticipationStoragePath(participation.id);
|
||
|
|
await generateParticipationPDF(
|
||
|
|
{
|
||
|
|
adherentNom: trajet.adherent.nom,
|
||
|
|
adherentPrenom: trajet.adherent.prenom,
|
||
|
|
adherentAdresse: trajet.adherent.adresse,
|
||
|
|
destinataireEmail,
|
||
|
|
destinataireNom,
|
||
|
|
dateTrajet: trajet.date,
|
||
|
|
adresseDepart: trajet.adresseDepart,
|
||
|
|
adresseArrivee: trajet.adresseArrivee,
|
||
|
|
montant: MONTANT_MOYEN,
|
||
|
|
participationId: participation.id,
|
||
|
|
},
|
||
|
|
filePath
|
||
|
|
);
|
||
|
|
|
||
|
|
await prisma.participationFinanciere.update({
|
||
|
|
where: { id: participation.id },
|
||
|
|
data: { filePath },
|
||
|
|
});
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|