Done ticket GEPAFINBE-32
This commit is contained in:
@@ -33,24 +33,26 @@ public class CompanyDao {
|
||||
@Autowired
|
||||
private UserWithCompanyRepository userWithCompanyRepository;
|
||||
|
||||
|
||||
public CompanyResponse createCompany(UserEntity userEntity, CompanyRequest companyRequest) {
|
||||
CompanyEntity existingCompany = companyRepository.findByVatNumber(companyRequest.getVatNumber());
|
||||
UserWithCompanyEntity userWithCompanyEntity = null;
|
||||
if (existingCompany != null) {
|
||||
UserWithCompanyEntity existingRelation = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), existingCompany.getId())
|
||||
.orElse(null);
|
||||
if (existingRelation == null) {
|
||||
createUserWithCompanyRelation(userEntity, existingCompany);
|
||||
userWithCompanyEntity = createUserWithCompanyRelation(userEntity, existingCompany, companyRequest.getIsLegalRepresentant());
|
||||
} else {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.USER_ALREADY_CONNECTED_TO_COMPANY));
|
||||
}
|
||||
return convertCompanyEntityToCompanyResponse(existingCompany);
|
||||
return convertCompanyEntityToCompanyResponse(existingCompany, userWithCompanyEntity);
|
||||
} else {
|
||||
validateCompany(companyRequest);
|
||||
CompanyEntity companyEntity = convertCompanyRequestToCompanyEntity(companyRequest);
|
||||
companyRepository.save(companyEntity);
|
||||
createUserWithCompanyRelation(userEntity, companyEntity);
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity);
|
||||
userWithCompanyEntity = createUserWithCompanyRelation(userEntity, companyEntity, companyRequest.getIsLegalRepresentant());
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,13 +74,14 @@ public class CompanyDao {
|
||||
}
|
||||
}
|
||||
|
||||
private UserWithCompanyEntity createUserWithCompanyRelation(UserEntity userEntity, CompanyEntity companyEntity) {
|
||||
private UserWithCompanyEntity createUserWithCompanyRelation(UserEntity userEntity, CompanyEntity companyEntity, Boolean isLegalRepresentant) {
|
||||
UserWithCompanyEntity userWithCompanyEntity = new UserWithCompanyEntity();
|
||||
if (userEntity.getBeneficiary() != null) {
|
||||
userWithCompanyEntity.setBeneficiaryId(userEntity.getBeneficiary().getId());
|
||||
}
|
||||
userWithCompanyEntity.setCompanyId(companyEntity.getId());
|
||||
userWithCompanyEntity.setUserId(userEntity.getId());
|
||||
userWithCompanyEntity.setIsLegalRepresentant(isLegalRepresentant);
|
||||
return userWithCompanyRepository.save(userWithCompanyEntity);
|
||||
}
|
||||
|
||||
@@ -100,7 +103,7 @@ public class CompanyDao {
|
||||
return entity;
|
||||
}
|
||||
|
||||
private CompanyResponse convertCompanyEntityToCompanyResponse(CompanyEntity entity) {
|
||||
private CompanyResponse convertCompanyEntityToCompanyResponse(CompanyEntity entity, UserWithCompanyEntity userWithCompanyEntity) {
|
||||
CompanyResponse response = new CompanyResponse();
|
||||
response.setId(entity.getId());
|
||||
response.setCompanyName(entity.getCompanyName());
|
||||
@@ -116,6 +119,9 @@ public class CompanyDao {
|
||||
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());
|
||||
return response;
|
||||
@@ -142,7 +148,11 @@ public class CompanyDao {
|
||||
Utils.setIfUpdated(companyEntity::getAnnualRevenue, companyEntity::setAnnualRevenue,
|
||||
companyRequest.getAnnualRevenue());
|
||||
companyRepository.save(companyEntity);
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity);
|
||||
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyId).orElse(null);
|
||||
Utils.setIfUpdated(userWithCompanyEntity::getIsLegalRepresentant, userWithCompanyEntity::setIsLegalRepresentant,
|
||||
companyRequest.getIsLegalRepresentant());
|
||||
userWithCompanyRepository.save(userWithCompanyEntity);
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
|
||||
}
|
||||
|
||||
public CompanyEntity validateCompany(Long companyId) {
|
||||
@@ -151,7 +161,8 @@ public class CompanyDao {
|
||||
}
|
||||
|
||||
public CompanyResponse getCompany(UserEntity userEntity, Long companyId) {
|
||||
return convertCompanyEntityToCompanyResponse(validateCompany(companyId));
|
||||
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyId).orElse(null);
|
||||
return convertCompanyEntityToCompanyResponse(validateCompany(companyId), userWithCompanyEntity);
|
||||
}
|
||||
|
||||
public void deleteCompany(UserEntity userEntity, Long companyId) {
|
||||
@@ -164,7 +175,10 @@ public class CompanyDao {
|
||||
UserEntity userEntity = userService.validateUser(userId);
|
||||
List<Long> companyIds = userWithCompanyRepository.findCompanyIdByUserId(userEntity.getId());
|
||||
List<CompanyEntity> list = companyRepository.findByIdIn(companyIds);
|
||||
return list.stream().map(this::convertCompanyEntityToCompanyResponse).toList();
|
||||
return list.stream().map(companyEntity->{
|
||||
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyEntity.getId()).orElse(null);
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
public UserWithCompanyEntity validateUserWithCompny(Long userId, Long companyId) {
|
||||
|
||||
Reference in New Issue
Block a user