From 9f0e2a201f5d3a5c913ee4041d5ff510fc621d93 Mon Sep 17 00:00:00 2001 From: woowabrie Date: Tue, 17 Oct 2023 12:12:57 +0900 Subject: [PATCH] refactor: use async function to send reserved mail --- .../application/mail/MailMessageService.kt | 8 ++++---- .../application/mail/SendingMailService.kt | 18 +----------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/apply/application/mail/MailMessageService.kt b/src/main/kotlin/apply/application/mail/MailMessageService.kt index ded9d0dc9..a13f1807c 100644 --- a/src/main/kotlin/apply/application/mail/MailMessageService.kt +++ b/src/main/kotlin/apply/application/mail/MailMessageService.kt @@ -10,7 +10,6 @@ import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.time.LocalDateTime -@Transactional @Service class MailMessageService( private val sendingMailService: SendingMailService, @@ -43,6 +42,7 @@ class MailMessageService( } } + @Transactional fun reserve(request: MailData): MailMessageResponse { val mailMessage = mailMessageRepository.save(request.toMailMessage()) val mailReservation = mailReservationRepository.save( @@ -51,6 +51,7 @@ class MailMessageService( return MailMessageResponse(mailMessage, mailReservation) } + @Transactional fun cancelReservation(mailMessageId: Long) { val mailReservation = mailReservationRepository.findByMailMessageId(mailMessageId) ?: throw IllegalArgumentException("메일 예약이 존재하지 않습니다. email: $mailMessageId") @@ -67,10 +68,9 @@ class MailMessageService( val messagesById = findMessageMapById(reservations.map { it.mailMessageId }) reservations.forEach { mailReservation -> - mailReservation.send() - mailReservationRepository.save(mailReservation) - sendingMailService.sendMailByBccSynchronous(MailData(messagesById.getValue(mailReservation.mailMessageId))) + sendingMailService.sendMailByBcc(MailData(messagesById.getValue(mailReservation.mailMessageId))) mailReservation.finish() + mailReservationRepository.save(mailReservation) } } diff --git a/src/main/kotlin/apply/application/mail/SendingMailService.kt b/src/main/kotlin/apply/application/mail/SendingMailService.kt index 7fb80fe52..39285c887 100644 --- a/src/main/kotlin/apply/application/mail/SendingMailService.kt +++ b/src/main/kotlin/apply/application/mail/SendingMailService.kt @@ -91,7 +91,7 @@ class SendingMailService( } @Async - fun sendMailByBcc(request: MailData, files: Map) { + fun sendMailByBcc(request: MailData, files: Map = emptyMap()) { val mailMessage = mailMessageRepository.save(request.toMailMessage()) val body = generateMailBody(request) val recipients = request.recipients + mailProperties.username @@ -107,22 +107,6 @@ class SendingMailService( saveMailHistories(mailMessage.id, succeeded, failed) } - fun sendMailByBccSynchronous(request: MailData, files: Map = emptyMap()) { - val mailMessage = mailMessageRepository.save(request.toMailMessage()) - val body = generateMailBody(request) - val recipients = mailMessage.recipients + mailProperties.username - - val succeeded = mutableListOf() - val failed = mutableListOf() - for (addresses in recipients.chunked(MAIL_SENDING_UNIT)) { - runCatching { mailSender.sendBcc(addresses, mailMessage.subject, body, files) } - .onSuccess { succeeded.addAll(addresses) } - .onFailure { failed.addAll(addresses) } - } - - saveMailHistories(mailMessage.id, succeeded, failed) - } - fun generateMailBody(mailData: MailData): String { val context = Context().apply { setVariables(