Skip to content

Commit

Permalink
refactor: add synchronous function to send mail
Browse files Browse the repository at this point in the history
  • Loading branch information
woowabrie committed Oct 10, 2023
1 parent 2e9e70b commit 7804385
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
42 changes: 40 additions & 2 deletions src/main/kotlin/apply/application/mail/MailService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -89,7 +94,7 @@ class MailService(
}

@Async
fun sendMailsByBcc(request: MailData, files: Map<String, ByteArrayResource>, afterAction: () -> Unit = {}) {
fun sendMailsByBcc(request: MailData, files: Map<String, ByteArrayResource>) {
val body = generateMailBody(request)
val recipients = request.recipients + mailProperties.username

Expand All @@ -102,7 +107,22 @@ class MailService(
}

eventPublisher.publishEvent(MailSentEvent(request, succeeded, failed))
afterAction()
}

fun sendMailsByBccSynchronous(request: MailData, files: Map<String, ByteArrayResource>) {
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 {
Expand All @@ -116,4 +136,22 @@ class MailService(
}
return templateEngine.process("mail/common", context)
}

private fun saveMailHistories(
mailMessageId: Long,
succeeded: MutableList<String>,
failed: MutableList<String>
) {
val mailHistories = mutableListOf<MailHistory>()

if (succeeded.isNotEmpty()) {
mailHistories.add(MailHistory(mailMessageId, succeeded, true))
}

if (failed.isNotEmpty()) {
mailHistories.add(MailHistory(mailMessageId, failed, false))
}

mailHistoryRepository.saveAll(mailHistories)
}
}

0 comments on commit 7804385

Please sign in to comment.