Skip to content

#134 - 바뀐 인텔리에 따라 스펙을 수정합니다. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wespot.message.v2

import com.wespot.message.dto.response.MessageV2StatusResponse
import com.wespot.message.port.`in`.MessageV2UsingStatusUseCase
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v2/messages")
class MessageV2UsingStatusController(
private val messageV2UsingStatusUseCase: MessageV2UsingStatusUseCase,
) {

@GetMapping("/status")
fun getMessageStatus(): ResponseEntity<MessageV2StatusResponse> {
val response = messageV2UsingStatusUseCase.getMessageStatus()

return ResponseEntity.ok(response)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.wespot.message.dto.response

import com.wespot.message.v2.MessageV2

data class MessageV2StatusResponse(
val isSendAllowed: Boolean,
val countRemainingMessages: Int,
val countUnReadMessages: Int,
val countUnReplayMessages: Int,
) {

companion object {

fun of(
isSendAllowed: Boolean,
countRemainingMessages: Int,
countUnReadMessages: Int,
countUnReplayMessages: Int,
): MessageV2StatusResponse {
return MessageV2StatusResponse(
isSendAllowed = isSendAllowed,
countRemainingMessages = countRemainingMessages,
countUnReadMessages = countUnReadMessages,
countUnReplayMessages = countUnReplayMessages,
)
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wespot.message.port.`in`

import com.wespot.message.dto.response.MessageV2StatusResponse

interface MessageV2UsingStatusUseCase {

fun getMessageStatus(): MessageV2StatusResponse

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.wespot.message.port.out

import com.wespot.message.v2.MessageV2
import java.time.LocalDate

interface MessageV2Port {

fun save(messageV2: MessageV2): MessageV2

fun countTodaySendMessages(userId: Long): Int
fun countTodaySendMessages(senderId: Long): Int

fun findAllMessageRoomBySenderId(senderId: Long): List<MessageV2>

Expand All @@ -24,4 +25,6 @@ interface MessageV2Port {

fun findAllMessageRoomBySenderIdAndReceiverId(senderId: Long, receiverId: Long): List<MessageV2>

fun findAllLastMessageOfRoomByReceiverIdAndFromDate(receiverId: Long, from: LocalDate): List<MessageV2>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.wespot.message.service.v2

import com.wespot.auth.service.SecurityUtils
import com.wespot.message.dto.response.MessageV2StatusResponse
import com.wespot.message.port.`in`.MessageV2UsingStatusUseCase
import com.wespot.message.port.out.MessageV2Port
import com.wespot.message.v2.MessageV2
import com.wespot.user.port.out.UserPort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate

@Service
class MessageV2UsingStatusService(
private val userPort: UserPort,
private val messageV2Port: MessageV2Port,
) : MessageV2UsingStatusUseCase {

@Transactional(readOnly = true)
override fun getMessageStatus(): MessageV2StatusResponse {
val loginUser = SecurityUtils.getLoginUser(userPort = userPort)
val today = LocalDate.now()
val yesterday = today.minusDays(1)
val messages =
messageV2Port.findAllLastMessageOfRoomByReceiverIdAndFromDate(
receiverId = loginUser.id,
from = yesterday
)
val countTodaySentMessage = messageV2Port.countTodaySendMessages(senderId = loginUser.id)

return MessageV2StatusResponse.of(
isSendAllowed = MessageV2.COUNT_OF_MAX_ABLE_TO_SEND_MESSAGE_PER_DAY > countTodaySentMessage,
countRemainingMessages = MessageV2.COUNT_OF_MAX_ABLE_TO_SEND_MESSAGE_PER_DAY - countTodaySentMessage,
countUnReadMessages = messages.filter { !it.isRead(viewer = loginUser) }.size,
countUnReplayMessages = messages.filter { it.isSentAtSameDate(date = today) }.size
)
}

}
7 changes: 6 additions & 1 deletion domain/src/main/kotlin/com/wespot/message/v2/MessageV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.wespot.user.event.UsedAnswerFeatureEvent
import com.wespot.user.message.AnonymousProfile
import org.hibernate.event.internal.EventUtil
import org.springframework.http.HttpStatus
import java.time.LocalDate
import java.time.LocalDateTime

data class MessageV2(
Expand Down Expand Up @@ -47,7 +48,7 @@ data class MessageV2(

companion object {

private const val COUNT_OF_MAX_ABLE_TO_SEND_MESSAGE_PER_DAY = 3
const val COUNT_OF_MAX_ABLE_TO_SEND_MESSAGE_PER_DAY = 3

fun createInitial(
content: String,
Expand Down Expand Up @@ -431,6 +432,10 @@ data class MessageV2(
isReceiverBlockedAt = if (isReceiverBlocked) LocalDateTime.now() else null
}

fun isSentAtSameDate(date: LocalDate): Boolean {
return date == createdAt.toLocalDate()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is MessageV2) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wespot.message.v2

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate
import java.time.LocalDateTime

interface MessageV2JpaRepository : JpaRepository<MessageJpaEntityV2, Long> {
Expand Down Expand Up @@ -37,4 +38,27 @@ interface MessageV2JpaRepository : JpaRepository<MessageJpaEntityV2, Long> {
receiverId: Long,
): List<MessageJpaEntityV2>

@Query(
"""
SELECT message
FROM MessageJpaEntityV2 message
WHERE message.receiverId = :receiverId
AND :from <= message.baseEntity.createdAt
AND message.baseEntity.createdAt = (
SELECT MAX(messageInSubquery.baseEntity.createdAt)
FROM MessageJpaEntityV2 messageInSubquery
WHERE :from <= messageInSubquery.baseEntity.createdAt
AND (
(message.messageRoomId IS NOT NULL AND message.messageRoomId = messageInSubquery.messageRoomId or message.messageRoomId = messageInSubquery.id)
OR
(message.messageRoomId IS NULL AND message.id = messageInSubquery.messageRoomId OR message.id = messageInSubquery.id)
)
)
"""
)
fun findAllLastMessageOfRoomByReceiverIdAndFromDate(
receiverId: Long,
from: LocalDate
): List<MessageJpaEntityV2>

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class MessageV2PersistenceAdapter(
return getCompleteMessageV2(messageRooms)
}

private fun getCompleteMessageV2(messageRooms: List<MessageJpaEntityV2>): List<MessageV2> {
val userIds: List<Long> = messageRooms.map { listOf(it.senderId, it.receiverId) }
private fun getCompleteMessageV2(messages: List<MessageJpaEntityV2>): List<MessageV2> {
val userIds: List<Long> = messages.map { listOf(it.senderId, it.receiverId) }
.flatMap { it.asSequence() }
.distinct()

Expand All @@ -60,14 +60,14 @@ class MessageV2PersistenceAdapter(
val schoolsMap = schoolJpaRepository.findAllByIdIn(schoolIds)
.associateBy { it.id }

val anonymousProfileIds: List<Long> = messageRooms.filter { it.anonymousProfileId != null }
val anonymousProfileIds: List<Long> = messages.filter { it.anonymousProfileId != null }
.map { it.anonymousProfileId!! }
.distinct()
val anonymousProfiles: Map<Long, AnonymousProfileJpaEntity> =
anonymousProfileJpaRepository.findByIdIn(anonymousProfileIds)
.associateBy { it.id }

return messageRooms
return messages
.filter { messageRoom -> usersMap[messageRoom.senderId] != null && usersMap[messageRoom.receiverId] != null }
.map { messageRoom ->
val sender = usersMap[messageRoom.senderId]!!
Expand Down Expand Up @@ -135,4 +135,14 @@ class MessageV2PersistenceAdapter(
return getCompleteMessageV2(messageRooms)
}

override fun findAllLastMessageOfRoomByReceiverIdAndFromDate(receiverId: Long, from: LocalDate): List<MessageV2> {
val messages =
messageV2JpaRepository.findAllLastMessageOfRoomByReceiverIdAndFromDate(
receiverId = receiverId,
from = from
)

return getCompleteMessageV2(messages = messages)
}

}