92 lines
2.9 KiB
TypeScript
92 lines
2.9 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 selon facturation de l'adhérent
|
|
// Si facturation = "Adhérent" (ou vide) → email adhérent
|
|
// Sinon → rechercher un Univers Pro dont nomEntreprise correspond à la facturation
|
|
let destinataireEmail: string;
|
|
let destinataireNom: string;
|
|
let destinataireType: 'adherent' | 'univers_pro';
|
|
|
|
const facturation = (trajet.adherent.facturation || '').trim();
|
|
const isAdherent = !facturation || /^adh[eé]rent$/i.test(facturation);
|
|
|
|
if (isAdherent) {
|
|
destinataireEmail = trajet.adherent.email;
|
|
destinataireNom = `${trajet.adherent.prenom} ${trajet.adherent.nom}`;
|
|
destinataireType = 'adherent';
|
|
} else if (trajet.universProId && trajet.universPro) {
|
|
destinataireEmail = trajet.universPro.email;
|
|
destinataireNom = `${trajet.universPro.prenom} ${trajet.universPro.nom} - ${trajet.universPro.nomEntreprise}`;
|
|
destinataireType = 'univers_pro';
|
|
} else {
|
|
const universPro = await prisma.universPro.findFirst({
|
|
where: { nomEntreprise: facturation },
|
|
});
|
|
if (universPro) {
|
|
destinataireEmail = universPro.email;
|
|
destinataireNom = `${universPro.prenom} ${universPro.nom} - ${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;
|
|
}
|