Skip to content

Commit

Permalink
refactor: delete factory method from MailHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
woowabrie committed Oct 9, 2023
1 parent eeec842 commit 463c5d9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/apply/application/mail/MailHistoryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class MailHistoryService(

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

mailHistoryRepository.saveAll(mailHistories)
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/apply/config/DatabaseInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,15 @@ class DatabaseInitializer(
mailMessageRepository.save(mailMessage)

val mailHistories = listOf(
MailHistory.ofSuccess(
MailHistory(
mailMessage = mailMessage,
recipients = mailMessage.recipients.subList(0, 2)
recipients = mailMessage.recipients.subList(0, 2),
true
),
MailHistory.ofSuccess(
MailHistory(
mailMessage = mailMessage,
recipients = mailMessage.recipients.subList(3, 4)
recipients = mailMessage.recipients.subList(3, 4),
true
)
)
mailHistoryRepository.saveAll(mailHistories)
Expand Down
14 changes: 2 additions & 12 deletions src/main/kotlin/apply/domain/mail/MailHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import javax.persistence.Lob
import javax.persistence.ManyToOne

@Entity
class MailHistory private constructor(
class MailHistory(
@ManyToOne
@JoinColumn(nullable = false, foreignKey = ForeignKey(name = "fk_mail_history_to_mail_message"))
val mailMessage: MailMessage,
Expand All @@ -28,14 +28,4 @@ class MailHistory private constructor(
@Column(nullable = false)
val sentTime: LocalDateTime = LocalDateTime.now(),
id: Long = 0L
) : BaseEntity(id) {
companion object {
fun ofSuccess(mailMessage: MailMessage, recipients: List<String>): MailHistory {
return MailHistory(mailMessage, recipients, true)
}

fun ofFailure(mailMessage: MailMessage, recipients: List<String>): MailHistory {
return MailHistory(mailMessage, recipients, false)
}
}
}
) : BaseEntity(id)
4 changes: 2 additions & 2 deletions src/test/kotlin/apply/MailHistoryFixtures.kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ fun createMailReservation(
)
}

fun createSuccessMailHistory2(
fun createSuccessMailHistory(
subject: String = SUBJECT,
body: String = BODY,
sender: String = SENDER,
recipients: List<String> = RECIPIENTS
): MailHistory {
return MailHistory.ofSuccess(createMailMessage(subject, body, sender, recipients), recipients)
return MailHistory(createMailMessage(subject, body, sender, recipients), recipients, true)
}
6 changes: 3 additions & 3 deletions src/test/kotlin/apply/application/MailHistoryServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package apply.application

import apply.application.mail.MailHistoryService
import apply.createSuccessMailHistory2
import apply.createSuccessMailHistory
import apply.domain.mail.MailHistoryRepository
import apply.domain.mail.MailMessageRepository
import io.kotest.core.spec.style.BehaviorSpec
Expand All @@ -19,8 +19,8 @@ class MailHistoryServiceTest : BehaviorSpec({

Given("메일 이력이 있는 경우") {
every { mailHistoryRepository.findAll() } returns listOf(
createSuccessMailHistory2(subject = "제목1"),
createSuccessMailHistory2(subject = "제목2")
createSuccessMailHistory(subject = "제목1"),
createSuccessMailHistory(subject = "제목2")
)

When("모든 메일 이력을 조회하면") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class MailHistoryRepositoryTest(
val mailMessage = mailMessageRepository.save(createMailMessage())

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

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

expect("메일 발송 성공에 대한 히스토리를 저장한다") {
val actual = mailHistoryRepository.findAll()
Expand Down

0 comments on commit 463c5d9

Please sign in to comment.