From 8fba9a1c81cd6bec2a255da6fa5a9eb84c5d9270 Mon Sep 17 00:00:00 2001 From: piyushkag Date: Mon, 30 Dec 2024 15:25:32 +0530 Subject: [PATCH] Added Read/Unread filter in notification API. --- .../tendermanagement/dao/NotificationDao.java | 27 ++++++++++++------- .../service/NotificationService.java | 2 +- .../service/impl/NotificationServiceImpl.java | 4 +-- .../web/rest/api/NotificationApi.java | 7 ++--- .../api/impl/NotificationApiController.java | 4 +-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java index 64db69ca..eb74d57e 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java @@ -253,19 +253,26 @@ public class NotificationDao { notificationRepository.save(notificationEntity); } - public List getNotificationByCompanyIdAndUserId(Long userId, Long companyId) { + public List getNotificationByCompanyIdAndUserId(Long userId, Long companyId, List statuses) { - companyDao.validateCompany(companyId); - userDao.validateUser(userId); - - UserWithCompanyEntity userWithCompanyData = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalseForNotification(userId, companyId); - if (userWithCompanyData == null) { - throw new CustomValidationException(Status.BAD_REQUEST, GepafinConstant.USER_MUST_BE_ASSOCIATED_WITH_COMPANY); + List notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalse(userId); + UserWithCompanyEntity userWithCompany = null; + List statusStrings; + if (companyId != null) { + userWithCompany = companyDao.validateUserWithCompny(userId, companyId); } - List notifications = notificationRepository.findByUserWithCompanyIdAndUserIdAndIsDeletedFalse(userWithCompanyData.getId(), - userWithCompanyData.getUserId()); - return notifications.stream().map(this::convertNotificationEntityToNotificationResponse).toList(); + if (statuses != null) { + statusStrings = statuses.stream().map(NotificationEnum::name) + .toList(); + notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalseAndStatusIn(userId, statusStrings); + + if (userWithCompany != null) { + notificationEntities = notificationRepository.findByUserIdAndUserWithCompanyIdAndIsDeletedFalseAndStatusIn(userId, userWithCompany.getId(), statusStrings); + } + } + + return notificationEntities.stream().map(this::convertNotificationEntityToNotificationResponse).toList(); } } diff --git a/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java b/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java index 4d5d3ba6..ca5927c0 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java +++ b/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java @@ -18,6 +18,6 @@ public interface NotificationService { public void deleteNotification(HttpServletRequest request, Long id); - public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId); + public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId, List statuses); } diff --git a/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java b/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java index 9fa0956a..86eaa5cf 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java +++ b/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java @@ -56,7 +56,7 @@ public class NotificationServiceImpl implements NotificationService { } @Override - public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId) { - return notificationDao.getNotificationByCompanyIdAndUserId(userId, companyId); + public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId, List statuses) { + return notificationDao.getNotificationByCompanyIdAndUserId(userId, companyId, statuses); } } \ No newline at end of file diff --git a/src/main/java/net/gepafin/tendermanagement/web/rest/api/NotificationApi.java b/src/main/java/net/gepafin/tendermanagement/web/rest/api/NotificationApi.java index 6f46244b..ca059499 100644 --- a/src/main/java/net/gepafin/tendermanagement/web/rest/api/NotificationApi.java +++ b/src/main/java/net/gepafin/tendermanagement/web/rest/api/NotificationApi.java @@ -33,8 +33,8 @@ public interface NotificationApi { ErrorConstants.BADREQUEST_ERROR_EXAMPLE))) }) @PostMapping(value = "/user/{userId}/sent", consumes = "application/json", produces = "application/json") ResponseEntity> sendNotification(HttpServletRequest request, @RequestBody NotificationReq notificationReq, - @Parameter(description = "The company id", required = false) @RequestParam(value = "companyId", required = false) Long companyId, - @Parameter(description = "The user id", required = true) @PathVariable("userId") Long userId); + @Parameter(description = "The user id", required = true) @PathVariable("userId") Long userId, + @Parameter(description = "The company id", required = false) @RequestParam(value = "companyId", required = false) Long companyId); @Operation(summary = "Api to get notification by id", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @@ -93,7 +93,8 @@ public interface NotificationApi { @GetMapping(value = "/user/{userId}/company/{companyId}/notifications", produces = "application/json") ResponseEntity>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request, @Parameter(description = "The user id", required = true) @PathVariable(value = "userId") Long userId, - @Parameter(description = "The company ID", required = true) @PathVariable(value = "companyId") Long companyId); + @Parameter(description = "The company ID", required = true) @PathVariable(value = "companyId") Long companyId, + @Parameter(description = "The notification status", required = false) @RequestParam(value = "status", required = false) List statuses); } diff --git a/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/NotificationApiController.java b/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/NotificationApiController.java index 586a5f9e..d2047d7a 100644 --- a/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/NotificationApiController.java +++ b/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/NotificationApiController.java @@ -65,8 +65,8 @@ public class NotificationApiController implements NotificationApi { } @Override - public ResponseEntity>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request,Long userId, Long companyId) { - List notificationResponses = notificationService.getNotificationsByCompanyIdAndUserId(userId, companyId); + public ResponseEntity>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request,Long userId, Long companyId, List statuses) { + List notificationResponses = notificationService.getNotificationsByCompanyIdAndUserId(userId, companyId, statuses); return ResponseEntity.status(HttpStatus.OK) .body(new Response<>(notificationResponses, Status.SUCCESS, Translator.toLocale(GepafinConstant.NOTIFICATION_FETCHED_SUCCESSFULLY))); }