refactor(use-case): persist status and error if present

This commit is contained in:
Pablo de la Torre Jamardo 2025-09-15 08:26:02 +02:00
parent fb0ddf391f
commit f04fb14192

View File

@ -39,19 +39,19 @@ public class SendEmailUseCase {
public void handle(EmailDTO emailDTO) {
String to = emailConfigurationPort.getDefaultRecipient();
Email email = Email.builder()
.from(emailDTO.from())
.to(to)
.subject(emailDTO.subject())
.body(emailDTO.body())
.build();
Email email = Email.create(emailDTO.from(), to, emailDTO.subject(), emailDTO.body());
emailValidatorService.validate(email);
log.info("Sending email from {} to {}", emailDTO.from(), to);
email = emailService.sendEmail(email);
try {
email = emailService.sendEmail(email);
email.markAsSent();
} catch (Exception e) {
log.error("Error sending email", e);
email.markAsFailed(e.getMessage());
}
emailRepository.save(email);
log.info("Email successfully sent and persisted to repository for recipient {}", to);
}