Done ticket GEPFINBE-94
This commit is contained in:
@@ -977,12 +977,30 @@ public class ApplicationDao {
|
||||
}
|
||||
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long 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);
|
||||
List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(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));
|
||||
}
|
||||
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());}
|
||||
validator.validatePreInstructor(request, assignedApplications.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
|
||||
Set<Long> documentIds = new HashSet<>();
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
|
||||
applicationForms.forEach(applicationForm -> {
|
||||
FormEntity formEntity = applicationForm.getForm();
|
||||
if (formEntity != null) {
|
||||
@@ -1004,36 +1022,35 @@ public class ApplicationDao {
|
||||
});
|
||||
}
|
||||
});
|
||||
List<DocumentEntity> documents = documentRepository.findAllById(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));
|
||||
return documentIds;
|
||||
}
|
||||
|
||||
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();
|
||||
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
|
||||
|
||||
for (DocumentEntity document : documents) {
|
||||
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());
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
IOUtils.copy(fileInputStream, zos);
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error downloading or adding document to ZIP: " + document.getFileName(), e);
|
||||
}
|
||||
addDocumentToZip(zos, s3Folder, document.getFilePath(), fileName);
|
||||
}
|
||||
|
||||
if (signedDocument != null) {
|
||||
String s3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId);
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, signedDocument.getFilePath())) {
|
||||
String fileName = signedDocument.getFileName();
|
||||
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);
|
||||
}
|
||||
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId);
|
||||
String signedDocFileName = signedDocument.getFileName();
|
||||
addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedDocFileName);
|
||||
}
|
||||
|
||||
zos.finish();
|
||||
@@ -1044,4 +1061,5 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,12 @@ import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
@@ -22,6 +20,7 @@ import net.gepafin.tendermanagement.repositories.*;
|
||||
import net.gepafin.tendermanagement.service.*;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import org.h2.util.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -93,6 +92,8 @@ public class CallDao {
|
||||
private S3PathConfig s3PathConfig;
|
||||
@Autowired
|
||||
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
|
||||
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
|
||||
@@ -425,15 +426,24 @@ public class CallDao {
|
||||
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 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))
|
||||
.stream()
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
CallResponse callResponse = getCallResponseBean(callEntity);
|
||||
callResponse.setPreferredCallId(preferredCall != null ? preferredCall.getId() : null);
|
||||
|
||||
@@ -646,24 +656,21 @@ public class CallDao {
|
||||
return createCallResponseBean;
|
||||
}
|
||||
|
||||
public List<CallDetailsResponseBean> getAllCalls(UserEntity user) {
|
||||
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,UserEntity user, Long companyId) {
|
||||
String type = user.getRoleEntity().getRoleType();
|
||||
List<String> callStatusList = CallStatusEnum.getStatusValues();
|
||||
|
||||
if (Boolean.FALSE.equals(ROLE_SUPER_ADMIN.getValue().equals(type))) {
|
||||
callStatusList = List.of(CallStatusEnum.PUBLISH.getValue());
|
||||
}
|
||||
|
||||
List<CallEntity> calls = callRepository.findByStatusInAndHubId(callStatusList, user.getHub().getId());
|
||||
List<Long> callIds = calls.stream().map(CallEntity::getId).collect(Collectors.toList());
|
||||
|
||||
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap = getBeneficiaryPreferredCallsForUser(user, callIds);
|
||||
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap =
|
||||
getBeneficiaryPreferredCallsForUser(request,user, callIds, companyId);
|
||||
|
||||
return calls.stream()
|
||||
.map(call -> {
|
||||
CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(call);
|
||||
String key = user.getId() + "_" + call.getId();
|
||||
|
||||
BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key);
|
||||
Long preferredId = (preferredCall != null && !preferredCall.getIsDeleted()) ? preferredCall.getId() : null;
|
||||
responseBean.setPreferredCallId(preferredId);
|
||||
@@ -673,9 +680,27 @@ public class CallDao {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(UserEntity user, List<Long> callIds) {
|
||||
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(HttpServletRequest request, UserEntity user, List<Long> callIds, Long companyId) {
|
||||
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls;
|
||||
|
||||
if (companyId != null) {
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndCompanyIdAndIsDeletedFalse(user.getId(), callIds, companyId);
|
||||
} else {
|
||||
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
.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()
|
||||
.collect(Collectors.toMap(
|
||||
|
||||
@@ -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)")
|
||||
List<BeneficiaryPreferredCallEntity> findByUserIdAndCompanyIdAndIsDeletedFalse(@Param("userId") Long userId, @Param("companyId") 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> findByUserIdAndCallIdAndCompanyIdAndIsDeletedFalse(Long userId, Long callId, Long companyId);
|
||||
|
||||
List<BeneficiaryPreferredCallEntity> findByUserIdAndCallIdInAndCompanyIdAndIsDeletedFalse(Long userId, List<Long> callIds,Long companyId);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import net.gepafin.tendermanagement.entities.DocumentEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
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")
|
||||
List<DocumentEntity> findAllByIsDeleteFalse();
|
||||
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,9 @@ public interface CallService {
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -54,17 +54,17 @@ public class CallServiceImpl implements CallService {
|
||||
}
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public CallResponse getCallById(HttpServletRequest request, Long callId) {
|
||||
public CallResponse getCallById(HttpServletRequest request, Long callId,Long companyId) {
|
||||
UserEntity user = validator.validateUser(request);
|
||||
CallEntity call = validator.validateUserWithCall(user, callId);
|
||||
return callDao.getCallById(user,call);
|
||||
return callDao.getCallById(request,user,call,companyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request) {
|
||||
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,Long companyId) {
|
||||
UserEntity user = validator.validateUser(request);
|
||||
return callDao.getAllCalls(user);
|
||||
return callDao.getAllCalls(request,user,companyId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ public interface CallApi {
|
||||
public ResponseEntity<Response<CallResponse>> updateCallStep1(HttpServletRequest request,
|
||||
@Parameter(description = "The call id", required = true) @PathVariable("callId") Long callId,
|
||||
@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 = {
|
||||
@ApiResponse(responseCode = "200", description = "OK"),
|
||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@@ -86,7 +86,7 @@ public interface CallApi {
|
||||
@GetMapping(value = "/{callId}",
|
||||
produces = { "application/json" })
|
||||
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",
|
||||
@@ -100,7 +100,7 @@ public interface CallApi {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
|
||||
@GetMapping(value = "",
|
||||
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",
|
||||
|
||||
@@ -59,16 +59,16 @@ public class CallApiController implements CallApi {
|
||||
}
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request, Long callId) {
|
||||
CallResponse createCallResponseBean = callService.getCallById(request, callId);
|
||||
public ResponseEntity<Response<CallResponse>> getCallById(HttpServletRequest request, Long callId,Long companyId) {
|
||||
CallResponse createCallResponseBean = callService.getCallById(request, callId,companyId);
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Response<>(createCallResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request) {
|
||||
List<CallDetailsResponseBean> calls = callService.getAllCalls(request);
|
||||
public ResponseEntity<Response<List<CallDetailsResponseBean>>> getAllCalls(HttpServletRequest request,Long companyId) {
|
||||
List<CallDetailsResponseBean> calls = callService.getAllCalls(request,companyId);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Response<>(calls, Status.SUCCESS, Translator.toLocale(GepafinConstant.CALL_FETCH_SUCCESS_MSG)));
|
||||
|
||||
Reference in New Issue
Block a user