package net.gepafin.tendermanagement.dao; import java.util.List; import net.gepafin.tendermanagement.entities.*; import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum; import net.gepafin.tendermanagement.repositories.ApplicationRepository; import net.gepafin.tendermanagement.repositories.FaqRepository; import net.gepafin.tendermanagement.web.rest.api.errors.*; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import net.gepafin.tendermanagement.model.request.CompanyRequest; import net.gepafin.tendermanagement.model.response.CompanyResponse; import net.gepafin.tendermanagement.repositories.CompanyRepository; import net.gepafin.tendermanagement.repositories.UserWithCompanyRepository; import net.gepafin.tendermanagement.service.UserService; import net.gepafin.tendermanagement.util.Utils; import static net.gepafin.tendermanagement.util.Utils.setIfUpdated; @Component public class CompanyDao { @Autowired private CompanyRepository companyRepository; @Autowired private UserService userService; @Autowired private UserWithCompanyRepository userWithCompanyRepository; @Autowired private ApplicationRepository applicationRepository; @Autowired private FaqRepository faqRepository; public CompanyResponse createCompany(UserEntity userEntity, CompanyRequest companyRequest) { CompanyEntity existingCompany = companyRepository.findByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId()); UserWithCompanyEntity userWithCompanyEntity = null; if (existingCompany != null) { UserWithCompanyEntity existingRelation = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), existingCompany.getId()) .orElse(null); if (existingRelation == null) { userWithCompanyEntity = createUserWithCompanyRelation(userEntity, existingCompany, companyRequest.getIsLegalRepresentant()); } else { throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.USER_ALREADY_CONNECTED_TO_COMPANY)); } return convertCompanyEntityToCompanyResponse(existingCompany, userWithCompanyEntity); } else { validateCompany(userEntity, companyRequest); CompanyEntity companyEntity = convertCompanyRequestToCompanyEntity(userEntity, companyRequest); companyRepository.save(companyEntity); userWithCompanyEntity = createUserWithCompanyRelation(userEntity, companyEntity, companyRequest.getIsLegalRepresentant()); return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity); } } private void validateCompany(UserEntity userEntity, CompanyRequest companyRequest) { if (Boolean.FALSE.equals(StringUtils.isEmpty(companyRequest.getEmail())) && Boolean.FALSE.equals(Utils.isValidEmail(companyRequest.getEmail()))) { throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_EMAIL)); } if (StringUtils.isEmpty(companyRequest.getVatNumber())) { throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VATNUMBER_MANDATORY)); } if (companyRepository.existsByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId())) { throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VATNUMBER_ALREADY_EXISTS)); } } private UserWithCompanyEntity createUserWithCompanyRelation(UserEntity userEntity, CompanyEntity companyEntity, Boolean isLegalRepresentant) { UserWithCompanyEntity userWithCompanyEntity = new UserWithCompanyEntity(); if (userEntity.getBeneficiary() != null) { userWithCompanyEntity.setBeneficiaryId(userEntity.getBeneficiary().getId()); } userWithCompanyEntity.setIsDeleted(Boolean.FALSE); userWithCompanyEntity.setCompanyId(companyEntity.getId()); userWithCompanyEntity.setUserId(userEntity.getId()); userWithCompanyEntity.setIsLegalRepresentant(isLegalRepresentant); return userWithCompanyRepository.save(userWithCompanyEntity); } private CompanyEntity convertCompanyRequestToCompanyEntity(UserEntity userEntity, CompanyRequest request) { CompanyEntity entity = new CompanyEntity(); entity.setCompanyName(request.getCompanyName()); entity.setVatNumber(request.getVatNumber()); entity.setCodiceFiscale(request.getCodiceFiscale()); entity.setAddress(request.getAddress()); entity.setPhoneNumber(request.getPhoneNumber()); entity.setCity(request.getCity()); entity.setProvince(request.getProvince()); entity.setCap(request.getCap()); entity.setCountry(request.getCountry()); entity.setPec(request.getPec()); entity.setEmail(request.getEmail()); entity.setNumberOfEmployees(request.getNumberOfEmployees()); entity.setAnnualRevenue(request.getAnnualRevenue()); entity.setContactName(request.getContactName()); entity.setContactEmail(request.getContactEmail()); entity.setHub(userEntity.getHub()); return entity; } private CompanyResponse convertCompanyEntityToCompanyResponse(CompanyEntity entity, UserWithCompanyEntity userWithCompanyEntity) { CompanyResponse response = new CompanyResponse(); response.setId(entity.getId()); response.setCompanyName(entity.getCompanyName()); response.setVatNumber(entity.getVatNumber()); response.setCodiceFiscale(entity.getCodiceFiscale()); response.setAddress(entity.getAddress()); response.setPhoneNumber(entity.getPhoneNumber()); response.setCity(entity.getCity()); response.setProvince(entity.getProvince()); response.setCap(entity.getCap()); response.setCountry(entity.getCountry()); response.setPec(entity.getPec()); response.setEmail(entity.getEmail()); response.setNumberOfEmployees(entity.getNumberOfEmployees()); response.setAnnualRevenue(entity.getAnnualRevenue()); if(userWithCompanyEntity!=null) { response.setIsLegalRepresentant(userWithCompanyEntity.getIsLegalRepresentant()); } response.setCreatedDate(entity.getCreatedDate()); response.setUpdatedDate(entity.getUpdatedDate()); response.setContactName(entity.getContactName()); response.setContactEmail(entity.getContactEmail()); return response; } public CompanyResponse updateCompany(UserEntity userEntity, Long companyId, CompanyRequest companyRequest) { CompanyEntity companyEntity = validateCompany(companyId); setIfUpdated(companyEntity::getCompanyName, companyEntity::setCompanyName, companyRequest.getCompanyName()); setIfUpdated(companyEntity::getVatNumber, companyEntity::setVatNumber, companyRequest.getVatNumber()); setIfUpdated(companyEntity::getCodiceFiscale, companyEntity::setCodiceFiscale, companyRequest.getCodiceFiscale()); setIfUpdated(companyEntity::getAddress, companyEntity::setAddress, companyRequest.getAddress()); setIfUpdated(companyEntity::getPhoneNumber, companyEntity::setPhoneNumber, companyRequest.getPhoneNumber()); setIfUpdated(companyEntity::getCity, companyEntity::setCity, companyRequest.getCity()); setIfUpdated(companyEntity::getProvince, companyEntity::setProvince, companyRequest.getProvince()); setIfUpdated(companyEntity::getCap, companyEntity::setCap, companyRequest.getCap()); setIfUpdated(companyEntity::getCountry, companyEntity::setCountry, companyRequest.getCountry()); setIfUpdated(companyEntity::getPec, companyEntity::setPec, companyRequest.getPec()); setIfUpdated(companyEntity::getEmail, companyEntity::setEmail, companyRequest.getEmail()); setIfUpdated(companyEntity::getNumberOfEmployees, companyEntity::setNumberOfEmployees, companyRequest.getNumberOfEmployees()); setIfUpdated(companyEntity::getAnnualRevenue, companyEntity::setAnnualRevenue, companyRequest.getAnnualRevenue()); setIfUpdated(companyEntity::getContactName,companyEntity::setContactName,companyRequest.getContactName()); setIfUpdated(companyEntity::getContactEmail,companyEntity::setContactEmail,companyRequest.getContactEmail()); companyRepository.save(companyEntity); UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyId); Utils.setIfUpdated(userWithCompanyEntity::getIsLegalRepresentant, userWithCompanyEntity::setIsLegalRepresentant, companyRequest.getIsLegalRepresentant()); userWithCompanyRepository.save(userWithCompanyEntity); return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity); } public CompanyEntity validateCompany(Long companyId) { return companyRepository.findById(companyId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMPANY_NOT_FOUND_MSG))); } public CompanyResponse getCompany(UserEntity userEntity, Long companyId) { UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyId); return convertCompanyEntityToCompanyResponse(validateCompany(companyId), userWithCompanyEntity); } public void deleteCompany(UserEntity userEntity, Long companyId) { CompanyEntity companyEntity = validateCompany(companyId); companyRepository.delete(companyEntity); userWithCompanyRepository.deleteByCompanyIdAndIsDeletedFalse(companyId); } public List getCompanyByUserId(Long userId) { UserEntity userEntity = userService.validateUser(userId); List activeCompanyIds = userWithCompanyRepository.findActiveCompanyIdsByUserId(userEntity.getId()); List companies = companyRepository.findByIdInAndHubId(activeCompanyIds, userEntity.getHub().getId()); return companies.stream().map(companyEntity -> { UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyEntity.getId()); return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity); }).toList(); } public UserWithCompanyEntity validateUserWithCompny(Long userId, Long companyId) { return userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userId, companyId).orElseThrow(() -> new ForbiddenAccessException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PERMISSION_DENIED))); } public UserWithCompanyEntity getUserWithCompany(Long userId, Long compnayId) { return userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userId, compnayId).orElseThrow( () -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_COMPANY_RELATION_NOT_FOUND))); } public void removeCompanyFromList(UserEntity userEntity, Long companyId) { CompanyEntity companyEntity = validateCompany(companyId); UserWithCompanyEntity existingRelation = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyEntity.getId()) .orElseThrow(() -> new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.USER_ALREADY_CONNECTED_TO_COMPANY))); List userApplications = applicationRepository.findByCompanyIdAndUserIdAndIsDeletedFalse(companyEntity.getId(), userEntity.getId()); List faqs = faqRepository.findByCompanyIdAndUserIdAndIsDeletedFalse(companyEntity.getId(), userEntity.getId()); for (ApplicationEntity application : userApplications) { if(Boolean.TRUE.equals(application.getStatus().equals(ApplicationStatusTypeEnum.SUBMIT.getValue()))) { throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.CANNOT_DELETE_COMPANY_WITH_APPLICATION_SUBMITT)); } if(Boolean.TRUE.equals(application.getStatus().equals(ApplicationStatusTypeEnum.DRAFT.getValue()))) { application.setIsDeleted(Boolean.TRUE); applicationRepository.save(application); } } for(FaqEntity faq:faqs) { faq.setIsDeleted(Boolean.TRUE); faqRepository.save(faq); } existingRelation.setIsDeleted(Boolean.TRUE); userWithCompanyRepository.save(existingRelation); } }