Skip to content

Commit

Permalink
Merge pull request #7237 from ita-social-projects/fix-unread-notifica…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
KizerovDmitriy committed Jun 28, 2024
2 parents bffcc97 + d83a3a5 commit ca68607
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
9 changes: 5 additions & 4 deletions dao/src/main/java/greencity/repository/NotificationRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ Page<Notification> findByTargetUserIdAndProjectNameInAndNotificationTypeInOrderB
ProjectName[] projectNames, NotificationType[] notificationTypes, Pageable pageable);

/**
* This method return unread notification.
* Checks if there are any unread notifications for the specified user.
*
* @param targetUserId user, which should return notification
* @return Notification for user, where viewed is false
* @param targetUserId the ID of the user for whom to check for unread
* notifications
* @return true if there are unread notifications for the user, false otherwise
*/
Optional<Notification> findNotificationByTargetUserIdAndViewedIsFalse(Long targetUserId);
boolean existsByTargetUserIdAndViewedIsFalse(Long targetUserId);

/**
* Changes {@link Notification} `viewed` as true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -112,10 +111,10 @@ public NotificationDto getNotification(Principal principal, Long notificationId,
*/
@Override
public void notificationSocket(ActionDto user) {
Optional<Notification> userNotification =
notificationRepo.findNotificationByTargetUserIdAndViewedIsFalse(user.getUserId());
boolean isExist =
notificationRepo.existsByTargetUserIdAndViewedIsFalse(user.getUserId());
messagingTemplate
.convertAndSend("/topic/" + user.getUserId() + "/notification", userNotification.isPresent());
.convertAndSend("/topic/" + user.getUserId() + "/notification", isExist);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,13 @@ void getNotificationThrowNotFoundExceptionTest() {
@Test
void notificationSocketTest() {
var dto = getActionDto();
var notification = getNotification();

when(notificationRepo.findNotificationByTargetUserIdAndViewedIsFalse(dto.getUserId()))
.thenReturn(Optional.of(notification));
when(notificationRepo.existsByTargetUserIdAndViewedIsFalse(dto.getUserId()))
.thenReturn(true);
userNotificationService.notificationSocket(dto);

verify(messagingTemplate).convertAndSend("/topic/" + dto.getUserId() + "/notification", true);
verify(notificationRepo).findNotificationByTargetUserIdAndViewedIsFalse(dto.getUserId());
verify(notificationRepo).existsByTargetUserIdAndViewedIsFalse(dto.getUserId());
}

@Test
Expand Down

0 comments on commit ca68607

Please sign in to comment.