Skip to content
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
2 changes: 2 additions & 0 deletions docs/notification-inbox-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
| 목록 | `isBundled` | 상세 카드가 둘 이상인지 여부 |
| 목록 | `cards` | 발송에 포함된 소모품별 상세 카드 |

카드의 `iconUrl`은 스냅샷이 아니라 조회 시점의 소모품 카테고리 아이콘이다. 공지이거나 카테고리가 삭제되면 null이다.

라벨과 교체 예상일은 발송 당시 스냅샷이다. 이후 소모품을 교체하거나 수정해도 과거 알림 내용은 변하지 않는다.
묶음·공지·기존 알림의 상위 `label`, `nextReplacementDate`, `itemId`는 기존 규칙을 유지한다.
신규 알림은 발송에 포함된 후보마다 `cards`에 타입, 라벨, 여분 부족 여부, 소모품 ID와 딥링크를 내려준다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class ItemService(
.associate { (it[0] as Long) to CategoryItemStats((it[1] as Long).toInt(), (it[2] as Long).toInt()) }
}

/** 과거 알림 카드의 아이콘을 위해 삭제된 소모품도 포함한다. */
@Transactional(readOnly = true)
fun findCategoryIdsByItemIds(userId: Long, itemIds: Collection<Long>): Map<Long, Long> {
if (itemIds.isEmpty()) return emptyMap()
return itemRepository.findAllById(itemIds.toSet())
.filter { it.userId == userId }
.associate { requireNotNull(it.id) to it.categoryId }
}

