Added Budget Page
This commit is contained in:
@@ -18,8 +18,12 @@ export async function POST(
|
||||
const participation = await prisma.participationFinanciere.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
adherent: { select: { prenom: true, nom: true } },
|
||||
trajet: { select: { date: true } },
|
||||
adherent: {
|
||||
select: { prenom: true, nom: true, email: true, facturation: true, prescripteur: true },
|
||||
},
|
||||
trajet: {
|
||||
select: { date: true, universProId: true, universPro: { select: { email: true, prenom: true, nom: true, nomEntreprise: true } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -27,6 +31,34 @@ export async function POST(
|
||||
return NextResponse.json({ error: 'Participation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Déterminer l'email selon la facturation de l'adhérent
|
||||
const facturation = (participation.adherent.facturation || '').trim();
|
||||
const isAdherent = !facturation || /^adh[eé]rent$/i.test(facturation);
|
||||
|
||||
let destinataireEmail: string;
|
||||
let destinataireNom: string;
|
||||
|
||||
if (isAdherent) {
|
||||
destinataireEmail = participation.adherent.email;
|
||||
destinataireNom = `${participation.adherent.prenom} ${participation.adherent.nom}`;
|
||||
} else if (participation.trajet?.universProId && participation.trajet?.universPro) {
|
||||
destinataireEmail = participation.trajet.universPro.email;
|
||||
destinataireNom = `${participation.trajet.universPro.prenom} ${participation.trajet.universPro.nom} - ${participation.trajet.universPro.nomEntreprise}`;
|
||||
} else {
|
||||
const universPro = await prisma.universPro.findFirst({
|
||||
where: { nomEntreprise: facturation },
|
||||
});
|
||||
if (universPro) {
|
||||
destinataireEmail = universPro.email;
|
||||
destinataireNom = `${universPro.prenom} ${universPro.nom} - ${universPro.nomEntreprise}`;
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ error: `Aucune fiche Univers Pro trouvée pour "${facturation}". Créez un contact avec ce nom d'entreprise ou vérifiez la facturation de l'adhérent.` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const filePath = getParticipationStoragePath(participation.id);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return NextResponse.json(
|
||||
@@ -72,7 +104,7 @@ export async function POST(
|
||||
|
||||
await transporter.sendMail({
|
||||
from: process.env.SMTP_FROM || smtpUser,
|
||||
to: participation.destinataireEmail,
|
||||
to: destinataireEmail,
|
||||
subject: `Participation financière - ${participation.adherent.prenom} ${participation.adherent.nom} - ${dateTrajet}`,
|
||||
text: `Bonjour,\n\nVeuillez trouver ci-joint la participation financière concernant le trajet du ${dateTrajet} pour ${participation.adherent.prenom} ${participation.adherent.nom}.\n\nCordialement`,
|
||||
html: `
|
||||
@@ -88,14 +120,19 @@ export async function POST(
|
||||
],
|
||||
});
|
||||
|
||||
// Mettre à jour le statut en "envoyé"
|
||||
// Mettre à jour le statut en "envoyé" et le destinataire (au cas où il aurait changé)
|
||||
await prisma.participationFinanciere.update({
|
||||
where: { id: params.id },
|
||||
data: { statut: 'envoye' },
|
||||
data: {
|
||||
statut: 'envoye',
|
||||
destinataireEmail,
|
||||
destinataireNom,
|
||||
destinataireType: isAdherent ? 'adherent' : 'univers_pro',
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
message: `Participation envoyée à ${participation.destinataireEmail}`,
|
||||
message: `Participation envoyée à ${destinataireEmail}. Le budget du prescripteur a été décrementé.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de l\'envoi de l\'email:', error);
|
||||
|
||||
Reference in New Issue
Block a user