Merge pull request #96 from Kitzanos/feature/GEPAFINBE-94

GEPFINBE-94 (Preferred calls: Differentiate based on company)
This commit is contained in:
rbonazzo-KZ
2024-11-18 15:55:49 +01:00
committed by GitHub
8 changed files with 107 additions and 62 deletions

View File

@@ -975,14 +975,32 @@ public class ApplicationDao {
applicationEntity = saveApplicationEntity(applicationEntity); applicationEntity = saveApplicationEntity(applicationEntity);
return getApplicationResponse(applicationEntity); return getApplicationResponse(applicationEntity);
} }
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request,Long applicationId) { public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) {
ApplicationEntity applicationEntity = validateApplication(applicationId); ApplicationEntity applicationEntity = validateApplication(applicationId);
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null); validateAssignedUser(request, applicationId);
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId); Set<Long> documentIds = extractDocumentIdsFromApplicationForms(applicationId);
if(assignedApplications!=null){ List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds);
validator.validatePreInstructor(request,assignedApplications.getUserId());}
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
if (documents.isEmpty() && signedDocument == null) {
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
}
return createZipWithDocuments(applicationEntity, documents, signedDocument, applicationId);
}
private void validateAssignedUser(HttpServletRequest request, Long applicationId) {
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository
.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
if (assignedApplications != null) {
validator.validatePreInstructor(request, assignedApplications.getUserId());
}
}
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
Set<Long> documentIds = new HashSet<>(); Set<Long> documentIds = new HashSet<>();
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
applicationForms.forEach(applicationForm -> { applicationForms.forEach(applicationForm -> {
FormEntity formEntity = applicationForm.getForm(); FormEntity formEntity = applicationForm.getForm();
if (formEntity != null) { if (formEntity != null) {
@@ -1004,36 +1022,35 @@ public class ApplicationDao {
}); });
} }
}); });
List<DocumentEntity> documents = documentRepository.findAllById(documentIds); return documentIds;
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
if (documents.isEmpty() && signedDocument == null) {
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
} }
private void addDocumentToZip(ZipOutputStream zos, String s3Folder, String filePath, String fileName) {
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, filePath)) {
zos.putNextEntry(new ZipEntry(fileName));
IOUtils.copy(fileInputStream, zos);
zos.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Error downloading or adding document to ZIP: " + fileName, e);
}
}
private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List<DocumentEntity> documents,
ApplicationSignedDocumentEntity signedDocument, Long applicationId) {
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream(); try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) { ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
for (DocumentEntity document : documents) {
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId); String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId);
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
for (DocumentEntity document : documents) {
String fileName = Utils.extractFileName(document.getFilePath()); String fileName = Utils.extractFileName(document.getFilePath());
zos.putNextEntry(new ZipEntry(fileName)); addDocumentToZip(zos, s3Folder, document.getFilePath(), fileName);
IOUtils.copy(fileInputStream, zos);
zos.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Error downloading or adding document to ZIP: " + document.getFileName(), e);
}
} }
if (signedDocument != null) { if (signedDocument != null) {
String s3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId); String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId);
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, signedDocument.getFilePath())) { String signedDocFileName = signedDocument.getFileName();
String fileName = signedDocument.getFileName(); addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedDocFileName);
zos.putNextEntry(new ZipEntry(fileName));
IOUtils.copy(fileInputStream, zos);
zos.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Error downloading or adding signed document to ZIP: " + signedDocument.getFileName(), e);
}
} }
zos.finish(); zos.finish();
@@ -1044,4 +1061,5 @@ public class ApplicationDao {
} }
} }
} }

View File