@Transactional(readOnly = true)
fun listItems(userId: Long): List<ItemResponse> {
val items = itemRepository.findActiveByUserId(userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ data class NotificationCardResponse(
@field:Schema(description = "발송 당시 소모품 이름. 공지는 null")
val itemName: String?,

@field:Schema(description = "소모품 카테고리 아이콘 URL. 발송 당시가 아닌 현재 카테고리 기준. 공지이거나 카테고리가 삭제되면 null")
val iconUrl: String?,

@field:Schema(description = "발송 당시 라벨. 여분 부족이 겹치면 ' · 여분 부족'이 붙음. 공지는 null", example = "교체 D+1 · 여분 부족")
val label: String?,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package depromeet.hotsix.obrit.notification.service

import depromeet.hotsix.obrit.category.service.CategoryQueryService
import depromeet.hotsix.obrit.global.exception.ResourceNotFoundException
import depromeet.hotsix.obrit.item.service.ItemService
import depromeet.hotsix.obrit.notification.dto.response.ListNotificationResponse
import depromeet.hotsix.obrit.notification.dto.response.MarkReadNotificationResponse
import depromeet.hotsix.obrit.notification.dto.response.NotificationCardResponse
Expand All @@ -16,19 +18,21 @@ class NotificationService(
private val notificationRepository: NotificationRepository,
private val notificationEntryRepository: NotificationEntryRepository,
private val notificationDeepLinkService: NotificationDeepLinkService,
private val itemService: ItemService,
private val categoryQueryService: CategoryQueryService,
) {

@Transactional(readOnly = true)
fun listAllNotification(userId: Long): List<ListNotificationResponse> {
val notifications = notificationRepository.findAllByUserIdOrderByCreatedAtDesc(userId)
if (notifications.isEmpty()) return emptyList()

val entriesByNotificationId = notificationEntryRepository
.findAllByNotificationIds(notifications.map { requireNotNull(it.id) })
.groupBy { requireNotNull(it.notification.id) }
val entries = notificationEntryRepository.findAllByNotificationIds(notifications.map { requireNotNull(it.id) })
val entriesByNotificationId = entries.groupBy { requireNotNull(it.notification.id) }
val iconUrlByItemId = findIconUrlsByItemId(userId, entries.mapNotNull { it.itemId })

return notifications.map {
val cards = entriesByNotificationId[it.id].orEmpty().map(::toCardResponse)
val cards = entriesByNotificationId[it.id].orEmpty().map { entry -> toCardResponse(entry, iconUrlByItemId) }
ListNotificationResponse(
id = it.id!!,
title = it.title,
Expand Down Expand Up @@ -66,12 +70,21 @@ class NotificationService(
notificationRepository.markAllAsReadByUserId(userId, LocalDateTime.now())
}

private fun toCardResponse(entry: NotificationEntry) = NotificationCardResponse(
private fun findIconUrlsByItemId(userId: Long, itemIds: Collection<Long>): Map<Long, String> {
val categoryIdByItemId = itemService.findCategoryIdsByItemIds(userId, itemIds)
val iconUrlByCategoryId = categoryQueryService.findVisibleCategoryIconUrls(userId, categoryIdByItemId.values)
return categoryIdByItemId.mapNotNull { (itemId, categoryId) ->
iconUrlByCategoryId[categoryId]?.let { itemId to it }
}.toMap()
}

private fun toCardResponse(entry: NotificationEntry, iconUrlByItemId: Map<Long, String>) = NotificationCardResponse(
entryId = requireNotNull(entry.id),
type = entry.type,
title = entry.title,
content = entry.body,
itemName = entry.itemName,
iconUrl = entry.itemId?.let { iconUrlByItemId[it] },
label = entry.label,
hasLowStock = entry.hasLowStock,
nextReplacementDate = entry.nextReplacementDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package depromeet.hotsix.obrit.notification.service

import depromeet.hotsix.obrit.category.entity.CategoryIcon
import depromeet.hotsix.obrit.category.repository.CategoryFixture
import depromeet.hotsix.obrit.category.repository.CategoryIconRepository
import depromeet.hotsix.obrit.category.repository.CategoryRepository
import depromeet.hotsix.obrit.global.common.storage.UrlResolver
import depromeet.hotsix.obrit.global.exception.ResourceNotFoundException
import depromeet.hotsix.obrit.item.entity.Item
import depromeet.hotsix.obrit.item.repository.ItemRepository
Expand All @@ -13,6 +18,7 @@ import depromeet.hotsix.obrit.user.repository.UserRepository
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.transaction.annotation.Transactional
Expand Down Expand Up @@ -47,6 +53,16 @@ class NotificationDispatchServiceTest {
@Autowired
private lateinit var clock: Clock

@Autowired
private lateinit var categoryRepository: CategoryRepository

@Autowired
private lateinit var categoryIconRepository: CategoryIconRepository

@Autowired
@Qualifier("s3PublicUrlResolver")
private lateinit var iconUrlResolver: UrlResolver

@Autowired
private lateinit var notificationService: NotificationService

Expand All @@ -61,17 +77,18 @@ class NotificationDispatchServiceTest {
userNotificationSettingsRepository.save(UserNotificationSettingsFixture.allEnabled(userId))
}

private fun saveItem(name: String, quantity: Int, nextReplacementDate: LocalDate): Item = itemRepository.save(
Item(
userId = userId,
categoryId = 1L,
name = name,
quantity = quantity,
replacementIntervalDays = 30,
lastReplacedDate = nextReplacementDate.minusDays(30),
nextReplacementDate = nextReplacementDate,
),
)
private fun saveItem(name: String, quantity: Int, nextReplacementDate: LocalDate, categoryId: Long = 1L): Item =
itemRepository.save(
Item(
userId = userId,
categoryId = categoryId,
name = name,
quantity = quantity,
replacementIntervalDays = 30,
lastReplacedDate = nextReplacementDate.minusDays(30),
nextReplacementDate = nextReplacementDate,
),
)

@Test
fun `대상이 하나면 단건 알림을 발송하고 지연 발송 횟수를 기록한다`() {
Expand Down Expand Up @@ -254,6 +271,20 @@ class NotificationDispatchServiceTest {
assertEquals(item.id, response.itemId)
}

@Test
fun `알림 카드는 삭제된 소모품이어도 카테고리 아이콘 URL을 반환한다`() {
val icon = categoryIconRepository.save(CategoryIcon(name = "수건", key = "category-icon/towel.png", url = ""))
val category = categoryRepository.save(CategoryFixture.presetCategory(name = "수건", iconId = icon.id))
val item = saveItem("수건", 2, today.minusDays(1), categoryId = requireNotNull(category.id))
notificationDispatchService.dispatch()
item.softDelete()
itemRepository.flush()

val card = notificationService.listAllNotification(userId).single().cards.single()

assertEquals(iconUrlResolver.resolve(icon.key), card.iconUrl)
}

@Test
fun `사전 알림은 교체 D 마이너스 라벨을 반환한다`() {
saveItem("칫솔", 2, today.plusDays(3))
Expand Down
Loading