Skip to content

Commit

Permalink
fix: 🔧 Move the Date Field to the Payload in Spending Sharing Message (…
Browse files Browse the repository at this point in the history
…#235)

* fix: move date to payload

* chore: add jackson config to socket module
  • Loading branch information
psychology50 authored Feb 5, 2025
1 parent 60ec395 commit 96eed43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.co.pennyway.socket.config;

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration;

@Configuration
class JacksonConfig {
@Bean
fun objectMapper(): ObjectMapper {
return ObjectMapper().apply {
registerModule(JavaTimeModule())
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.amqp.rabbit.annotation.QueueBinding
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Component
import java.time.LocalDate

@Slf4j
@Component
Expand All @@ -27,6 +28,11 @@ class SpendingShareEventListener(
private val log = logger()
}

private data class Payload(
val date: LocalDate,
val spendingOnDates: List<SpendingChatShareEvent.SpendingOnDate>
)

@RabbitListener(
containerFactory = "simpleRabbitListenerContainerFactory",
bindings = [QueueBinding(
Expand All @@ -38,7 +44,7 @@ class SpendingShareEventListener(
fun handle(event: SpendingChatShareEvent) {
log.debug("handle: {}", event)

convertToJson(event.spendingOnDates())
convertToJson(Payload(event.date(), event.spendingOnDates()))
.getOrNull()
?.let { payload ->
chatMessageSendService.execute(
Expand All @@ -50,15 +56,15 @@ class SpendingShareEventListener(
event.senderId(),
event.name(),
null,
mapOf("Content-Type" to "application/json", "date" to event.date())
mapOf("Content-Type" to "application/json")
)
)
}
}

private fun convertToJson(spendingOnDates: List<SpendingChatShareEvent.SpendingOnDate>): Result<String> =
private fun convertToJson(payload: Payload): Result<String> =
runCatching {
objectMapper.writeValueAsString(spendingOnDates)
objectMapper.writeValueAsString(payload)
}.onFailure {
log.error("Failed to serialize spendingOnDates", it)
}
Expand Down

0 comments on commit 96eed43

Please sign in to comment.