From 8074d5d73d6b906053c7fe2d78ceb3e8d12daa7a Mon Sep 17 00:00:00 2001 From: piyushkag Date: Sat, 28 Dec 2024 12:19:39 +0530 Subject: [PATCH] Added API for getting notifications by compnayId and userId. --- .../tendermanagement/dao/NotificationDao.java | 19 +++++++++++++++++++ .../repositories/NotificationRepository.java | 3 +++ .../service/NotificationService.java | 3 +++ .../service/impl/NotificationServiceImpl.java | 4 ++++ .../web/rest/api/NotificationApi.java | 12 ++++++++++++ .../api/impl/NotificationApiController.java | 10 +++++++--- 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java index 5f7d700f..64db69ca 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/NotificationDao.java @@ -61,6 +61,9 @@ public class NotificationDao { @Autowired private ApplicationService applicationService; + @Autowired + private UserDao userDao; + public NotificationResponse sendNotification(NotificationReq notificationReq) { // Ensure userId is properly set in notificationReq if not already @@ -249,4 +252,20 @@ public class NotificationDao { notificationEntity.setIsDeleted(true); notificationRepository.save(notificationEntity); } + + public List getNotificationByCompanyIdAndUserId(Long userId, Long companyId) { + + 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 notifications = notificationRepository.findByUserWithCompanyIdAndUserIdAndIsDeletedFalse(userWithCompanyData.getId(), + userWithCompanyData.getUserId()); + return notifications.stream().map(this::convertNotificationEntityToNotificationResponse).toList(); + } + } diff --git a/src/main/java/net/gepafin/tendermanagement/repositories/NotificationRepository.java b/src/main/java/net/gepafin/tendermanagement/repositories/NotificationRepository.java index f3c1d037..bce3ba68 100644 --- a/src/main/java/net/gepafin/tendermanagement/repositories/NotificationRepository.java +++ b/src/main/java/net/gepafin/tendermanagement/repositories/NotificationRepository.java @@ -1,6 +1,7 @@ package net.gepafin.tendermanagement.repositories; import net.gepafin.tendermanagement.entities.NotificationEntity; +import org.springframework.data.domain.Page; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -14,4 +15,6 @@ public interface NotificationRepository extends JpaRepository findByUserIdAndUserWithCompanyIdAndIsDeletedFalseAndStatusIn(Long userId, Long userWithCompanyId, List statuses); List findByUserIdAndIsDeletedFalseAndStatusIn(Long userId, List statuses); + + List findByUserWithCompanyIdAndUserIdAndIsDeletedFalse(Long userWithCompanyId, Long userId); } diff --git a/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java b/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java index 4657883d..4d5d3ba6 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java +++ b/src/main/java/net/gepafin/tendermanagement/service/NotificationService.java @@ -17,4 +17,7 @@ public interface NotificationService { public NotificationResponse updateNotificationStatus(HttpServletRequest request, Long id, NotificationEnum status); public void deleteNotification(HttpServletRequest request, Long id); + + public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId); + } 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 2a725935..9fa0956a 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java +++ b/src/main/java/net/gepafin/tendermanagement/service/impl/NotificationServiceImpl.java @@ -55,4 +55,8 @@ public class NotificationServiceImpl implements NotificationService { return; } + @Override + public List getNotificationsByCompanyIdAndUserId(Long userId, Long companyId) { + return notificationDao.getNotificationByCompanyIdAndUserId(userId, companyId); + } } \ 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 739a1516..6f46244b 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 @@ -83,6 +83,18 @@ public interface NotificationApi { ResponseEntity> deleteNotification(HttpServletRequest request, @Parameter(description = "The notification id", required = true) @PathVariable(value = "id", required = true) Long id); + @Operation(summary = "API to get notifications by user ID and company ID", responses = { @ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { + @ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })), + @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { + @ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })), + @ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { + @ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) }) + @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); + } 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 11c61f23..586a5f9e 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 @@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletRequest; import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import net.gepafin.tendermanagement.enums.NotificationEnum; -import net.gepafin.tendermanagement.enums.NotificationTypeEnum; import net.gepafin.tendermanagement.model.request.NotificationReq; import net.gepafin.tendermanagement.model.response.NotificationResponse; import net.gepafin.tendermanagement.model.util.Response; @@ -19,8 +18,6 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; -import static org.hibernate.internal.util.collections.CollectionHelper.listOf; - @RestController @RequestMapping("${openapi.gepafin.base-path:/v1/notification}") public class NotificationApiController implements NotificationApi { @@ -67,4 +64,11 @@ public class NotificationApiController implements NotificationApi { return ResponseEntity.status(HttpStatus.OK).body(new Response<>(null, Status.SUCCESS, Translator.toLocale(GepafinConstant.NOTIFICATION_DELETED_SUCCESSFULLY))); } + @Override + public ResponseEntity>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request,Long userId, Long companyId) { + List notificationResponses = notificationService.getNotificationsByCompanyIdAndUserId(userId, companyId); + return ResponseEntity.status(HttpStatus.OK) + .body(new Response<>(notificationResponses, Status.SUCCESS, Translator.toLocale(GepafinConstant.NOTIFICATION_FETCHED_SUCCESSFULLY))); + } + } \ No newline at end of file