feat: make SportEasy invitation email sending asynchronous
This commit is contained in:
+35
-28
@@ -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,39 +98,34 @@ 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);
|
||||||
|
|
||||||
if (mailSender != null) {
|
// Envoi asynchrone via CompletableFuture pour ne pas bloquer la réponse HTTP
|
||||||
try {
|
java.util.concurrent.CompletableFuture.runAsync(() -> {
|
||||||
MimeMessage message = mailSender.createMimeMessage();
|
if (mailSender != null) {
|
||||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
try {
|
||||||
helper.setFrom(fromEmail);
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
helper.setTo(recipientEmail);
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
helper.setSubject(subject);
|
helper.setFrom(fromEmail);
|
||||||
helper.setText(htmlContent, true);
|
helper.setTo(recipientEmail);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.info("Aucun JavaMailSender configuré (Mode DEV). Simulation de l'envoi d'e-mail SportEasy.");
|
||||||
logDevEmail(recipientEmail, subject, htmlContent);
|
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) {
|
return true;
|
||||||
licence.setSportEasyEmailSent(true);
|
|
||||||
licence.setSportEasyEmailSentAt(LocalDateTime.now());
|
|
||||||
licenceRepository.save(licence);
|
|
||||||
}
|
|
||||||
|
|
||||||
return sentSuccessfully;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
|||||||
Reference in New Issue
Block a user