Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/BeneficiaryPreferredCallDao.java

132 lines
6.8 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.enums.RoleStatusEnum;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.request.BeneficiaryPreferredCallReq;
import net.gepafin.tendermanagement.model.response.BeneficiaryPreferredCallResponseBean;
import net.gepafin.tendermanagement.repositories.BeneficiaryPreferredCallRepository;
import net.gepafin.tendermanagement.service.UserService;
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 java.util.List;
import java.util.stream.Collectors;
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
@Component
public class BeneficiaryPreferredCallDao {
private final Logger log = LoggerFactory.getLogger(BeneficiaryPreferredCallDao.class);
@Autowired
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
@Autowired
private UserService userService;
public BeneficiaryPreferredCallResponseBean createBeneficiaryPreferredCall(BeneficiaryPreferredCallReq request,UserEntity user) {
log.info("Creating new beneficiary preferred call with details: {}", request);
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();
UserEntity user= userService.validateUser(userEntity.getId());
if (user.getBeneficiary()!=null) {
entity.setBeneficiaryId(user.getBeneficiary().getId());
}
entity.setStatus(BeneficiaryCallStatus.ENABLED.getValue());
entity.setCallId(request.getCallId());
entity.setUserId(userEntity.getId());
entity.setCompanyId(request.getCompanyId());
return entity;
}
public BeneficiaryPreferredCallResponseBean getBeneficiaryPreferredCallById(Long id) {
log.info("Fetching beneficiary preferred call with ID: {}", id);
BeneficiaryPreferredCallEntity entity = validateBeneficiaryPreferredCall(id);
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);
// }
private boolean isUserABeneficiary(Long userId) {
UserEntity user=userService.validateUser(userId);
return RoleStatusEnum.ROLE_BENEFICIARY.getValue().equals(user.getRoleEntity().getRoleType());
}
public void deleteBeneficiaryPreferredCallById(Long id) {
log.info("Deleting beneficiary preferred call with ID: {}", id);
validateBeneficiaryPreferredCall(id);
beneficiaryPreferredCallRepository.deleteById(id);
log.info("Beneficiary preferred call deleted with ID: {}", id);
}
public List<BeneficiaryPreferredCallResponseBean> getAllBeneficiaryPreferredCalls() {
log.info("Fetching all beneficiary preferred calls");
List<BeneficiaryPreferredCallResponseBean> calls = beneficiaryPreferredCallRepository.findAll()
.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.findById(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.findByUserIdAndCompanyId(userEntity.getId(), companyId);
return calls.stream()
.map(this::convertEntityToResponse)
.collect(Collectors.toList());
}
}