Skip to content

Commit

Permalink
refactor: readTree 방식으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jan 14, 2025
1 parent 65ef7e7 commit e13d560
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,14 +17,24 @@ import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.time.ZonedDateTime

fun ObjectMapper.readMail(message: String): Map<String, Any> =
(readValue(message) as Map<String, Any>).let { it["Message"] as String }.let { readValue(it) as Map<String, Any> }
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
Expand All @@ -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)
Expand Down

0 comments on commit e13d560

Please sign in to comment.