From e13d560bff99fa2d49116b30f3d445afc6771dd4 Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Tue, 14 Jan 2025 17:26:50 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20readTree=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aws/EmailSendSesMessageReverseRelay.kt | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) 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)