Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/BeneficiaryPreferredCallDao.java
2024-11-13 13:08:48 +05:30

133 lines
6.9 KiB
Java

package net.gepafin.tendermanagement.dao;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.BeneficiaryPreferredCallEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.enums.BeneficiaryCallStatus;
import net.gepafin.tendermanagement.model.request.BeneficiaryPreferredCallReq;
import net.gepafin.tendermanagement.model.response.BeneficiaryPreferredCallResponseBean;
import net.gepafin.tendermanagement.repositories.BeneficiaryPreferredCallRepository;
import net.gepafin.tendermanagement.util.Validator;
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.stream.Collectors;
@Component
public class BeneficiaryPreferredCallDao {
private final Logger log = LoggerFactory.getLogger(BeneficiaryPreferredCallDao.class);
@Autowired
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
@Autowired
private Validator validator;
public BeneficiaryPreferredCallResponseBean createBeneficiaryPreferredCall(HttpServletRequest httpServletRequest, BeneficiaryPreferredCallReq request,UserEntity user) {
log.info("Creating new beneficiary preferred call with details: {}", request);
validator.validateUserWithCompany(httpServletRequest, request.getCompanyId());
BeneficiaryPreferredCallEntity entity = convertRequestToEntity(request,user);
entity = beneficiaryPreferredCallRepository.save(entity);
log.info("Beneficiary preferred call created with ID: {}", entity.getId());
return convertEntityToResponse(entity);
}
private BeneficiaryPreferredCallEntity convertRequestToEntity(BeneficiaryPreferredCallReq request,UserEntity userEntity) {
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(request.getCompanyId());
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());
entity.setIsDeleted(true);
beneficiaryPreferredCallRepository.save(entity);
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.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);
existingEntity.setStatus(status.getValue());
beneficiaryPreferredCallRepository.save(existingEntity);
log.info("Beneficiary preferred call status updated with ID: {}", existingEntity.getId());
}
public List<BeneficiaryPreferredCallResponseBean> getBeneficiaryPreferredCallByUserId(UserEntity userEntity, Long companyId) {
List<BeneficiaryPreferredCallEntity> calls = beneficiaryPreferredCallRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyId);
return calls.stream()
.map(this::convertEntityToResponse)
.collect(Collectors.toList());
}
}