feat: make SportEasy invitation email sending asynchronous
This commit is contained in:
+19
-12
@@ -43,6 +43,18 @@ public class SportEasyEmailService {
|
||||
|
||||
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
|
||||
public boolean sendInvitationForLicence(Long licenceId, boolean forceResend) {
|
||||
Licence licence = licenceRepository.findById(licenceId)
|
||||
@@ -86,8 +98,12 @@ public class SportEasyEmailService {
|
||||
String subject = "AS Talange - Invitation SportEasy (" + categorieNom + ")";
|
||||
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) {
|
||||
try {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
@@ -98,27 +114,18 @@ public class SportEasyEmailService {
|
||||
helper.setText(htmlContent, true);
|
||||
|
||||
mailSender.send(message);
|
||||
sentSuccessfully = true;
|
||||
log.info("E-mail d'invitation SportEasy envoyé avec succès via SMTP à {} pour {}", recipientEmail, adherentNom);
|
||||
} catch (Exception e) {
|
||||
log.warn("Impossible d'envoyer l'e-mail via SMTP pour {}: {}. Bascule en mode simulation / log.", recipientEmail, e.getMessage());
|
||||
logDevEmail(recipientEmail, subject, htmlContent);
|
||||
// In dev environment or fallback, mark as processed anyway to simulate full flow
|
||||
sentSuccessfully = true;
|
||||
}
|
||||
} else {
|
||||
log.info("Aucun JavaMailSender configuré (Mode DEV). Simulation de l'envoi d'e-mail SportEasy.");
|
||||
logDevEmail(recipientEmail, subject, htmlContent);
|
||||
sentSuccessfully = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (sentSuccessfully) {
|
||||
licence.setSportEasyEmailSent(true);
|
||||
licence.setSportEasyEmailSentAt(LocalDateTime.now());
|
||||
licenceRepository.save(licence);
|
||||
}
|
||||
|
||||
return sentSuccessfully;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
Reference in New Issue
Block a user