Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/BeneficiaryPreferredCallDao.java
2024-11-27 17:01:03 +05:30

181 lines
10 KiB
Java

package net.gepafin.tendermanagement.dao;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.ApplicationEntity;
import net.gepafin.tendermanagement.entities.BeneficiaryPreferredCallEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.entities.UserWithCompanyEntity;
import net.gepafin.tendermanagement.enums.BeneficiaryCallStatus;
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
import net.gepafin.tendermanagement.model.request.BeneficiaryPreferredCallReq;
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
import net.gepafin.tendermanagement.model.response.BeneficiaryPreferredCallResponseBean;
import net.gepafin.tendermanagement.repositories.BeneficiaryPreferredCallRepository;
import net.gepafin.tendermanagement.repositories.UserWithCompanyRepository;
import net.gepafin.tendermanagement.service.CompanyService;
import net.gepafin.tendermanagement.util.LoggingUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.util.Validator;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
public class BeneficiaryPreferredCallDao {
private final Logger log = LoggerFactory.getLogger(BeneficiaryPreferredCallDao.class);
@Autowired
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
@Autowired
private Validator validator;
@Autowired
private UserWithCompanyRepository userWithCompanyRepository;
@Autowired
private CompanyService companyService;
@Autowired
private LoggingUtil loggingUtil;
@Autowired
private HttpServletRequest httpServletRequest;
public BeneficiaryPreferredCallResponseBean createBeneficiaryPreferredCall(HttpServletRequest httpServletRequest, BeneficiaryPreferredCallReq request, UserEntity user) {
log.info("Creating new beneficiary preferred call with details: {}", request);
validator.validateUserWithCompany(httpServletRequest, request.getCompanyId());
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(user.getId(), request.getCompanyId());
Optional<BeneficiaryPreferredCallEntity> existingCall = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdAndUserWithCompanyIdAndIsDeletedFalse(user.getId(), request.getCallId(), userWithCompanyEntity.getId());
if (existingCall.isPresent()) {
log.warn("Duplicate beneficiary preferred call detected: {}", existingCall.get());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DUPLICATE_BENEFICIARY_CALL));
}
BeneficiaryPreferredCallEntity entity = convertRequestToEntity(request, user,userWithCompanyEntity);
entity = beneficiaryPreferredCallRepository.save(entity);
/** This code is responsible for adding a version history log for "Create Beneficiary Preferred Call" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(httpServletRequest).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(entity).build());
log.info("Beneficiary preferred call created with ID: {}", entity.getId());
return convertEntityToResponse(entity);
}
private BeneficiaryPreferredCallEntity convertRequestToEntity(BeneficiaryPreferredCallReq request,UserEntity userEntity,UserWithCompanyEntity userWithCompanyEntity) {
BeneficiaryPreferredCallEntity entity = new BeneficiaryPreferredCallEntity();
if (userEntity.getBeneficiary()!=null) {
entity.setBeneficiaryId(userEntity.getBeneficiary().getId());
}
entity.setStatus(BeneficiaryCallStatus.ENABLED.getValue());
entity.setCallId(request.getCallId());
entity.setUserId(userEntity.getId());
entity.setCompanyId(userWithCompanyEntity.getCompanyId());
entity.setUserWithCompany(userWithCompanyEntity);
entity.setIsDeleted( false);
return entity;
}
public BeneficiaryPreferredCallResponseBean getBeneficiaryPreferredCallById(HttpServletRequest request, Long id) {
log.info("Fetching beneficiary preferred call with ID: {}", id);
BeneficiaryPreferredCallEntity entity = validateBeneficiaryPreferredCall(id);
validator.validateUserId(request, entity.getUserId());
log.info("Beneficiary preferred call found: {}", entity);
return convertEntityToResponse(entity);
}
// public BeneficiaryPreferredCallResponseBean updateBeneficiaryPreferredCall(Long id, BeneficiaryPreferredCallReq request,
// UserEntity userEntity) {
// log.info("Updating beneficiary preferred call with ID: {}", id);
// BeneficiaryPreferredCallEntity existingEntity = validateBeneficiaryPreferredCall(id);
// setIfUpdated(existingEntity::getCallId, existingEntity::setCallId, request.getCallId());
// setIfUpdated(existingEntity::getCompanyId, existingEntity::setCompanyId, request.getCompanyId());
//
// existingEntity = beneficiaryPreferredCallRepository.save(existingEntity);
//
// log.info("Beneficiary preferred call updated with ID: {}", existingEntity.getId());
// return convertEntityToResponse(existingEntity);
// }
public void deleteBeneficiaryPreferredCallById(HttpServletRequest request, Long id) {
log.info("Soft deleting beneficiary preferred call with ID: {}", id);
BeneficiaryPreferredCallEntity entity = validateBeneficiaryPreferredCall(id);
validator.validateUserId(request, entity.getUserId());
BeneficiaryPreferredCallEntity oldBeneficiaryPreferredCallEntity = Utils.getClonedEntityForData(entity);
entity.setIsDeleted(true);
beneficiaryPreferredCallRepository.save(entity);
/** This code is responsible for adding a version history log for the "Delete Beneficiary Preferred Call" operation. **/
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldBeneficiaryPreferredCallEntity).newData(entity).build());
log.info("Beneficiary preferred call soft deleted with ID: {}", id);
}
public List<BeneficiaryPreferredCallResponseBean> getAllBeneficiaryPreferredCalls(HttpServletRequest request) {
UserEntity userEntity = validator.validateUser(request);
log.info("Fetching all beneficiary preferred calls");
List<BeneficiaryPreferredCallResponseBean> calls = beneficiaryPreferredCallRepository.findByUserIdAndIsDeletedFalse(userEntity.getId())
.stream()
.map(this::convertEntityToResponse)
.collect(Collectors.toList());
log.info("Total beneficiary preferred calls found: {}", calls.size());
return calls;
}
private BeneficiaryPreferredCallEntity validateBeneficiaryPreferredCall(Long id) {
log.info("Validating beneficiary preferred call with ID: {}", id);
return beneficiaryPreferredCallRepository.findByIdAndIsDeletedFalse(id)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.BENEFICIARY_CALL_NOT_FOUND)));
}
private BeneficiaryPreferredCallResponseBean convertEntityToResponse(BeneficiaryPreferredCallEntity entity) {
BeneficiaryPreferredCallResponseBean response = new BeneficiaryPreferredCallResponseBean();
response.setId(entity.getId());
response.setBeneficiaryId(entity.getBeneficiaryId());
response.setStatus(BeneficiaryCallStatus.valueOf(entity.getStatus()));
response.setCallId(entity.getCallId());
response.setUserId(entity.getUserId());
response.setCompanyId(entity.getCompanyId());
response.setUserWithCompanyId(entity.getUserWithCompany().getId());
response.setCreatedDate(entity.getCreatedDate());
response.setUpdatedDate(entity.getUpdatedDate());
return response;
}
public void updateBeneficiaryPreferredCallStatus(Long id, BeneficiaryCallStatus status) {
log.info("Updating status for beneficiary preferred call with ID: {}", id);
BeneficiaryPreferredCallEntity existingEntity = validateBeneficiaryPreferredCall(id);
BeneficiaryPreferredCallEntity oldBeneficiaryPreferredCallEntity = Utils.getClonedEntityForData(existingEntity);
existingEntity.setStatus(status.getValue());
beneficiaryPreferredCallRepository.save(existingEntity);
/** This code is responsible for adding a version history log for the "Update Application" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(httpServletRequest).actionType(VersionActionTypeEnum.UPDATE).oldData(oldBeneficiaryPreferredCallEntity).newData(existingEntity).build());
log.info("Beneficiary preferred call status updated with ID: {}", existingEntity.getId());
}
public List<BeneficiaryPreferredCallResponseBean> getBeneficiaryPreferredCallByUserId(UserEntity userEntity, Long companyId) {
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(userEntity.getId(),companyId);
List<BeneficiaryPreferredCallEntity> calls = beneficiaryPreferredCallRepository.findByUserIdAndUserWithCompanyIdAndIsDeletedFalse(userEntity.getId(), userWithCompanyEntity.getId());
return calls.stream()
.map(this::convertEntityToResponse)
.collect(Collectors.toList());
}
}