Skip to content

Commit

Permalink
Added: Fetch all user visible notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Jan 29, 2024
1 parent 9e5f8d0 commit 5afe0b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.1 - 2024-01-05]
## [0.0.1 - 2024-01-29]
### Added
- First version of the project
- Spring Application
Expand Down Expand Up @@ -76,3 +76,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Email as Mime message
- EmailSenderIfError annotation
- Frontend DTO converters
- Fetch all user visible notifications
7 changes: 3 additions & 4 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -800,17 +800,16 @@ public ResponseEntity<String> fetchTokenScript(
return convertToResponseEntity(() -> this.tokenManagerService.fetchAuthenticationScript(projectCode, bridgehead));
}

@RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.DEVELOPER, ProjectRole.PILOT, ProjectRole.FINAL, ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN})
@RoleConstraints(organisationRoles = {OrganisationRole.RESEARCHER, OrganisationRole.BRIDGEHEAD_ADMIN, OrganisationRole.PROJECT_MANAGER_ADMIN})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.NOTIFICATIONS_MODULE)
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_DASHBOARD_SITE, module = ProjectManagerConst.NOTIFICATIONS_MODULE)
@FrontendAction(action = ProjectManagerConst.FETCH_NOTIFICATIONS_ACTION)
@GetMapping(value = ProjectManagerConst.FETCH_NOTIFICATIONS)
public ResponseEntity<String> fetchNotifications(
@ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode,
// bridgehead required for identifying developer, pilot, final user or bridgehead admin in role constraints
@ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE, required = false) String projectCode,
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD, required = false) String bridgehead
) {
return convertToResponseEntity(() -> this.notificationService.fetchNotifications(projectCode, Optional.ofNullable(bridgehead)));
return convertToResponseEntity(() -> this.notificationService.fetchUserVisibleNotifications(Optional.ofNullable(projectCode), Optional.ofNullable(bridgehead)));
}


Expand Down
24 changes: 17 additions & 7 deletions src/main/java/de/samply/notification/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import de.samply.db.repository.NotificationRepository;
import de.samply.db.repository.ProjectRepository;
import de.samply.frontend.dto.DtoFactory;
import de.samply.project.ProjectService;
import de.samply.security.SessionUser;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -16,11 +19,17 @@ public class NotificationService {

private final NotificationRepository notificationRepository;
private final ProjectRepository projectRepository;
private final ProjectService projectService;
private final SessionUser sessionUser;

public NotificationService(NotificationRepository notificationRepository,
ProjectRepository projectRepository) {
ProjectRepository projectRepository,
ProjectService projectService,
SessionUser sessionUser) {
this.notificationRepository = notificationRepository;
this.projectRepository = projectRepository;
this.projectService = projectService;
this.sessionUser = sessionUser;
}

public void createNotification(@NotNull String projectCode, String bridgehead, @NotNull String email,
Expand All @@ -46,12 +55,13 @@ private Project fetchProject(String projectCode) throws NotificationServiceExcep
return project.get();
}

public List<de.samply.frontend.dto.Notification> fetchNotifications(String projectCode, Optional<String> bridghead) throws NotificationServiceException {
Project project = fetchProject(projectCode);
return ((bridghead.isEmpty()) ?
notificationRepository.findAllByProjectOrderByTimestampDesc(project) :
notificationRepository.findAllByProjectAndBridgeheadOrBridgeheadIsNullOrderByTimestampDesc(project, bridghead.get()))
.stream().map(DtoFactory::convert).toList();
public List<de.samply.frontend.dto.Notification> fetchUserVisibleNotifications(Optional<String> projectCodeOptional, Optional<String> bridgheadOptional) throws NotificationServiceException {
List<Notification> result = new ArrayList<>();
List<Project> projects = (projectCodeOptional.isEmpty()) ? projectService.fetchAllUserVisibleProjects() : List.of(fetchProject(projectCodeOptional.get()));
List<String> bridgeheads = (bridgheadOptional.isEmpty()) ? sessionUser.getBridgeheads().stream().toList() : List.of(bridgheadOptional.get());
projects.forEach(project -> bridgeheads.forEach(bridgehead -> result.addAll(
notificationRepository.findAllByProjectAndBridgeheadOrBridgeheadIsNullOrderByTimestampDesc(project, bridgehead))));
return result.stream().map(DtoFactory::convert).toList();
}


Expand Down

0 comments on commit 5afe0b7

Please sign in to comment.