diff --git a/src/main/kotlin/apply/application/mail/MailReservationService.kt b/src/main/kotlin/apply/application/mail/MailReservationService.kt index c7b5a5b12..e7619318f 100644 --- a/src/main/kotlin/apply/application/mail/MailReservationService.kt +++ b/src/main/kotlin/apply/application/mail/MailReservationService.kt @@ -31,9 +31,9 @@ class MailReservationService( reservations.forEach { mailReservation -> mailReservation.send() - mailService.sendMailsByBcc(MailData(messagesById.getValue(mailReservation.id)), emptyMap()) { - mailReservation.finish() - } + mailReservationRepository.save(mailReservation) + mailService.sendMailsByBccSynchronous(MailData(messagesById.getValue(mailReservation.id)), emptyMap()) + mailReservation.finish() } } } diff --git a/src/main/kotlin/apply/application/mail/MailService.kt b/src/main/kotlin/apply/application/mail/MailService.kt index d4daaeece..1525e1180 100644 --- a/src/main/kotlin/apply/application/mail/MailService.kt +++ b/src/main/kotlin/apply/application/mail/MailService.kt @@ -2,6 +2,9 @@ package apply.application.mail import apply.application.ApplicationProperties import apply.domain.applicationform.ApplicationFormSubmittedEvent +import apply.domain.mail.MailHistory +import apply.domain.mail.MailHistoryRepository +import apply.domain.mail.MailMessageRepository import apply.domain.mail.MailSentEvent import apply.domain.recruitment.RecruitmentRepository import apply.domain.recruitment.getOrThrow @@ -24,6 +27,8 @@ private const val MAIL_SENDING_UNIT: Int = 50 class MailService( private val userRepository: UserRepository, private val recruitmentRepository: RecruitmentRepository, + private val mailMessageRepository: MailMessageRepository, + private val mailHistoryRepository: MailHistoryRepository, private val applicationProperties: ApplicationProperties, private val templateEngine: ISpringTemplateEngine, private val mailSender: MailSender, @@ -89,7 +94,7 @@ class MailService( } @Async - fun sendMailsByBcc(request: MailData, files: Map, afterAction: () -> Unit = {}) { + fun sendMailsByBcc(request: MailData, files: Map) { val body = generateMailBody(request) val recipients = request.recipients + mailProperties.username @@ -102,7 +107,22 @@ class MailService( } eventPublisher.publishEvent(MailSentEvent(request, succeeded, failed)) - afterAction() + } + + fun sendMailsByBccSynchronous(request: MailData, files: Map) { + 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 { @@ -116,4 +136,22 @@ class MailService( } return templateEngine.process("mail/common", context) } + + private fun saveMailHistories( + mailMessageId: Long, + succeeded: MutableList, + failed: MutableList + ) { + val mailHistories = mutableListOf() + + if (succeeded.isNotEmpty()) { + mailHistories.add(MailHistory(mailMessageId, succeeded, true)) + } + + if (failed.isNotEmpty()) { + mailHistories.add(MailHistory(mailMessageId, failed, false)) + } + + mailHistoryRepository.saveAll(mailHistories) + } }