From e81e62cebbe4dcf0ce87dc8a9dd0fd2c4008f285 Mon Sep 17 00:00:00 2001 From: rajesh Date: Mon, 16 Sep 2024 17:01:47 +0530 Subject: [PATCH] updated code for get all application api --- .../tendermanagement/dao/ApplicationDao.java | 51 +++++++++++++------ .../repositories/ApplicationRepository.java | 4 ++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java index 8d68e984..d9ff714b 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java @@ -4,6 +4,7 @@ import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import net.gepafin.tendermanagement.entities.*; import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum; +import net.gepafin.tendermanagement.enums.RoleStatusEnum; import net.gepafin.tendermanagement.model.request.ApplicationFormFieldRequestBean; import net.gepafin.tendermanagement.model.request.ApplicationRequest; import net.gepafin.tendermanagement.model.request.ApplicationRequestBean; @@ -114,26 +115,44 @@ public class ApplicationDao { log.info("Application deleted with ID: {}", id); } - public List getAllApplications(UserEntity userEntity,Long callId) { - log.info("Fetching all applications"); - List applicationResponses=new ArrayList<>(); - if(callId!=null) { + public List getAllApplications(UserEntity userEntity, Long callId) { + RoleStatusEnum roleStatus = RoleStatusEnum.valueOf(userEntity.getRoleEntity().getRoleType()); + boolean isBeneficiary = RoleStatusEnum.ROLE_BENEFICIARY.equals(roleStatus); + + log.info("Fetching applications for RoleType: {}", roleStatus); + List applicationResponses = new ArrayList<>(); + + if (callId != null) { + // Fetch based on callId and user if role is BENEFICIARY, otherwise fetch all for the call + log.info("Fetching applications for callId: {}", callId); CallEntity call = callService.validateCall(callId); - Optional applicationEntity1 = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId()); - if (applicationEntity1.isPresent()) { - ApplicationResponse responseBean = getApplicationResponse(applicationEntity1.get()); - applicationResponses.add(responseBean); - return applicationResponses; - } + + // Use a single method to handle both conditions for consistency + List applicationEntities = isBeneficiary + ? applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId()) + .map(List::of) // Convert Optional to a List of one element + .orElse(List.of()) // If not present, return an empty list + : applicationRepository.findByCallIdAndIsDeletedFalse(call.getId()); + + applicationResponses = applicationEntities.stream() + .map(this::getApplicationResponse) + .collect(Collectors.toList()); + + } else { + // Fetch all applications for the user if BENEFICIARY, or fetch all applications in general + List applicationEntities = isBeneficiary + ? applicationRepository.findByUserIdAndIsDeletedFalse(userEntity.getId()) + : applicationRepository.findByIsDeletedFalse(); + + applicationResponses = applicationEntities.stream() + .map(this::getApplicationResponse) + .collect(Collectors.toList()); } - List applicationEntities = applicationRepository.findByUserIdAndIsDeletedFalse(userEntity.getId()); - for(ApplicationEntity applicationEntity:applicationEntities){ - ApplicationResponse responseBean = getApplicationResponse(applicationEntity); - applicationResponses.add(responseBean); - } - return applicationResponses; + + return applicationResponses; } + private ApplicationResponse getApplicationResponse(ApplicationEntity applicationEntity) { ApplicationResponse responseBean = new ApplicationResponse(); responseBean.setId(applicationEntity.getId()); diff --git a/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationRepository.java b/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationRepository.java index b32b8f46..8d41608c 100644 --- a/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationRepository.java +++ b/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationRepository.java @@ -19,4 +19,8 @@ public interface ApplicationRepository extends JpaRepository findById(@Param("id") Long id); + + public List findByCallIdAndIsDeletedFalse(Long callId); + + public List findByIsDeletedFalse(); }