From d4f85f47db57186a7c9464a3fde449c8d5467106 Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Tue, 14 Jan 2025 17:45:15 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor/#525]=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC=EB=A6=AC=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0=20(#526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: Event#eventType 제거 및 EventUtils#generateEventPublishedTime 구현 * refactor: Event#eventType 제거 반영 * refactor: 상수 enum으로 변경 * fix: @CrmTransactional 추가되지 않은 부분 추가 * chore: MessagePayload#eventType 주석 수정 * refactor: readTree 방식으로 수정 --- .../com/few/crm/email/domain/EmailTemplate.kt | 1 - .../com/few/crm/email/domain/SentEmail.kt | 1 - .../email/event/schedule/ScheduledEvent.kt | 20 +---- .../crm/email/event/send/EmailSendEvent.kt | 82 ++++--------------- .../send/NotificationEmailSendTimeOutEvent.kt | 33 ++------ .../send/handler/EmailClickEventHandler.kt | 5 +- .../handler/EmailDeliveryDelayEventHandler.kt | 2 + .../send/handler/EmailDeliveryEventHandler.kt | 8 +- .../send/handler/EmailOpenEventHandler.kt | 5 +- .../send/handler/EmailSentEventHandler.kt | 2 + ...otificationEmailSendTimeOutEventHandler.kt | 6 +- ...ationEmailSendTimeOutInvokeEventHandler.kt | 4 +- ...tificationEmailSendTimeOutEventReplayer.kt | 3 - .../template/EmailTemplateTransactionEvent.kt | 31 +------ .../relay/send/EmailSendEventMessageMapper.kt | 50 +++++------ .../aws/EmailSendSesMessageReverseRelay.kt | 37 +++++---- .../send/aws/ScheduledEventReverseRelay.kt | 1 - .../schedule/TimeOutEventTaskManager.kt | 1 - library/event/src/main/kotlin/event/Event.kt | 4 +- .../event/src/main/kotlin/event/EventUtils.kt | 9 ++ .../src/main/kotlin/event/TimeOutEvent.kt | 18 +--- .../kotlin/event/message/MessagePayload.kt | 2 +- .../test/kotlin/event/fixtures/TestEvent.kt | 12 +-- .../kotlin/event/fixtures/TestTimeOutEvent.kt | 14 ---- 24 files changed, 105 insertions(+), 246 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 253bf4af9..7ea64e9d3 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 @@ -58,7 +58,6 @@ class EmailTemplate( registerEvent( PostEmailTemplateEvent( templateId = this.id!!, - eventType = "POST", ), ) } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/domain/SentEmail.kt b/domain/crm/src/main/kotlin/com/few/crm/email/domain/SentEmail.kt index 071930fdf..082d8d2a8 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/domain/SentEmail.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/domain/SentEmail.kt @@ -19,7 +19,6 @@ class SentEmail( emailBody = emailBody, messageId = emailMessageId, destination = destination, - eventType = eventType.name, ), ) } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEvent.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEvent.kt index 0b1323b65..92ca66125 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEvent.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEvent.kt @@ -2,26 +2,10 @@ package com.few.crm.email.event.schedule import event.Event import event.EventDetails -import event.EventUtils -import java.time.LocalDateTime -abstract class ScheduledEvent( - eventId: String, - eventType: String, - eventTime: LocalDateTime, -) : Event( - eventId, - eventType, - eventTime, - ) +abstract class ScheduledEvent : Event() @EventDetails class CancelScheduledEvent( val targetEventId: String, - eventId: String = EventUtils.generateEventId(), - eventTime: LocalDateTime = LocalDateTime.now(), -) : ScheduledEvent( - eventId, - "CancelScheduledEvent", - eventTime, - ) \ No newline at end of file +) : ScheduledEvent() \ No newline at end of file diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/EmailSendEvent.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/EmailSendEvent.kt index b023576d6..ac553595d 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/EmailSendEvent.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/EmailSendEvent.kt @@ -2,128 +2,74 @@ package com.few.crm.email.event.send import event.Event import event.EventDetails -import event.EventUtils import java.time.LocalDateTime +enum class EmailSendStatus { + SENT, + OPEN, + DELIVERY, + CLICK, + DELIVERYDELAY, +} + abstract class EmailSendEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), val messageId: String, val destination: String, val timestamp: LocalDateTime, -) : Event( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, - ) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as EmailSendEvent - - if (messageId != other.messageId) return false - if (destination != other.destination) return false - if (timestamp != other.timestamp) return false - - return true - } - - override fun hashCode(): Int { - var result = messageId.hashCode() - result = 31 * result + destination.hashCode() - result = 31 * result + timestamp.hashCode() - return result - } - - override fun toString(): String = "EmailSendEvent(messageId='$messageId', destination='$destination', timestamp=$timestamp)" -} +) : Event() -@EventDetails( - outBox = false, -) +@EventDetails class EmailSentEvent( val userExternalId: String, val emailBody: String, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), messageId: String, destination: String, timestamp: LocalDateTime = LocalDateTime.now(), ) : EmailSendEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, messageId = messageId, destination = destination, timestamp = timestamp, ) -@EventDetails(outBox = false) +@EventDetails class EmailDeliveryEvent( - eventId: String, - eventType: String, - eventTime: LocalDateTime, messageId: String, destination: String, timestamp: LocalDateTime, ) : EmailSendEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, messageId = messageId, destination = destination, timestamp = timestamp, ) -@EventDetails(outBox = false) +@EventDetails class EmailOpenEvent( - eventId: String, - eventType: String, - eventTime: LocalDateTime, messageId: String, destination: String, timestamp: LocalDateTime, ) : EmailSendEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, messageId = messageId, destination = destination, timestamp = timestamp, ) -@EventDetails(outBox = false) +@EventDetails class EmailClickEvent( - eventId: String, - eventType: String, - eventTime: LocalDateTime, messageId: String, destination: String, timestamp: LocalDateTime, ) : EmailSendEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, messageId = messageId, destination = destination, timestamp = timestamp, ) -@EventDetails(outBox = false) +@EventDetails class EmailDeliveryDelayEvent( - eventId: String, - eventType: String, - eventTime: LocalDateTime, messageId: String, destination: String, timestamp: LocalDateTime, ) : EmailSendEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, messageId = messageId, destination = destination, timestamp = timestamp, diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/NotificationEmailSendTimeOutEvent.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/NotificationEmailSendTimeOutEvent.kt index 5a3e4cf05..19eb2a7c8 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/NotificationEmailSendTimeOutEvent.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/NotificationEmailSendTimeOutEvent.kt @@ -1,26 +1,24 @@ package com.few.crm.email.event.send import event.EventDetails -import event.EventUtils import event.TimeExpiredEvent import event.TimeOutEvent import org.springframework.context.ApplicationEventPublisher import java.time.LocalDateTime +enum class NotificationEmailSendTimeOutEventType { + AWS, + APP, +} + @EventDetails open class NotificationEmailSendTimeOutEvent( val templateId: Long, val userIds: List, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), expiredTime: LocalDateTime, completed: Boolean = false, eventPublisher: ApplicationEventPublisher, ) : TimeOutEvent( - eventId, - eventType, - eventTime, expiredTime, completed, eventPublisher, @@ -39,7 +37,6 @@ open class NotificationEmailSendTimeOutEvent( templateId = templateId, userIds = userIds, expiredTime = expiredTime, - eventType = "TimeOutEvent", eventPublisher = eventPublisher, ) } @@ -50,7 +47,6 @@ open class NotificationEmailSendTimeOutEvent( templateId = templateId, userIds = userIds, timeOutEventId = eventId, - eventType = "ExpiredEvent", ) fun isLongTermEvent(now: LocalDateTime): Boolean = expiredTime.isAfter(now) @@ -59,46 +55,31 @@ open class NotificationEmailSendTimeOutEvent( AwsNotificationEmailSendTimeOutEvent( templateId = templateId, userIds = userIds, - eventId = eventId, - eventType = eventType, - eventTime = eventTime, expiredTime = expiredTime, completed = completed, eventPublisher = eventPublisher, ) } -@EventDetails() +@EventDetails class NotificationEmailSendTimeOutInvokeEvent( val templateId: Long, val userIds: List, timeOutEventId: String, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), ) : TimeExpiredEvent( timeOutEventId, - eventId, - eventType, - eventTime, ) -@EventDetails() +@EventDetails class AwsNotificationEmailSendTimeOutEvent( templateId: Long, userIds: List, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), expiredTime: LocalDateTime, completed: Boolean = false, eventPublisher: ApplicationEventPublisher, ) : NotificationEmailSendTimeOutEvent( templateId, userIds, - eventId, - eventType, - eventTime, expiredTime, completed, eventPublisher, diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailClickEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailClickEventHandler.kt index 8c730b2ab..37d474397 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailClickEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailClickEventHandler.kt @@ -1,7 +1,9 @@ package com.few.crm.email.event.send.handler import com.few.crm.email.event.send.EmailClickEvent +import com.few.crm.email.event.send.EmailSendStatus import com.few.crm.email.repository.EmailSendHistoryRepository +import com.few.crm.support.jpa.CrmTransactional import event.EventHandler import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component @@ -13,13 +15,14 @@ class EmailClickEventHandler( ) : EventHandler { val logger = KotlinLogging.logger {} + @CrmTransactional override fun handle(event: EmailClickEvent) { logger.info { "Handling EmailClickEvent: $event" } // TODO check emailSendHistory and update status if history is not found retry 3 times emailSendHistoryRepository .findByEmailMessageId(event.messageId) ?.let { - it.sendStatus = event.eventType.uppercase(Locale.getDefault()) + it.sendStatus = EmailSendStatus.CLICK.name emailSendHistoryRepository.save(it) } ?: logger.error { "EmailSendHistory not found for messageId: ${event.messageId}" } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryDelayEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryDelayEventHandler.kt index 6af40b68c..f922a4df3 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryDelayEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryDelayEventHandler.kt @@ -1,6 +1,7 @@ package com.few.crm.email.event.send.handler import com.few.crm.email.event.send.EmailDeliveryDelayEvent +import com.few.crm.support.jpa.CrmTransactional import event.EventHandler import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component @@ -9,6 +10,7 @@ import org.springframework.stereotype.Component class EmailDeliveryDelayEventHandler : EventHandler { val logger = KotlinLogging.logger {} + @CrmTransactional override fun handle(event: EmailDeliveryDelayEvent) { logger.info { "Handling EmailDeliveryDelayEvent: $event" } } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryEventHandler.kt index 72383dd02..5fd3f4d7f 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailDeliveryEventHandler.kt @@ -1,12 +1,12 @@ package com.few.crm.email.event.send.handler import com.few.crm.email.event.send.EmailDeliveryEvent +import com.few.crm.email.event.send.EmailSendStatus import com.few.crm.email.repository.EmailSendHistoryRepository +import com.few.crm.support.jpa.CrmTransactional import event.EventHandler import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component -import org.springframework.transaction.annotation.Propagation -import org.springframework.transaction.annotation.Transactional import java.util.* @Component @@ -15,14 +15,14 @@ class EmailDeliveryEventHandler( ) : EventHandler { val log = KotlinLogging.logger {} - @Transactional(propagation = Propagation.REQUIRES_NEW) + @CrmTransactional override fun handle(event: EmailDeliveryEvent) { log.info { "Handling EmailDeliveryEvent: $event" } // TODO check emailSendHistory and update status if history is not found retry 3 times emailSendHistoryRepository .findByEmailMessageId(event.messageId) ?.let { - it.sendStatus = event.eventType.uppercase(Locale.getDefault()) + it.sendStatus = EmailSendStatus.DELIVERY.name emailSendHistoryRepository.save(it) } ?: log.error { "EmailSendHistory not found for messageId: ${event.messageId}" } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailOpenEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailOpenEventHandler.kt index 6410db8f7..c025381f3 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailOpenEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailOpenEventHandler.kt @@ -1,7 +1,9 @@ package com.few.crm.email.event.send.handler import com.few.crm.email.event.send.EmailOpenEvent +import com.few.crm.email.event.send.EmailSendStatus import com.few.crm.email.repository.EmailSendHistoryRepository +import com.few.crm.support.jpa.CrmTransactional import event.EventHandler import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component @@ -13,13 +15,14 @@ class EmailOpenEventHandler( ) : EventHandler { val logger = KotlinLogging.logger {} + @CrmTransactional override fun handle(event: EmailOpenEvent) { logger.info { "Handling EmailOpenEvent: $event" } // TODO check emailSendHistory and update status if history is not found retry 3 times emailSendHistoryRepository .findByEmailMessageId(event.messageId) ?.let { - it.sendStatus = event.eventType.uppercase(Locale.getDefault()) + it.sendStatus = EmailSendStatus.OPEN.name emailSendHistoryRepository.save(it) } ?: logger.error { "EmailSendHistory not found for messageId: ${event.messageId}" } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailSentEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailSentEventHandler.kt index 9c536a04c..f07695820 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailSentEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/EmailSentEventHandler.kt @@ -4,6 +4,7 @@ import com.few.crm.email.domain.EmailSendEventType import com.few.crm.email.domain.EmailSendHistory import com.few.crm.email.event.send.EmailSentEvent import com.few.crm.email.repository.EmailSendHistoryRepository +import com.few.crm.support.jpa.CrmTransactional import event.EventHandler import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component @@ -14,6 +15,7 @@ class EmailSentEventHandler( ) : EventHandler { val logger = KotlinLogging.logger {} + @CrmTransactional override fun handle(event: EmailSentEvent) { logger.info { "Handling EmailSentEvent: $event" } emailSendHistoryRepository.save( diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutEventHandler.kt index 0d2b18d90..636a307d8 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutEventHandler.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.few.crm.email.domain.ScheduledEvent import com.few.crm.email.event.send.AwsNotificationEmailSendTimeOutEvent import com.few.crm.email.event.send.NotificationEmailSendTimeOutEvent +import com.few.crm.email.event.send.NotificationEmailSendTimeOutEventType import com.few.crm.email.repository.ScheduledEventRepository import com.few.crm.support.jpa.CrmTransactional import event.EventHandler @@ -18,10 +19,11 @@ class NotificationEmailSendTimeOutEventHandler( override fun handle(event: NotificationEmailSendTimeOutEvent) { val scheduledAt = if (event is AwsNotificationEmailSendTimeOutEvent) { - "AWS" + NotificationEmailSendTimeOutEventType.AWS.name } else { - "APP" + NotificationEmailSendTimeOutEventType.APP.name } + scheduledEventRepository.save( ScheduledEvent( eventId = event.eventId, diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutInvokeEventHandler.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutInvokeEventHandler.kt index 6c7fcf480..ccc6f3823 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutInvokeEventHandler.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/handler/NotificationEmailSendTimeOutInvokeEventHandler.kt @@ -1,7 +1,6 @@ package com.few.crm.email.event.send.handler import com.fasterxml.jackson.databind.ObjectMapper -import com.few.crm.email.domain.EmailSendEventType import com.few.crm.email.event.send.EmailSentEvent import com.few.crm.email.event.send.NotificationEmailSendTimeOutInvokeEvent import com.few.crm.email.repository.EmailTemplateRepository @@ -30,6 +29,7 @@ class NotificationEmailSendTimeOutInvokeEventHandler( scheduledEventRepository .findByEventIdAndCompletedFalseForUpdate(event.timeOutEventId) ?.complete() ?: return + val templateId = event.templateId val userIds = event.userIds val template = @@ -49,13 +49,13 @@ class NotificationEmailSendTimeOutInvokeEventHandler( content = NonContent(), ), ) + applicationEventPublisher.publishEvent( EmailSentEvent( userExternalId = user.externalId!!, emailBody = template.body, destination = email, messageId = emailMessageId, - eventType = EmailSendEventType.SEND.name, ), ) } diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/replayer/NotificationEmailSendTimeOutEventReplayer.kt b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/replayer/NotificationEmailSendTimeOutEventReplayer.kt index 9bbbb17b8..5af6d9273 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/event/send/replayer/NotificationEmailSendTimeOutEventReplayer.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/event/send/replayer/NotificationEmailSendTimeOutEventReplayer.kt @@ -62,9 +62,6 @@ class NotificationEmailSendTimeOutEventReplayer( NotificationEmailSendTimeOutEvent( templateId = eventJson.templateId(), userIds = eventJson.userIds(), - eventId = it.eventId, - eventType = it.eventClass, - eventTime = eventJson.eventTime(), expiredTime = eventJson.expiredTime(), completed = it.completed, eventPublisher = applicationEventPublisher, 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 846f56f45..55d67f964 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 @@ -2,37 +2,12 @@ package com.few.crm.email.event.template import event.Event import event.EventDetails -import event.EventUtils -import java.time.LocalDateTime -abstract class EmailTemplateTransactionEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), -) : Event( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, - ) +abstract class EmailTemplateTransactionEvent : Event() -abstract class EmailTemplateTransactionAfterCompletionEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), -) : EmailTemplateTransactionEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, - ) +abstract class EmailTemplateTransactionAfterCompletionEvent : EmailTemplateTransactionEvent() @EventDetails class PostEmailTemplateEvent( val templateId: Long, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), -) : EmailTemplateTransactionAfterCompletionEvent( - eventId = eventId, - eventType = eventType, - eventTime = eventTime, - ) \ No newline at end of file +) : EmailTemplateTransactionAfterCompletionEvent() \ No newline at end of file diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/EmailSendEventMessageMapper.kt b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/EmailSendEventMessageMapper.kt index 52f6f6a6e..912e7c908 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/EmailSendEventMessageMapper.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/EmailSendEventMessageMapper.kt @@ -7,54 +7,48 @@ import org.springframework.stereotype.Component import java.time.LocalDateTime import java.util.* +fun Map.messageId() = this["messageId"] as String + +fun Map.destination() = this["destination"] as String + +fun Map.timestamp() = this["timestamp"] as LocalDateTime + @Component class EmailSendEventMessageMapper : MessageMapper() { override fun map(event: EmailSendEvent): Optional = Optional.empty() fun to(messagePayload: MessagePayload): Optional = when (messagePayload.eventType!!.lowercase(Locale.getDefault())) { - "open" -> + EmailSendStatus.OPEN.name.lowercase() -> Optional.of( EmailOpenEvent( - eventId = messagePayload.eventId!!, - eventType = messagePayload.eventType!!, - eventTime = messagePayload.eventTime!!, - messageId = messagePayload.data!!["messageId"] as String, - destination = messagePayload.data!!["destination"] as String, - timestamp = messagePayload.data!!["timestamp"] as LocalDateTime, + messageId = messagePayload.data!!.messageId(), + destination = messagePayload.data!!.destination(), + timestamp = messagePayload.data!!.timestamp(), ), ) - "delivery" -> + EmailSendStatus.DELIVERY.name.lowercase() -> Optional.of( EmailDeliveryEvent( - eventId = messagePayload.eventId!!, - eventType = messagePayload.eventType!!, - eventTime = messagePayload.eventTime!!, - messageId = messagePayload.data!!["messageId"] as String, - destination = messagePayload.data!!["destination"] as String, - timestamp = messagePayload.data!!["timestamp"] as LocalDateTime, + messageId = messagePayload.data!!.messageId(), + destination = messagePayload.data!!.destination(), + timestamp = messagePayload.data!!.timestamp(), ), ) - "click" -> + EmailSendStatus.CLICK.name.lowercase() -> Optional.of( EmailClickEvent( - eventId = messagePayload.eventId!!, - eventType = messagePayload.eventType!!, - eventTime = messagePayload.eventTime!!, - messageId = messagePayload.data!!["messageId"] as String, - destination = messagePayload.data!!["destination"] as String, - timestamp = messagePayload.data!!["timestamp"] as LocalDateTime, + messageId = messagePayload.data!!.messageId(), + destination = messagePayload.data!!.destination(), + timestamp = messagePayload.data!!.timestamp(), ), ) - "deliverydelay" -> + EmailSendStatus.DELIVERYDELAY.name.lowercase() -> Optional.of( EmailDeliveryDelayEvent( - eventId = messagePayload.eventId!!, - eventType = messagePayload.eventType!!, - eventTime = messagePayload.eventTime!!, - messageId = messagePayload.data!!["messageId"] as String, - destination = messagePayload.data!!["destination"] as String, - timestamp = messagePayload.data!!["timestamp"] as LocalDateTime, + messageId = messagePayload.data!!.messageId(), + destination = messagePayload.data!!.destination(), + timestamp = messagePayload.data!!.timestamp(), ), ) else -> Optional.empty() diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/EmailSendSesMessageReverseRelay.kt b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/EmailSendSesMessageReverseRelay.kt index 586c37ffc..75fe6c1ad 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/EmailSendSesMessageReverseRelay.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/EmailSendSesMessageReverseRelay.kt @@ -1,7 +1,7 @@ package com.few.crm.email.relay.send.aws +import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue import com.few.crm.config.CrmSqsConfig.Companion.SQS_LISTENER_CONTAINER_FACTORY import com.few.crm.email.event.send.EmailSendEvent import com.few.crm.email.relay.send.EmailSendEventMessageMapper @@ -17,14 +17,24 @@ import org.springframework.stereotype.Service import java.time.LocalDateTime import java.time.ZonedDateTime -fun ObjectMapper.readMail(message: String): Map = - (readValue(message) as Map).let { it["Message"] as String }.let { readValue(it) as Map } +fun ObjectMapper.readMessage(message: String): JsonNode = readTree(message)["Message"]!! -fun Map<*, *>.messageId() = this["messageId"] as String +fun JsonNode.mail(): JsonNode = this["mail"] -fun Map<*, *>.timestamp(): LocalDateTime = (this["timestamp"] as String).let { ZonedDateTime.parse(it).toLocalDateTime() } +fun JsonNode.eventType(): String = this["eventType"].asText() -fun Map<*, *>.destination() = (this["destination"] as List<*>).joinToString(", ") +fun JsonNode.messageId(): String = this["messageId"].asText() + +fun JsonNode.timestamp(): LocalDateTime = (this["timestamp"].asText()).let { ZonedDateTime.parse(it).toLocalDateTime() } + +fun JsonNode.destination() = (this["destination"] as List<*>).joinToString(", ") + +fun JsonNode.data() = + mapOf( + "messageId" to messageId(), + "destination" to destination(), + "timestamp" to timestamp(), + ) @Profile("!local") @Service @@ -41,19 +51,14 @@ class EmailSendSesMessageReverseRelay( acknowledgement: Acknowledgement, ) { objectMapper - .readMail(message) + .readMessage(message) .let { - val mail = it["mail"] as Map<*, *> + val mail = it.mail() MessagePayload( eventId = EventUtils.generateEventId(), - eventType = it["eventType"] as String, - eventTime = LocalDateTime.now(), - data = - mapOf( - "messageId" to mail.messageId(), - "destination" to mail.destination(), - "timestamp" to mail.timestamp(), - ), + eventType = it.eventType(), + eventTime = EventUtils.generateEventPublishedTime(), + data = mail.data(), ) }.let { eventMessageMapper.to(it) diff --git a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/ScheduledEventReverseRelay.kt b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/ScheduledEventReverseRelay.kt index f52dee731..f2b19c8c6 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/ScheduledEventReverseRelay.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/email/relay/send/aws/ScheduledEventReverseRelay.kt @@ -35,7 +35,6 @@ class ScheduledEventReverseRelay( templateId = templateId, userIds = userIds, timeOutEventId = eventTime, - eventType = "AWSINVOKE", ).let { event -> publish(event) } diff --git a/domain/crm/src/main/kotlin/com/few/crm/support/schedule/TimeOutEventTaskManager.kt b/domain/crm/src/main/kotlin/com/few/crm/support/schedule/TimeOutEventTaskManager.kt index 71ab1c473..f5f71efc4 100644 --- a/domain/crm/src/main/kotlin/com/few/crm/support/schedule/TimeOutEventTaskManager.kt +++ b/domain/crm/src/main/kotlin/com/few/crm/support/schedule/TimeOutEventTaskManager.kt @@ -88,7 +88,6 @@ class TimeOutEventTaskManager( "templateId" to event.templateId, "userIds" to event.userIds, "eventId" to event.eventId, - "eventType" to event.eventType, "eventTime" to event.eventTime, "expiredTime" to event.expiredTime, "completed" to event.completed, diff --git a/library/event/src/main/kotlin/event/Event.kt b/library/event/src/main/kotlin/event/Event.kt index f1b7674ee..c9e4ea020 100644 --- a/library/event/src/main/kotlin/event/Event.kt +++ b/library/event/src/main/kotlin/event/Event.kt @@ -8,11 +8,9 @@ import java.time.LocalDateTime * 이벤트 * * @property eventId 이벤트 식별자 - * @property eventType 이벤트 행위 타입 * @property eventTime 이벤트 발행 시간 (기본값: 현재 시간) */ abstract class Event( val eventId: String = EventUtils.generateEventId(), - val eventType: String, - val eventTime: LocalDateTime = LocalDateTime.now(), + val eventTime: LocalDateTime = EventUtils.generateEventPublishedTime(), ) \ No newline at end of file diff --git a/library/event/src/main/kotlin/event/EventUtils.kt b/library/event/src/main/kotlin/event/EventUtils.kt index 941afbb49..1aa0d0d13 100644 --- a/library/event/src/main/kotlin/event/EventUtils.kt +++ b/library/event/src/main/kotlin/event/EventUtils.kt @@ -1,5 +1,6 @@ package event +import java.time.LocalDateTime import java.util.* /** @@ -22,5 +23,13 @@ class EventUtils { * @return 이벤트 ID */ fun generateEventId(): String = UUID.randomUUID().toString() + + /** + * Generate event time + * + * 이벤트 발행 시간을 생성합니다. + * @return + */ + fun generateEventPublishedTime(): LocalDateTime = LocalDateTime.now() } } \ No newline at end of file diff --git a/library/event/src/main/kotlin/event/TimeOutEvent.kt b/library/event/src/main/kotlin/event/TimeOutEvent.kt index 4b42740a9..394336178 100644 --- a/library/event/src/main/kotlin/event/TimeOutEvent.kt +++ b/library/event/src/main/kotlin/event/TimeOutEvent.kt @@ -15,17 +15,10 @@ import java.time.LocalDateTime * @see TimeExpiredEvent 시간 만료 이벤트 */ abstract class TimeOutEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), val expiredTime: LocalDateTime, var completed: Boolean = false, protected val eventPublisher: ApplicationEventPublisher, -) : Event( - eventId, - eventType, - eventTime, - ), +) : Event(), Runnable { /** * Complete event @@ -75,11 +68,4 @@ abstract class TimeOutEvent( */ abstract class TimeExpiredEvent( val timeOutEventId: String, - eventId: String, - eventType: String, - eventTime: LocalDateTime, -) : Event( - eventId, - eventType, - eventTime, - ) \ No newline at end of file +) : Event() \ No newline at end of file diff --git a/library/event/src/main/kotlin/event/message/MessagePayload.kt b/library/event/src/main/kotlin/event/message/MessagePayload.kt index 03fdd01c7..1b2daef1b 100644 --- a/library/event/src/main/kotlin/event/message/MessagePayload.kt +++ b/library/event/src/main/kotlin/event/message/MessagePayload.kt @@ -10,7 +10,7 @@ import java.time.LocalDateTime * 메시지 페이로드 * * @property eventId 이벤트 식별자 - * @property eventType 이벤트 행위 타입 + * @property eventType 이벤트 타입 * @property eventTime 이벤트 발행 시간 * @property data 이벤트 데이터 * diff --git a/library/event/src/test/kotlin/event/fixtures/TestEvent.kt b/library/event/src/test/kotlin/event/fixtures/TestEvent.kt index 4a7ebcf47..4cd4bf36b 100644 --- a/library/event/src/test/kotlin/event/fixtures/TestEvent.kt +++ b/library/event/src/test/kotlin/event/fixtures/TestEvent.kt @@ -2,16 +2,6 @@ package event.fixtures import event.Event import event.EventDetails -import event.EventUtils -import java.time.LocalDateTime @EventDetails(outBox = true) -class TestEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), -) : Event( - eventId, - eventType, - eventTime, - ) \ No newline at end of file +class TestEvent : Event() \ No newline at end of file diff --git a/library/event/src/test/kotlin/event/fixtures/TestTimeOutEvent.kt b/library/event/src/test/kotlin/event/fixtures/TestTimeOutEvent.kt index 20308f6b6..5bfa8cb2d 100644 --- a/library/event/src/test/kotlin/event/fixtures/TestTimeOutEvent.kt +++ b/library/event/src/test/kotlin/event/fixtures/TestTimeOutEvent.kt @@ -1,7 +1,6 @@ package event.fixtures import event.EventDetails -import event.EventUtils import event.TimeExpiredEvent import event.TimeOutEvent import org.springframework.context.ApplicationEventPublisher @@ -9,16 +8,10 @@ import java.time.LocalDateTime @EventDetails(outBox = false) class TestTimeOutEvent( - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), expiredTime: LocalDateTime = LocalDateTime.now(), completed: Boolean = false, eventPublisher: ApplicationEventPublisher, ) : TimeOutEvent( - eventId, - eventType, - eventTime, expiredTime, completed, eventPublisher, @@ -26,19 +19,12 @@ class TestTimeOutEvent( override fun timeExpiredEvent(): TimeExpiredEvent = TestTimeExpiredEvent( timeOutEventId = eventId, - eventType = "ExpiredEvent", ) } @EventDetails(outBox = false) class TestTimeExpiredEvent( timeOutEventId: String, - eventId: String = EventUtils.generateEventId(), - eventType: String, - eventTime: LocalDateTime = LocalDateTime.now(), ) : TimeExpiredEvent( timeOutEventId, - eventId, - eventType, - eventTime, ) \ No newline at end of file