Skip to content

Commit

Permalink
refactor: use async function to send reserved mail
Browse files Browse the repository at this point in the history
  • Loading branch information
woowabrie committed Oct 18, 2023
1 parent dd427ec commit 9f0e2a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/apply/application/mail/MailMessageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,6 +42,7 @@ class MailMessageService(
}
}

@Transactional
fun reserve(request: MailData): MailMessageResponse {
val mailMessage = mailMessageRepository.save(request.toMailMessage())
val mailReservation = mailReservationRepository.save(
Expand All @@ -51,6 +51,7 @@ class MailMessageService(
return MailMessageResponse(mailMessage, mailReservation)
}

@Transactional
fun cancelReservation(mailMessageId: Long) {
val mailReservation = mailReservationRepository.findByMailMessageId(mailMessageId)
?: throw IllegalArgumentException("메일 예약이 존재하지 않습니다. email: $mailMessageId")
Expand All @@ -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)
}
}

Expand Down
18 changes: 1 addition & 17 deletions src/main/kotlin/apply/application/mail/SendingMailService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SendingMailService(
}

@Async
fun sendMailByBcc(request: MailData, files: Map<String, ByteArrayResource>) {
fun sendMailByBcc(request: MailData, files: Map<String, ByteArrayResource> = emptyMap()) {
val mailMessage = mailMessageRepository.save(request.toMailMessage())
val body = generateMailBody(request)
val recipients = request.recipients + mailProperties.username
Expand All @@ -107,22 +107,6 @@ class SendingMailService(
saveMailHistories(mailMessage.id, succeeded, failed)
}

fun sendMailByBccSynchronous(request: MailData, files: Map<String, ByteArrayResource> = emptyMap()) {
val mailMessage = mailMessageRepository.save(request.toMailMessage())
val body = generateMailBody(request)
val recipients = mailMessage.recipients + mailProperties.username

val succeeded = mutableListOf<String>()
val failed = mutableListOf<String>()
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(
Expand Down

0 comments on commit 9f0e2a2

Please sign in to comment.