2026-02-15 14:36:28 +01:00
import { NextRequest , NextResponse } from 'next/server' ;
import { prisma } from '@/lib/prisma' ;
import { getCurrentUser } from '@/lib/auth' ;
import { getParticipationStoragePath } from '@/lib/participation-pdf' ;
import fs from 'fs' ;
// POST - Envoyer la participation par email
export async function POST (
request : NextRequest ,
{ params } : { params : { id : string } }
) {
try {
const user = await getCurrentUser ( ) ;
if ( ! user ) {
return NextResponse . json ( { error : 'Non autorisé' } , { status : 401 } ) ;
}
const participation = await prisma . participationFinanciere . findUnique ( {
where : { id : params.id } ,
include : {
2026-02-15 15:05:59 +01:00
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 } } } ,
} ,
2026-02-15 14:36:28 +01:00
} ,
} ) ;
if ( ! participation ) {
return NextResponse . json ( { error : 'Participation non trouvée' } , { status : 404 } ) ;
}
2026-02-15 15:05:59 +01:00
// 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 }
) ;
}
}
2026-02-15 14:36:28 +01:00
const filePath = getParticipationStoragePath ( participation . id ) ;
if ( ! fs . existsSync ( filePath ) ) {
return NextResponse . json (
{ error : 'Le document PDF n\'a pas été généré' } ,
{ status : 400 }
) ;
}
// Vérifier la configuration email
const smtpHost = process . env . SMTP_HOST ;
const smtpUser = process . env . SMTP_USER ;
const smtpPass = process . env . SMTP_PASS ;
if ( ! smtpHost || ! smtpUser || ! smtpPass ) {
return NextResponse . json (
{
error :
"L'envoi par email n'est pas configuré. Configurez SMTP_HOST, SMTP_USER et SMTP_PASS dans les variables d'environnement." ,
} ,
{ status : 503 }
) ;
}
const nodemailer = await import ( 'nodemailer' ) ;
const transporter = nodemailer . default . createTransport ( {
host : smtpHost ,
port : parseInt ( process . env . SMTP_PORT || '587' ) ,
secure : process.env.SMTP_SECURE === 'true' ,
auth : {
user : smtpUser ,
pass : smtpPass ,
} ,
} ) ;
const pdfBuffer = fs . readFileSync ( filePath ) ;
const dateTrajet = participation . trajet ? . date
? new Date ( participation . trajet . date ) . toLocaleDateString ( 'fr-FR' , {
day : '2-digit' ,
month : 'long' ,
year : 'numeric' ,
} )
: '' ;
await transporter . sendMail ( {
from : process . env . SMTP_FROM || smtpUser ,
2026-02-15 15:05:59 +01:00
to : destinataireEmail ,
2026-02-15 14:36:28 +01:00
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 : `
< p > Bonjour , < / p >
< p > Veuillez trouver ci - joint la participation financière concernant le trajet du < strong > $ { dateTrajet } < / strong > pour < strong > $ { participation . adherent . prenom } $ { participation . adherent . nom } < / strong > . < / p >
< p > Cordialement < / p >
` ,
attachments : [
{
filename : ` participation-financiere- ${ params . id } .pdf ` ,
content : pdfBuffer ,
} ,
] ,
} ) ;
2026-02-15 15:05:59 +01:00
// Mettre à jour le statut en "envoyé" et le destinataire (au cas où il aurait changé)
2026-02-15 14:36:28 +01:00
await prisma . participationFinanciere . update ( {
where : { id : params.id } ,
2026-02-15 15:05:59 +01:00
data : {
statut : 'envoye' ,
destinataireEmail ,
destinataireNom ,
destinataireType : isAdherent ? 'adherent' : 'univers_pro' ,
} ,
2026-02-15 14:36:28 +01:00
} ) ;
return NextResponse . json ( {
2026-02-15 15:05:59 +01:00
message : ` Participation envoyée à ${ destinataireEmail } . Le budget du prescripteur a été décrementé. ` ,
2026-02-15 14:36:28 +01:00
} ) ;
} catch ( error ) {
console . error ( 'Erreur lors de l\'envoi de l\'email:' , error ) ;
return NextResponse . json (
{
error :
error instanceof Error ? error . message : 'Erreur lors de l\'envoi de l\'email' ,
} ,
{ status : 500 }
) ;
}
}