Skip to content

Commit

Permalink
refactor: move mails functions into MailMessageService
Browse files Browse the repository at this point in the history
  • Loading branch information
woowabrie committed Oct 10, 2023
1 parent f2760b4 commit 49f91f6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 111 deletions.
31 changes: 28 additions & 3 deletions src/main/kotlin/apply/application/mail/MailDtos.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apply.application.mail

import apply.domain.mail.MailHistory
import apply.domain.mail.MailMessage
import apply.domain.mail.MailReservation
import apply.domain.mail.MailReservationStatus
Expand All @@ -12,16 +13,24 @@ data class MailMessageResponse(
val sender: String,
val recipients: List<String>,
val createdDateTime: LocalDateTime,
val reservation: MailReservationResponse?
val sentTime: LocalDateTime?,
val reservation: MailReservationResponse?,
val histories: List<MailHistoryResponse>
) {
constructor(mailMessage: MailMessage, mailReservation: MailReservation? = null) : this(
constructor(
mailMessage: MailMessage,
mailReservation: MailReservation? = null,
mailHistories: List<MailHistory> = emptyList()
) : this(
mailMessage.id,
mailMessage.subject,
mailMessage.body,
mailMessage.sender,
mailMessage.recipients,
mailMessage.createdDateTime,
mailReservation?.let { MailReservationResponse(it) }
mailHistories.firstOrNull()?.sentTime,
mailReservation?.let { MailReservationResponse(it) },
mailHistories.map { MailHistoryResponse(it) }
)
}

Expand All @@ -38,3 +47,19 @@ data class MailReservationResponse(
mailReservation.reservationTime
)
}

data class MailHistoryResponse(
val id: Long,
val mailMessageId: Long,
val recipients: List<String>,
val success: Boolean,
val sentTime: LocalDateTime
) {
constructor(mailHistory: MailHistory) : this(
mailHistory.id,
mailHistory.mailMessageId,
mailHistory.recipients,
mailHistory.success,
mailHistory.sentTime
)
}
53 changes: 53 additions & 0 deletions src/main/kotlin/apply/application/mail/MailMessageService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
package apply.application.mail

import apply.domain.mail.MailHistoryRepository
import apply.domain.mail.MailMessage
import apply.domain.mail.MailMessageRepository
import apply.domain.mail.MailReservation
import apply.domain.mail.MailReservationRepository
import apply.domain.mail.MailReservationStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Transactional
@Service
class MailMessageService(
private val mailService: MailService,
private val mailMessageRepository: MailMessageRepository,
private val mailReservationRepository: MailReservationRepository,
private val mailHistoryRepository: MailHistoryRepository
) {
fun findSentMails(): List<MailMessageResponse> {
val histories = mailHistoryRepository.findAll()
val messagesById = findMessageMapById(histories.map { it.mailMessageId })
return messagesById.map { (id, message) ->
MailMessageResponse(
mailMessage = message,
mailHistories = histories.filter { it.mailMessageId == id }
)
}
}

fun findReservedMails(): List<MailMessageResponse> {
val reservations = mailReservationRepository.findByStatus(MailReservationStatus.WAITING)
val messagesById = findMessageMapById(reservations.map { it.mailMessageId })

return reservations
.filter { messagesById.contains(it.mailMessageId) }
.map {
MailMessageResponse(
mailMessage = messagesById.getValue(it.mailMessageId),
mailReservation = it
)
}
}

fun reserve(request: MailData): MailMessageResponse {
val mailMessage = mailMessageRepository.save(request.toMailMessage())
val mailReservation = mailReservationRepository.save(
Expand All @@ -27,4 +58,26 @@ class MailMessageService(
mailReservationRepository.deleteById(mailReservation.id)
mailMessageRepository.deleteById(mailReservation.mailMessageId)
}

fun sendReservedMail(standardTime: LocalDateTime = LocalDateTime.now()) {
val reservations = mailReservationRepository.findByReservationTimeBetweenAndStatus(
standardTime.minusMinutes(1),
standardTime.plusMinutes(1),
MailReservationStatus.WAITING
)
val messagesById = findMessageMapById(reservations.map { it.mailMessageId })

reservations.forEach { mailReservation ->
mailReservation.send()
mailReservationRepository.save(mailReservation)
mailService.sendMailsByBccSynchronous(MailData(messagesById.getValue(mailReservation.id)), emptyMap())
mailReservation.finish()
}
}

private fun findMessageMapById(mailMessageIds: List<Long>): Map<Long, MailMessage> {
return mailMessageRepository
.findAllById(mailMessageIds)
.associateBy { it.id }
}
}
39 changes: 0 additions & 39 deletions src/main/kotlin/apply/application/mail/MailReservationService.kt

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/kotlin/apply/ui/api/MailReservationRestController.kt

This file was deleted.

12 changes: 11 additions & 1 deletion src/main/kotlin/apply/ui/api/MailRestController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apply.ui.api

import apply.application.mail.MailData
import apply.application.mail.MailMessageService
import apply.application.mail.MailService
import apply.domain.user.User
import apply.security.LoginUser
Expand All @@ -15,7 +16,8 @@ import org.springframework.web.multipart.MultipartFile
@RestController
@RequestMapping("/api/mail")
class MailRestController(
private val mailService: MailService
private val mailService: MailService,
private val mailMessageService: MailMessageService
) {
@PostMapping
fun sendMail(
Expand All @@ -27,4 +29,12 @@ class MailRestController(
mailService.sendMailsByBcc(request, inputStreamFiles)
return ResponseEntity.noContent().build()
}

@PostMapping("/reserved")
fun sendMail(
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
mailMessageService.sendReservedMail()
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import apply.createMailMessage
import apply.createMailReservation
import apply.domain.mail.MailMessageRepository
import apply.domain.mail.MailReservationRepository
import apply.domain.mail.MailReservationStatus
import apply.domain.mail.getOrThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
Expand Down Expand Up @@ -90,4 +91,22 @@ class MailMessageIntegrationTest(
}
}
}

Given("특정 시간에 발송할 예약 메일이 있는 경우") {
val reservationTime = createAvailableReservationTime()
val mailMessage1 = mailMessageRepository.save(createMailMessage())
val mailMessage2 = mailMessageRepository.save(createMailMessage())

mailReservationRepository.save(createMailReservation(mailMessage1.id, reservationTime))
mailReservationRepository.save(createMailReservation(mailMessage2.id, reservationTime.plusHours(3)))

When("해당 시간에 메일 발송 요청을 하면") {
mailMessageService.sendReservedMail(reservationTime)

Then("메일 전송이 완료된다") {
val actual = mailReservationRepository.findByStatus(MailReservationStatus.FINISHED)
actual.size shouldBe 1
}
}
}
})

This file was deleted.

0 comments on commit 49f91f6

Please sign in to comment.