From edd843e59e8d253636e406e62f3394b9d30625e5 Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Fri, 10 Jan 2025 22:04:18 +0900 Subject: [PATCH] =?UTF-8?q?[Fix/#521]=20PostEmailTemplateEvent=EA=B0=80=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0=20(#522)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: PostEmailTemplateEvent 수정 * refactor: 수정 완료시 이벤트 발행 하도록 수정 * refactor: 수정과 저장 명시적으로 하도록 수정 --- .../com/few/crm/email/domain/EmailTemplate.kt | 70 +++++++++++-------- .../template/EmailTemplateTransactionEvent.kt | 4 +- .../crm/email/usecase/PostTemplateUseCase.kt | 19 ++++- 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/domain/EmailTemplate.kt b/domain/crm/src/main/kotlin/com/few/crm/email/domain/EmailTemplate.kt index d6ac35342..253bf4af9 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/domain/EmailTemplate.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/domain/EmailTemplate.kt @@ -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, - ): EmailTemplate { - this.body = body - this.variables = variables - return this - } + fun modifyBody( + body: String, + variables: List, + ): 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 + } } } \ No newline at end of file diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/template/EmailTemplateTransactionEvent.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/template/EmailTemplateTransactionEvent.kt index 2cef92e86..539c3aada 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/template/EmailTemplateTransactionEvent.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/template/EmailTemplateTransactionEvent.kt @@ -32,5 +32,7 @@ class PostEmailTemplateEvent( eventType: String, eventTime: LocalDateTime = LocalDateTime.now(), ) : EmailTemplateTransactionAfterCompletionEvent( - eventType = "PostEvent", + eventId = eventId, + eventType = eventType, + eventTime = eventTime, ) \ No newline at end of file diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/usecase/PostTemplateUseCase.kt b/domain/crm/src/main/kotlin/com/few/crm/email/usecase/PostTemplateUseCase.kt index 5a2aca7f2..afffb64b2 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/usecase/PostTemplateUseCase.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/usecase/PostTemplateUseCase.kt @@ -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 @@ -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 @@ -42,8 +45,10 @@ class PostTemplateUseCase( val modifiedOrNewTemplate = persistedTemplate + ?.modify() ?.modifySubject(subject) ?.modifyBody(body, variables) + ?.done() ?: run { EmailTemplate.new( templateName = templateName, @@ -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) } }