@@ -7,14 +7,12 @@ import java.math.BigDecimal;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.entities.*; import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum; import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import net.gepafin.tendermanagement.model.response.*; import net.gepafin.tendermanagement.model.response.*;
@@ -22,6 +20,7 @@ import net.gepafin.tendermanagement.repositories.*;
import net.gepafin.tendermanagement.service.*; import net.gepafin.tendermanagement.service.*;
import net.gepafin.tendermanagement.util.DateTimeUtil; import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.Utils; import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.util.Validator;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -93,6 +92,8 @@ public class CallDao {
private S3PathConfig s3PathConfig; private S3PathConfig s3PathConfig;
@Autowired @Autowired
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository; private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
@Autowired
private Validator validator;
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) { public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
@@ -425,15 +426,24 @@ public class CallDao {
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND))); Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
} }
public CallResponse getCallById(UserEntity user, CallEntity callEntity) { public CallResponse getCallById(HttpServletRequest request,UserEntity user, CallEntity callEntity, Long companyId) {
Long userId = user.getId(); Long userId = user.getId();
Long callId = callEntity.getId(); Long callId = callEntity.getId();
BeneficiaryPreferredCallEntity preferredCall = beneficiaryPreferredCallRepository BeneficiaryPreferredCallEntity preferredCall;
if (companyId != null) {
validator.validateUserWithCompany(request, companyId);
preferredCall = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdAndCompanyIdAndIsDeletedFalse(userId, callId, companyId)
.orElse(null);
} else {
preferredCall = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndIsDeletedFalse(userId, List.of(callId)) .findByUserIdAndCallIdInAndIsDeletedFalse(userId, List.of(callId))
.stream() .stream()
.findFirst() .findFirst()
.orElse(null); .orElse(null);
}
CallResponse callResponse = getCallResponseBean(callEntity); CallResponse callResponse = getCallResponseBean(callEntity);
callResponse.setPreferredCallId(preferredCall != null ? preferredCall.getId() : null); callResponse.setPreferredCallId(preferredCall != null ? preferredCall.getId() : null);
@@ -646,24 +656,21 @@ public class CallDao {
return createCallResponseBean; return createCallResponseBean;
} }
public List<CallDetailsResponseBean> getAllCalls(UserEntity user) { public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,UserEntity user, Long companyId) {
String type = user.getRoleEntity().getRoleType(); String type = user.getRoleEntity().getRoleType();
List<String> callStatusList = CallStatusEnum.getStatusValues(); List<String> callStatusList = CallStatusEnum.getStatusValues();
if (Boolean.FALSE.equals(ROLE_SUPER_ADMIN.getValue().equals(type))) { if (Boolean.FALSE.equals(ROLE_SUPER_ADMIN.getValue().equals(type))) {
callStatusList = List.of(CallStatusEnum.PUBLISH.getValue()); callStatusList = List.of(CallStatusEnum.PUBLISH.getValue());
} }
List<CallEntity> calls = callRepository.findByStatusInAndHubId(callStatusList, user.getHub().getId()); List<CallEntity> calls = callRepository.findByStatusInAndHubId(callStatusList, user.getHub().getId());
List<Long> callIds = calls.stream().map(CallEntity::getId).collect(Collectors.toList()); List<Long> callIds = calls.stream().map(CallEntity::getId).collect(Collectors.toList());
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap =
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap = getBeneficiaryPreferredCallsForUser(user, callIds); getBeneficiaryPreferredCallsForUser(request,user, callIds, companyId);
return calls.stream() return calls.stream()
.map(call -> { .map(call -> {
CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(call); CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(call);
String key = user.getId() + "_" + call.getId(); String key = user.getId() + "_" + call.getId();
BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key); BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key);
Long preferredId = (preferredCall != null && !preferredCall.getIsDeleted()) ? preferredCall.getId() : null; Long preferredId = (preferredCall != null && !preferredCall.getIsDeleted()) ? preferredCall.getId() : null;
responseBean.setPreferredCallId(preferredId); responseBean.setPreferredCallId(preferredId);
@@ -673,9 +680,27 @@ public class CallDao {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(UserEntity user, List<Long> callIds) { public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(HttpServletRequest request, UserEntity user, List<Long> callIds, Long companyId) {
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls = beneficiaryPreferredCallRepository List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls;
if (companyId != null) {
validator.validateUserWithCompany(request, companyId);
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndCompanyIdAndIsDeletedFalse(user.getId(), callIds, companyId);
} else {
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndIsDeletedFalse(user.getId(), callIds); .findByUserIdAndCallIdInAndIsDeletedFalse(user.getId(), callIds);
beneficiaryPreferredCalls = beneficiaryPreferredCalls.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(
BeneficiaryPreferredCallEntity::getCallId,
call -> call,
(existing, replacement) -> existing
),
map -> new ArrayList<>(map.values())
));
}
return beneficiaryPreferredCalls.stream() return beneficiaryPreferredCalls.stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(

View File

@@ -16,9 +16,9 @@ public interface BeneficiaryPreferredCallRepository extends JpaRepository<Benefi
@Query("SELECT preferredCall FROM BeneficiaryPreferredCallEntity preferredCall where preferredCall.userId=:userId AND (:companyId is null OR preferredCall.companyId=:companyId)") @Query("SELECT preferredCall FROM BeneficiaryPreferredCallEntity preferredCall where preferredCall.userId=:userId AND (:companyId is null OR preferredCall.companyId=:companyId)")
List<BeneficiaryPreferredCallEntity> findByUserIdAndCompanyIdAndIsDeletedFalse(@Param("userId") Long userId, @Param("companyId") Long companyId); List<BeneficiaryPreferredCallEntity> findByUserIdAndCompanyIdAndIsDeletedFalse(@Param("userId") Long userId, @Param("companyId") Long companyId);
List<BeneficiaryPreferredCallEntity> findByBeneficiaryIdAndCompanyId(Long beneficiaryId,Long companyId); List<BeneficiaryPreferredCallEntity> findByBeneficiaryIdAndCompanyId(Long beneficiaryId,Long companyId);
public List<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdInAndIsDeletedFalse(Long userId, List<Long> callIds); List<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdInAndIsDeletedFalse(Long userId, List<Long> callIds);
Optional<BeneficiaryPreferredCallEntity> findByIdAndIsDeletedFalse(Long id); Optional<BeneficiaryPreferredCallEntity> findByIdAndIsDeletedFalse(Long id);
Optional<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdAndCompanyIdAndIsDeletedFalse(Long userId, Long callId, Long companyId); Optional<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdAndCompanyIdAndIsDeletedFalse(Long userId, Long callId, Long companyId);
List<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdInAndCompanyIdAndIsDeletedFalse(Long userId, List<Long> callIds,Long companyId);
} }

View File

@@ -3,6 +3,7 @@ import net.gepafin.tendermanagement.entities.DocumentEntity;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@@ -27,5 +28,6 @@ public interface DocumentRepository extends JpaRepository<DocumentEntity, Long>
@Query("SELECT d FROM DocumentEntity d WHERE d.isDeleted = false") @Query("SELECT d FROM DocumentEntity d WHERE d.isDeleted = false")
List<DocumentEntity> findAllByIsDeleteFalse(); List<DocumentEntity> findAllByIsDeleteFalse();
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
} }

View File

@@ -19,9 +19,9 @@ public interface CallService {
CallResponse updateCallStep1(HttpServletRequest request, Long callId, UpdateCallRequestStep1 updateCallRequest); CallResponse updateCallStep1(HttpServletRequest request, Long callId, UpdateCallRequestStep1 updateCallRequest);
CallResponse getCallById (HttpServletRequest request, Long callId); CallResponse getCallById (HttpServletRequest request, Long callId,Long companyId);
List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request); List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,Long companyId);
CallResponse validateCallData(HttpServletRequest request, Long callId); CallResponse validateCallData(HttpServletRequest request, Long callId);

View File

@@ -54,17 +54,17 @@ public class CallServiceImpl implements CallService {
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public CallResponse getCallById(HttpServletRequest request, Long callId) { public CallResponse getCallById(HttpServletRequest request, Long callId,Long companyId) {
UserEntity user = validator.validateUser(request); UserEntity user = validator.validateUser(request);
CallEntity call = validator.validateUserWithCall(user, callId); CallEntity call = validator.validateUserWithCall(user, callId);
return callDao.getCallById(user,call); return callDao.getCallById(request,user,call,companyId);
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request) { public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,Long companyId) {
UserEntity user = validator.validateUser(request); UserEntity user = validator.validateUser(request);
return callDao.getAllCalls(user); return callDao.getAllCalls(request,user,companyId);
} }

View File

@@ -74,7 +74,7 @@ public interface CallApi {
public ResponseEntity<Response<CallResponse>> updateCallStep1(HttpServletRequest request, public ResponseEntity<Response<CallResponse>> updateCallStep1(HttpServletRequest request,
@Parameter(description = "The call id", required = true) @PathVariable("callId") Long callId, @Parameter(description = "The call id", required = true) @PathVariable("callId") Long callId,
@Parameter(description = "Call request object", required = true) @Valid @RequestBody UpdateCallRequestStep1 updateCallRequest); @Parameter(description = "Call request object", required = true) @Valid @RequestBody UpdateCallRequestStep1 updateCallRequest);
@Operation(summary = "Api to get call by id updated today to check the bug", @Operation(summary = "Api to get call by id",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -86,7 +86,7 @@ public interface CallApi {
@GetMapping(value = "/{callId}", @GetMapping(value = "/{callId}",
produces = { "application/json" }) produces = { "application/json" })
ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request, ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request,
@Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId); @Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId,@RequestParam(value = "companyId", required = false) Long companyId);
@Operation(summary = "Api to get all calls", @Operation(summary = "Api to get all calls",
@@ -100,7 +100,7 @@ public interface CallApi {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) }) @ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@GetMapping(value = "", @GetMapping(value = "",
produces = { "application/json" }) produces = { "application/json" })
ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request); ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request,@RequestParam(value = "companyId", required = false) Long companyId);
@Operation(summary = "Api to validate call", @Operation(summary = "Api to validate call",

View File

@@ -59,16 +59,16 @@ public class CallApiController implements CallApi {
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request, Long callId) { public ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request, Long callId,Long companyId) {
CallResponse createCallResponseBean = callService.getCallById(request, callId); CallResponse createCallResponseBean = callService.getCallById(request, callId,companyId);
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(createCallResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG))); .body(new Response<>(createCallResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG)));
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request) { public ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request,Long companyId) {
List<CallDetailsResponseBean> calls = callService.getAllCalls(request); List<CallDetailsResponseBean> calls = callService.getAllCalls(request,companyId);
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(calls, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG))); .body(new Response<>(calls, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG)));