Skip to content

Commit

Permalink
[Fix/#521] PostEmailTemplateEvent가 적용되지 않는 문제 해결 (#522)
Browse files Browse the repository at this point in the history
* refactor: PostEmailTemplateEvent 수정

* refactor: 수정 완료시 이벤트 발행 하도록 수정

* refactor: 수정과 저장 명시적으로 하도록 수정
  • Loading branch information
belljun3395 authored Jan 10, 2025
1 parent 0f4dd3e commit edd843e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,58 @@ class EmailTemplate(
)
}

fun isNewTemplate(): Boolean {
fun isNewTemplate(): Boolean = id == null

private fun registerModifyEvent() {
registerEvent(
PostEmailTemplateEvent(
templateId = this.id ?: 0,
templateId = this.id!!,
eventType = "POST",
),
)
return id == null
}

fun modifySubject(subject: String?): EmailTemplate {
subject?.let {
this.subject = it
fun modify(): EmailTemplateModifyBuilder = EmailTemplateModifyBuilder(this)

class EmailTemplateModifyBuilder(
private val template: EmailTemplate,
private var isVersionUpdated: Boolean = false,
) {
fun modifySubject(subject: String?): EmailTemplateModifyBuilder {
subject?.let {
template.subject = subject
}
return this
}
return this
}

fun modifyBody(
body: String,
variables: List<String>,
): EmailTemplate {
this.body = body
this.variables = variables
return this
}
fun modifyBody(
body: String,
variables: List<String>,
): EmailTemplateModifyBuilder {
template.body = body
template.variables = variables
return this
}

fun updateVersion(version: Float?): EmailTemplate {
version?.let {
if (it <= this.version) {
throw IllegalArgumentException("Invalid version: $it")
fun updateVersion(version: Float?): EmailTemplateModifyBuilder {
version?.let {
if (it <= template.version) {
throw IllegalArgumentException("Invalid version: $it")
}
this.template.version = it
} ?: kotlin.run {
this.template.version += 0.1f
}
this.version = it
} ?: kotlin.run {
this.version += 0.1f
isVersionUpdated = true
return this
}

registerEvent(
PostEmailTemplateEvent(
templateId = this.id!!,
eventType = "POST",
),
)
return this
fun done(): EmailTemplate {
if (!isVersionUpdated) {
updateVersion(null)
}
template.registerModifyEvent()
return template
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ class PostEmailTemplateEvent(
eventType: String,
eventTime: LocalDateTime = LocalDateTime.now(),
) : EmailTemplateTransactionAfterCompletionEvent(
eventType = "PostEvent",
eventId = eventId,
eventType = eventType,
eventTime = eventTime,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.few.crm.email.usecase

import com.few.crm.email.domain.EmailTemplate
import com.few.crm.email.domain.EmailTemplateHistory
import com.few.crm.email.repository.EmailTemplateHistoryRepository
import com.few.crm.email.repository.EmailTemplateRepository
import com.few.crm.email.service.HtmlValidator
import com.few.crm.email.usecase.dto.PostTemplateUseCaseIn
Expand All @@ -11,6 +13,7 @@ import org.springframework.stereotype.Service
@Service
class PostTemplateUseCase(
private val emailTemplateRepository: EmailTemplateRepository,
private val emailTemplateHistoryRepository: EmailTemplateHistoryRepository,
private val htmlValidator: HtmlValidator,
) {
@CrmTransactional
Expand Down Expand Up @@ -42,8 +45,10 @@ class PostTemplateUseCase(

val modifiedOrNewTemplate =
persistedTemplate
?.modify()
?.modifySubject(subject)
?.modifyBody(body, variables)
?.done()
?: run {
EmailTemplate.new(
templateName = templateName,
Expand All @@ -55,9 +60,19 @@ class PostTemplateUseCase(

modifiedOrNewTemplate.let { template ->
if (template.isNewTemplate()) {
emailTemplateRepository.save(template)
emailTemplateRepository.save(template).let {
emailTemplateHistoryRepository.save(
EmailTemplateHistory(
templateId = it.id!!,
subject = it.subject,
body = it.body,
variables = it.variables,
version = it.version,
),
)
}
} else {
template.updateVersion(version)
emailTemplateRepository.save(template)
}
}

Expand Down

0 comments on commit edd843e

Please sign in to comment.