Done Ticket GEPAFINBE-171
This commit is contained in:
@@ -92,16 +92,20 @@ public class CompanyDocumentDao {
|
||||
private AmazonS3 amazonS3;
|
||||
|
||||
public List<CompanyDocumentResponseBean> uploadFileForCompany(HttpServletRequest request, Long userId, List<MultipartFile> files, Long companyId, Long documentCategoryId, CompanyDocumentTypeEnum companyDocumentSourceTypeEnum, LocalDateTime expirationDate,String name){
|
||||
|
||||
log.info("Uploading files for company. userId={}, companyId={}, documentCategoryId={}", userId, companyId, documentCategoryId);
|
||||
DocumentCategoryEntity categoryEntity = categoryDao.validateCategory(documentCategoryId);
|
||||
validator.validateUserWithCompany(request,companyId);
|
||||
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(userId,companyId);
|
||||
|
||||
LocalDateTime currentDate = LocalDateTime.now();
|
||||
if (expirationDate.isBefore(currentDate)) {
|
||||
log.warn("Expiration date {} is before current time {}", expirationDate, currentDate);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_EXPIRATION_DATE));
|
||||
}
|
||||
List<CompanyDocumentEntity> companyDocumentEntities = new ArrayList<>();
|
||||
for (MultipartFile file : files){
|
||||
log.info("Uploading file '{}' for companyId={}", file.getOriginalFilename(), companyId);
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = uploadFileOnAmazonS3(file, companyDocumentSourceTypeEnum, companyId);
|
||||
if (uploadFileOnAmazonS3Response != null) {
|
||||
CompanyDocumentEntity companyDocumentEntity = new CompanyDocumentEntity();
|
||||
@@ -114,8 +118,10 @@ public class CompanyDocumentDao {
|
||||
companyDocumentEntity.setName(name);
|
||||
if (expirationDate.isBefore(currentDate.plusDays(7))) {
|
||||
companyDocumentEntity.setStatus(CompanyDocumentStatusEnum.DUE.getValue());
|
||||
log.info("Document '{}' marked as DUE (expires within 7 days).", uploadFileOnAmazonS3Response.getFileName());
|
||||
} else {
|
||||
companyDocumentEntity.setStatus(CompanyDocumentStatusEnum.VALID.getValue());
|
||||
log.info("Document '{}' marked as VALID.", uploadFileOnAmazonS3Response.getFileName());
|
||||
}
|
||||
|
||||
companyDocumentEntity.setCategoryEntity(categoryEntity);
|
||||
@@ -125,9 +131,9 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
}
|
||||
companyDocumentRepository.saveAll(companyDocumentEntities);
|
||||
log.info("Saved {} documents for companyId={}", companyDocumentEntities.size(), companyId);
|
||||
|
||||
/** This code is responsible for adding a version history log for the "Upload company document" operation. **/
|
||||
|
||||
companyDocumentEntities.forEach(entity -> loggingUtil.addVersionHistory(
|
||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(entity).build()));
|
||||
|
||||
@@ -143,6 +149,7 @@ public class CompanyDocumentDao {
|
||||
log.info("Generated S3 path {}", s3Path);
|
||||
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while uploading file '{}' for Company ID '{}': {}", file.getOriginalFilename(), companyId, e.getMessage());
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
}
|
||||
@@ -151,13 +158,17 @@ public class CompanyDocumentDao {
|
||||
try {
|
||||
return s3ConfigBean.generateCompanyDocumentPath(typeOfDocument, companyId);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Failed to generate S3 path for Company ID '{}' and Document Type '{}': {}", companyId, typeOfDocument, e.getMessage());
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG));
|
||||
}
|
||||
}
|
||||
|
||||
public CompanyDocumentEntity validateCompanyDocument(Long id) {
|
||||
return companyDocumentRepository.findByIdAndNotDeleted(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.COMPANY_DOCUMENT_NOT_FOUND)));
|
||||
return companyDocumentRepository.findByIdAndNotDeleted(id).orElseThrow(() -> {
|
||||
log.warn("Company Document not found with ID '{}'", id);
|
||||
return new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.COMPANY_DOCUMENT_NOT_FOUND));
|
||||
});
|
||||
}
|
||||
|
||||
public CompanyDocumentResponseBean convertToCompanyDocumentResponseBean(CompanyDocumentEntity entity) {
|
||||
@@ -182,20 +193,23 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
|
||||
public CompanyDocumentResponseBean updateCompanyDocument(HttpServletRequest request,Long companyDocumentId, CompanyDocumentRequest companyDocumentRequest){
|
||||
|
||||
log.info("Start: Updating company document with ID: {}", companyDocumentId);
|
||||
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
|
||||
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
|
||||
CompanyDocumentEntity oldCompanyDocumentData = Utils.getClonedEntityForData(companyDocumentEntity);
|
||||
LocalDateTime currentDate = LocalDateTime.now();
|
||||
if (companyDocumentRequest.getExpirationDate() != null) {
|
||||
if (companyDocumentRequest.getExpirationDate().isBefore(currentDate)) {
|
||||
log.warn("Invalid expiration date: {}", companyDocumentRequest.getExpirationDate());
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_EXPIRATION_DATE));
|
||||
}
|
||||
companyDocumentEntity.setExpirationDate(companyDocumentRequest.getExpirationDate());
|
||||
if (companyDocumentRequest.getExpirationDate().isBefore(currentDate.plusDays(7))) {
|
||||
companyDocumentEntity.setStatus(CompanyDocumentStatusEnum.DUE.getValue());
|
||||
log.info("Document '{}' marked as DUE.", companyDocumentEntity.getName());
|
||||
} else {
|
||||
companyDocumentEntity.setStatus(CompanyDocumentStatusEnum.VALID.getValue());
|
||||
log.info("Document '{}' marked as VALID (expiration is beyond 7 days).", companyDocumentEntity.getName());
|
||||
}
|
||||
}
|
||||
if (companyDocumentRequest.getCategoryId() != null && companyDocumentRequest.getCategoryId() >0) {
|
||||
@@ -204,6 +218,7 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
setIfUpdated(companyDocumentEntity::getName, companyDocumentEntity::setName, companyDocumentRequest.getName());
|
||||
companyDocumentRepository.save(companyDocumentEntity);
|
||||
log.info("Saved updates for Company Document ID '{}'", companyDocumentId);
|
||||
|
||||
/** This code is responsible for adding a version history log for the "updating company document" operation. **/
|
||||
loggingUtil.addVersionHistory(
|
||||
@@ -213,12 +228,14 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
|
||||
public CompanyDocumentResponseBean getCompanyDocument(UserEntity user ,Long companyDocumentId) {
|
||||
log.info("Fetching company document with ID '{}' for user '{}'", companyDocumentId, user.getId());
|
||||
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
|
||||
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
|
||||
return convertToCompanyDocumentResponseBean(companyDocumentEntity);
|
||||
}
|
||||
|
||||
public void deleteCompanyFile(Long companyDocumentId){
|
||||
log.info("Deleting file for company document ID '{}'", companyDocumentId);
|
||||
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
|
||||
deleteCompanyFileFromS3(companyDocumentEntity);
|
||||
}
|
||||
@@ -259,6 +276,7 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
|
||||
public DocumentResponseBean createDuplicateCompanyDocument(HttpServletRequest request , Long userId ,Long companyDocumentId , Long applicationId , DocumentTypeEnum documentTypeEnum){
|
||||
log.info("Creating duplicate of company document ID '{}' for application ID '{}'", companyDocumentId, applicationId);
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationId);
|
||||
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
|
||||
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
|
||||
@@ -272,7 +290,7 @@ public class CompanyDocumentDao {
|
||||
try {
|
||||
response = amazonS3ServiceImpl.copyFile(companyDocumentEntity.getName(), companyDocumentPath, documentPath);
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while uploading file from Amazon S3: {}", e);
|
||||
log.error("Error occurred while uploading file from Amazon S3: {} for application ID '{}' and company Document ID '{}' ", e,applicationId,companyDocumentId);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
@@ -296,12 +314,14 @@ public class CompanyDocumentDao {
|
||||
}
|
||||
|
||||
public List<CompanyDocumentResponseBean> getAllCompanyDocument(UserEntity user , Long companyId, CompanyDocumentTypeEnum typeEnum){
|
||||
log.info("Fetching all company documents for Company ID '{}', User ID '{}', Type '{}'", companyId, user.getId(), typeEnum);
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
companyService.validateCompany(companyId);
|
||||
|
||||
Specification<CompanyDocumentEntity> spec = filterCompanyDocuments(companyId, user.getId(), typeEnum);
|
||||
|
||||
List<CompanyDocumentEntity> companyDocumentEntities = companyDocumentRepository.findAll(spec);
|
||||
log.info("Retrieved all documents for Company ID '{}'", companyId);
|
||||
return companyDocumentEntities.stream()
|
||||
.map(this::convertToCompanyDocumentResponseBean)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Reference in New Issue
Block a user