diff --git a/src/main/java/net/dancier/dancer/core/events/ApplicationEventListener.java b/src/main/java/net/dancier/dancer/core/events/ApplicationEventListener.java index be59a88c..cddfdaf6 100644 --- a/src/main/java/net/dancier/dancer/core/events/ApplicationEventListener.java +++ b/src/main/java/net/dancier/dancer/core/events/ApplicationEventListener.java @@ -3,12 +3,14 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import net.dancier.dancer.core.ScheduleMessagePort; +import net.dancier.dancer.mail.model.DancierMailMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import java.net.URI; +import java.util.UUID; @Component @RequiredArgsConstructor @@ -20,7 +22,16 @@ public class ApplicationEventListener { private final ScheduleMessagePort scheduleMessagePort; @EventListener - @Transactional + public void handle(DancierMailMessage dancierMailMessage) { + scheduleMessagePort.schedule( + dancierMailMessage, + UUID.randomUUID().toString(), + SOURCE, + "email-sending-requested" + ); + } + + @EventListener public void handle(ProfileUpdatedEvent profileUpdatedEvent) { log.info("Got a Profile Change: {}", profileUpdatedEvent); scheduleMessagePort.schedule( diff --git a/src/main/java/net/dancier/dancer/mail/service/MailEnqueueService.java b/src/main/java/net/dancier/dancer/mail/service/MailEnqueueService.java index 8afe08a9..bb3e9ea5 100644 --- a/src/main/java/net/dancier/dancer/mail/service/MailEnqueueService.java +++ b/src/main/java/net/dancier/dancer/mail/service/MailEnqueueService.java @@ -7,6 +7,7 @@ import net.dancier.dancer.mail.model.OutgoingMailStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Component; @@ -18,12 +19,15 @@ public class MailEnqueueService { private final OutgoingMailRepository outgoingMailRepository; + private final ApplicationEventPublisher applicationEventPublisher; + public void enqueueMail(DancierMailMessage dancierMailMessage) { OutgoingMail outgoingMail = new OutgoingMail(); outgoingMail.setStatus(OutgoingMailStatus.QUEUED); outgoingMail.setRetry(0); outgoingMail.setMail(dancierMailMessage); this.outgoingMailRepository.save(outgoingMail); + applicationEventPublisher.publishEvent(dancierMailMessage); } }