Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. @Override
  2. public SendNotificationVM sendNotification(ManagedSendNotificationVM notificationVM) {
  3. Set<User> users = getUsers(notificationVM);
  4. Notification notification = Notification.builder()
  5. .message(notificationVM.getMessage())
  6. .status(NotificationStatus.SENT)
  7. .messageType(notificationVM.getMessageType())
  8. .title(notificationVM.getTitle())
  9. .build();
  10. repository.save(notification);
  11. log.debug("Saved notification");
  12.  
  13. users.forEach(user -> {
  14. NotificationHistory notificationHistory = NotificationHistory.builder()
  15. .user(user)
  16. .notification(notification)
  17. .readStatus(HistoryNotificationStatus.UNREAD)
  18. .build();
  19. notificationHistoryRepository.save(notificationHistory);
  20. log.debug("Updated notification history");
  21. });
  22. log.info("Notification[id={}] has been created for users{}. Producer: [{}]", notification.getId(), users.stream().map(User::getId).collect(Collectors.toList()), getCurrentUserLogin());
  23. return mapper.toDto(notification);
  24. }
  25.  
  26. private Set<User> getUsers(ManagedSendNotificationVM notificationVM) {
  27. List<User> users = userService.findAllOrThrow(getList(notificationVM.getUsers()));
  28. List<Group> groups = groupService.findAllOrThrow(getList(notificationVM.getGroups()));
  29. List<User> usersFromGroup = groups.stream()
  30. .map(Group::getUsers)
  31. .flatMap(Collection::stream)
  32. .collect(Collectors.toList());
  33. users.addAll(usersFromGroup);
  34. if (users.isEmpty()) {
  35. log.warn("Cannot find valid users from [{}] idS and groups[]", notificationVM.getUsers(), notificationVM.getGroups());
  36. throw new com.longulf.net.errors.EntityNotFoundException("Cannot find any users to send notification");
  37. }
  38. return Sets.newHashSet(users);
  39. }
  40.  
  41. @Override
  42. public HistoryNotificationStatus markNotificationForCurrentUser(HistoryNotificationStatus status, Long id) {
  43. User user = userService.getUserWithAuthority()
  44. .orElseThrow(() -> new EntityNotFoundException("User could not be found"));
  45.  
  46. NotificationHistory notificationHistory = user.getNotifications()
  47. .stream()
  48. .filter(n -> n.getNotification().getId().equals(id))
  49. .findFirst()
  50. .orElseThrow(() -> new com.longulf.net.errors.EntityNotFoundException("Cannot find user's notification with notificationId=" + id));
  51. notificationHistory.setReadStatus(HistoryNotificationStatus.READ);
  52. log.info("Notification[id={}] status has been marked as READ by user[{}]",id, getCurrentUserLoginOrThrow());
  53. return status;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement