feat: make SportEasy invitation email sending asynchronous
AS Talange CI/CD Pipeline / Build & Run Unit Tests (push) Successful in 4m47s
AS Talange CI/CD Pipeline / Deploy to Test Environment (push) Successful in 6m21s

This commit is contained in:
2026-08-06 22:42:33 +02:00
parent c40f70e6b3
commit a8e94603bd
@@ -43,6 +43,18 @@ public class SportEasyEmailService {
public record BatchSendResult(int sentCount, int skippedCount, int errorCount, String message) {} public record BatchSendResult(int sentCount, int skippedCount, int errorCount, String message) {}
public void sendInvitationForLicenceAsync(Long licenceId) {
if (licenceId == null) {
log.warn("Impossible d'envoyer l'invitation SportEasy : ID de licence nul.");
return;
}
try {
sendInvitationForLicence(licenceId, true);
} catch (Exception e) {
log.error("Erreur lors de l'envoi asynchrone de l'invitation SportEasy pour la licence ID {}: ", licenceId, e);
}
}
@Transactional @Transactional
public boolean sendInvitationForLicence(Long licenceId, boolean forceResend) { public boolean sendInvitationForLicence(Long licenceId, boolean forceResend) {
Licence licence = licenceRepository.findById(licenceId) Licence licence = licenceRepository.findById(licenceId)
@@ -86,8 +98,12 @@ public class SportEasyEmailService {
String subject = "AS Talange - Invitation SportEasy (" + categorieNom + ")"; String subject = "AS Talange - Invitation SportEasy (" + categorieNom + ")";
String htmlContent = buildEmailHtml(adherentNom, categorieNom, saisonNom, inviteUrl); String htmlContent = buildEmailHtml(adherentNom, categorieNom, saisonNom, inviteUrl);
boolean sentSuccessfully = false; licence.setSportEasyEmailSent(true);
licence.setSportEasyEmailSentAt(LocalDateTime.now());
licenceRepository.save(licence);
// Envoi asynchrone via CompletableFuture pour ne pas bloquer la réponse HTTP
java.util.concurrent.CompletableFuture.runAsync(() -> {
if (mailSender != null) { if (mailSender != null) {
try { try {
MimeMessage message = mailSender.createMimeMessage(); MimeMessage message = mailSender.createMimeMessage();
@@ -98,27 +114,18 @@ public class SportEasyEmailService {
helper.setText(htmlContent, true); helper.setText(htmlContent, true);
mailSender.send(message); mailSender.send(message);
sentSuccessfully = true;
log.info("E-mail d'invitation SportEasy envoyé avec succès via SMTP à {} pour {}", recipientEmail, adherentNom); log.info("E-mail d'invitation SportEasy envoyé avec succès via SMTP à {} pour {}", recipientEmail, adherentNom);
} catch (Exception e) { } catch (Exception e) {
log.warn("Impossible d'envoyer l'e-mail via SMTP pour {}: {}. Bascule en mode simulation / log.", recipientEmail, e.getMessage()); log.warn("Impossible d'envoyer l'e-mail via SMTP pour {}: {}. Bascule en mode simulation / log.", recipientEmail, e.getMessage());
logDevEmail(recipientEmail, subject, htmlContent); logDevEmail(recipientEmail, subject, htmlContent);
// In dev environment or fallback, mark as processed anyway to simulate full flow
sentSuccessfully = true;
} }
} else { } else {
log.info("Aucun JavaMailSender configuré (Mode DEV). Simulation de l'envoi d'e-mail SportEasy."); log.info("Aucun JavaMailSender configuré (Mode DEV). Simulation de l'envoi d'e-mail SportEasy.");
logDevEmail(recipientEmail, subject, htmlContent); logDevEmail(recipientEmail, subject, htmlContent);
sentSuccessfully = true;
} }
});
if (sentSuccessfully) { return true;
licence.setSportEasyEmailSent(true);
licence.setSportEasyEmailSentAt(LocalDateTime.now());
licenceRepository.save(licence);
}
return sentSuccessfully;
} }
@Transactional @Transactional