Skip to content

Commit

Permalink
[Refactor/#525] 이벤트 라이브러리 개선 (#526)
Browse files Browse the repository at this point in the history
* refactor: Event#eventType 제거 및  EventUtils#generateEventPublishedTime 구현

* refactor: Event#eventType 제거 반영

* refactor: 상수 enum으로 변경

* fix: @CrmTransactional 추가되지 않은 부분 추가

* chore: MessagePayload#eventType 주석 수정

* refactor: readTree 방식으로 수정
  • Loading branch information
belljun3395 authored Jan 14, 2025
1 parent 47ea415 commit d4f85f4
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class EmailTemplate(
registerEvent(
PostEmailTemplateEvent(
templateId = this.id!!,
eventType = "POST",
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class SentEmail(
emailBody = emailBody,
messageId = emailMessageId,
destination = destination,
eventType = eventType.name,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
) : ScheduledEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long>,
eventId: String = EventUtils.generateEventId(),
eventType: String,
eventTime: LocalDateTime = LocalDateTime.now(),
expiredTime: LocalDateTime,
completed: Boolean = false,
eventPublisher: ApplicationEventPublisher,
) : TimeOutEvent(
eventId,
eventType,
eventTime,
expiredTime,
completed,
eventPublisher,
Expand All @@ -39,7 +37,6 @@ open class NotificationEmailSendTimeOutEvent(
templateId = templateId,
userIds = userIds,
expiredTime = expiredTime,
eventType = "TimeOutEvent",
eventPublisher = eventPublisher,
)
}
Expand All @@ -50,7 +47,6 @@ open class NotificationEmailSendTimeOutEvent(
templateId = templateId,
userIds = userIds,
timeOutEventId = eventId,
eventType = "ExpiredEvent",
)

fun isLongTermEvent(now: LocalDateTime): Boolean = expiredTime.isAfter(now)
Expand All @@ -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<Long>,
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<Long>,
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +15,14 @@ class EmailClickEventHandler(
) : EventHandler<EmailClickEvent> {
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}" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +10,7 @@ import org.springframework.stereotype.Component
class EmailDeliveryDelayEventHandler : EventHandler<EmailDeliveryDelayEvent> {
val logger = KotlinLogging.logger {}

@CrmTransactional
override fun handle(event: EmailDeliveryDelayEvent) {
logger.info { "Handling EmailDeliveryDelayEvent: $event" }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,14 +15,14 @@ class EmailDeliveryEventHandler(
) : EventHandler<EmailDeliveryEvent> {
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}" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +15,14 @@ class EmailOpenEventHandler(
) : EventHandler<EmailOpenEvent> {
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}" }
Expand Down
Loading

0 comments on commit d4f85f4

Please sign in to comment.