diff --git a/src/main/java/org/swyp/linkit/domain/notification/dto/NotificationDto.java b/src/main/java/org/swyp/linkit/domain/notification/dto/NotificationDto.java index fce64e73..0ab0e311 100644 --- a/src/main/java/org/swyp/linkit/domain/notification/dto/NotificationDto.java +++ b/src/main/java/org/swyp/linkit/domain/notification/dto/NotificationDto.java @@ -21,8 +21,9 @@ public class NotificationDto { private Long refId; private boolean isRead; private LocalDateTime createdAt; + private String message; - public static NotificationDto from(Notification notification) { + public static NotificationDto from(Notification notification, String message) { return NotificationDto.builder() .id(notification.getId()) .receiverId(notification.getReceiver().getId()) @@ -31,6 +32,7 @@ public static NotificationDto from(Notification notification) { .refId(notification.getRefId()) .isRead(notification.isRead()) .createdAt(notification.getCreatedAt()) + .message(message) .build(); } } \ No newline at end of file diff --git a/src/main/java/org/swyp/linkit/domain/notification/service/NotificationServiceImpl.java b/src/main/java/org/swyp/linkit/domain/notification/service/NotificationServiceImpl.java index 7d840d89..1a18694a 100644 --- a/src/main/java/org/swyp/linkit/domain/notification/service/NotificationServiceImpl.java +++ b/src/main/java/org/swyp/linkit/domain/notification/service/NotificationServiceImpl.java @@ -24,8 +24,11 @@ import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; @Slf4j @@ -71,7 +74,7 @@ public NotificationDto createNotification(Long receiverId, Long senderId, Notifi publishNotificationToRedis(savedNotification, senderNickname, message); log.info("알림 생성 및 발송: receiverId={}, type={}, refId={}", receiverId, type, refId); - return NotificationDto.from(savedNotification); + return NotificationDto.from(savedNotification, message); } @Override @@ -87,7 +90,7 @@ public NotificationDto createSystemNotification(Long receiverId, NotificationTyp publishNotificationToRedis(savedNotification, "시스템", message); log.info("시스템 알림 생성 및 발송: receiverId={}, type={}, refId={}", receiverId, type, refId); - return NotificationDto.from(savedNotification); + return NotificationDto.from(savedNotification, message); } // ===== 미읽음 개수 조회 ===== @@ -143,8 +146,23 @@ public NotificationListResponseDto getNotifications(Long userId) { combinedNotifications.addAll(unreadNotifications); combinedNotifications.addAll(readNotifications); + // sender nickname 배치 조회 + Set senderIds = combinedNotifications.stream() + .map(Notification::getSenderId) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + Map nicknameMap = senderIds.isEmpty() + ? Collections.emptyMap() + : userRepository.findAllById(senderIds).stream() + .collect(Collectors.toMap(User::getId, + u -> u.getNickname() != null ? u.getNickname() : "시스템")); + List notificationDtos = combinedNotifications.stream() - .map(NotificationDto::from) + .map(n -> { + String senderNickname = nicknameMap.getOrDefault(n.getSenderId(), "시스템"); + String message = generateNotificationMessage(n.getNotificationType(), senderNickname); + return NotificationDto.from(n, message); + }) .collect(Collectors.toList()); return NotificationListResponseDto.of(notificationDtos, unreadNotifications.size());