Skip to content

Commit

Permalink
refactor: make relationships with MailHistory and MailMessage by id r…
Browse files Browse the repository at this point in the history
…eference
  • Loading branch information
woowabrie committed Oct 9, 2023
1 parent 00ee05f commit 3fab767
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
18 changes: 9 additions & 9 deletions src/main/kotlin/apply/application/mail/MailData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ data class MailData(
@field:NotNull
var id: Long = 0L
) {
constructor(mailHistory: MailHistory) : this(
mailHistory.mailMessage.subject,
mailHistory.mailMessage.body,
mailHistory.mailMessage.sender,
mailHistory.recipients,
mailHistory.sentTime,
id = mailHistory.id
)

constructor(mailMessage: MailMessage) : this(
mailMessage.subject,
mailMessage.body,
Expand All @@ -48,6 +39,15 @@ data class MailData(
id = mailMessage.id
)

constructor(mailMessage: MailMessage, mailHistory: MailHistory) : this(
mailMessage.subject,
mailMessage.body,
mailMessage.sender,
mailHistory.recipients,
mailHistory.sentTime,
id = mailHistory.id
)

fun toMailMessage(): MailMessage {
// TODO: 작성자 ID 바인딩
return MailMessage(subject, body, sender, recipients, 1L)
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/apply/application/mail/MailHistoryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ class MailHistoryService(

val mailHistories = mutableListOf<MailHistory>()
if (event.succeedRecipients.isNotEmpty()) {
mailHistories.add(MailHistory(mailMessage, event.succeedRecipients, true))
mailHistories.add(MailHistory(mailMessage.id, event.succeedRecipients, true))
}
if (event.failedRecipients.isNotEmpty()) {
mailHistories.add(MailHistory(mailMessage, event.failedRecipients, false))
mailHistories.add(MailHistory(mailMessage.id, event.failedRecipients, false))
}

mailHistoryRepository.saveAll(mailHistories)
}

fun findAll(): List<MailData> {
return mailHistoryRepository.findAll().map { MailData(it) }
val histories = mailHistoryRepository.findAll()
val messagesById = mailMessageRepository.findAllById(histories.map { it.mailMessageId }).associateBy { it.id }
return histories.map { MailData(messagesById.getValue(it.mailMessageId), it) }
}

fun getById(mailHistoryId: Long): MailData {
val mailHistory = mailHistoryRepository.getOrThrow(mailHistoryId)
return MailData(mailHistory)
val mailMessage = mailMessageRepository.getOrThrow(mailHistory.mailMessageId)
return MailData(mailMessage, mailHistory)
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/apply/config/DatabaseInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ class DatabaseInitializer(

val mailHistories = listOf(
MailHistory(
mailMessage = mailMessage,
mailMessageId = mailMessage.id,
recipients = mailMessage.recipients.subList(0, 2),
true
success = true
),
MailHistory(
mailMessage = mailMessage,
mailMessageId = mailMessage.id,
recipients = mailMessage.recipients.subList(3, 4),
true
success = true
)
)
mailHistoryRepository.saveAll(mailHistories)
Expand Down
8 changes: 2 additions & 6 deletions src/main/kotlin/apply/domain/mail/MailHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import java.time.LocalDateTime
import javax.persistence.Column
import javax.persistence.Convert
import javax.persistence.Entity
import javax.persistence.ForeignKey
import javax.persistence.JoinColumn
import javax.persistence.Lob
import javax.persistence.ManyToOne

@Entity
class MailHistory(
@ManyToOne
@JoinColumn(nullable = false, foreignKey = ForeignKey(name = "fk_mail_history_to_mail_message"))
val mailMessage: MailMessage,
@Column(nullable = false)
val mailMessageId: Long = 0L,

@Column(nullable = false)
@Convert(converter = StringToListConverter::class)
Expand Down
7 changes: 3 additions & 4 deletions src/test/kotlin/apply/MailHistoryFixtures.kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.time.LocalDateTime
private const val SUBJECT: String = "메일제목"
private const val BODY: String = "메일 본문 입니다."
private const val SENDER: String = "[email protected]"
private const val MAIL_MESSAGE_ID: Long = 1L
private val RECIPIENTS: List<String> = listOf("[email protected]", "[email protected]")
private val SENT_TIME: LocalDateTime = LocalDateTime.now()
private val RESERVATION_TIME: LocalDateTime = LocalDateTime.now().plusHours(3).withMinute(0)
Expand Down Expand Up @@ -44,10 +45,8 @@ fun createMailReservation(
}

fun createSuccessMailHistory(
subject: String = SUBJECT,
body: String = BODY,
sender: String = SENDER,
mailMessageId: Long = MAIL_MESSAGE_ID,
recipients: List<String> = RECIPIENTS
): MailHistory {
return MailHistory(createMailMessage(subject, body, sender, recipients), recipients, true)
return MailHistory(mailMessageId, recipients, true)
}
15 changes: 10 additions & 5 deletions src/test/kotlin/apply/application/MailHistoryServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package apply.application

import apply.application.mail.MailHistoryService
import apply.createMailMessage
import apply.createSuccessMailHistory
import apply.domain.mail.MailHistoryRepository
import apply.domain.mail.MailMessageRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.collections.shouldContain
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
Expand All @@ -18,17 +19,21 @@ class MailHistoryServiceTest : BehaviorSpec({
val mailHistoryService = MailHistoryService(mailHistoryRepository, mailMessageRepository)

Given("메일 이력이 있는 경우") {
val mailMessage = createMailMessage(id = 1L)

every { mailHistoryRepository.findAll() } returns listOf(
createSuccessMailHistory(subject = "제목1"),
createSuccessMailHistory(subject = "제목2")
createSuccessMailHistory(mailMessage.id, listOf("[email protected]")),
createSuccessMailHistory(mailMessage.id, listOf("[email protected]"))
)

every { mailMessageRepository.findAllById(any()) } returns listOf(mailMessage)

When("모든 메일 이력을 조회하면") {
val actual = mailHistoryService.findAll()

Then("모든 메일 이력을 확인할 수 있다") {
actual[0].subject shouldBe "제목1"
actual[1].subject shouldBe "제목2"
actual[0].recipients shouldContain "[email protected]"
actual[1].recipients shouldContain "[email protected]"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class MailHistoryRepositoryTest(
val mailMessage = mailMessageRepository.save(createMailMessage())

expect("메일 발송 성공에 대한 히스토리를 저장한다") {
val actual = mailHistoryRepository.save(MailHistory(mailMessage, mailMessage.recipients, true))
val actual = mailHistoryRepository.save(MailHistory(mailMessage.id, mailMessage.recipients, true))
actual.id.shouldNotBeZero()
}
}

context("메일 히스토리 조회") {
val mailMessage = mailMessageRepository.save(createMailMessage())
mailHistoryRepository.save(MailHistory(mailMessage, mailMessage.recipients, true))
mailHistoryRepository.save(MailHistory(mailMessage.id, mailMessage.recipients, true))

expect("메일 발송 성공에 대한 히스토리를 저장한다") {
val actual = mailHistoryRepository.findAll()
actual.shouldNotBeEmpty()
actual[0].mailMessage.id.shouldNotBeZero()
actual[0].mailMessageId.shouldNotBeZero()
}
}

Expand Down

0 comments on commit 3fab767

Please sign in to comment.