Created new endpoint to update VAT number of company
This commit is contained in:
@@ -229,6 +229,7 @@ public class CompanyDao {
|
||||
response.setUpdatedDate(entity.getUpdatedDate());
|
||||
response.setContactName(userWithCompanyEntity.getContactName());
|
||||
response.setContactEmail(userWithCompanyEntity.getContactEmail());
|
||||
response.setValidVat(entity.getValidVat());
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -259,28 +260,6 @@ public class CompanyDao {
|
||||
//
|
||||
// }
|
||||
|
||||
//allow adding VAT later
|
||||
if(StringUtils.isNotBlank(companyRequest.getVatNumber())
|
||||
&& StringUtils.isBlank(companyEntity.getVatNumber())) {
|
||||
CompanyEntity existingCompany = companyRepository.findByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
||||
if(existingCompany!=null){
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.VATNUMBER_ALREADY_EXISTS));
|
||||
}
|
||||
Boolean validVat=Boolean.FALSE;
|
||||
if(companyRequest.getVatNumber()!=null){
|
||||
VatCheckResponseBean vatCheckResponseBean=vatCheckDao.checkVatNumber(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
||||
if(vatCheckResponseBean!=null && Boolean.TRUE.equals(vatCheckResponseBean.getValid())){
|
||||
validVat=Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
companyEntity.setVatNumber(companyRequest.getVatNumber());
|
||||
companyEntity.setValidVat(validVat);
|
||||
if(companyRequest.getVatCheckResponse() != null) {
|
||||
String responseJson = Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse());
|
||||
companyEntity.setJson(responseJson);
|
||||
updateCodiceAtecoFieldWithNewJson(companyEntity);
|
||||
}
|
||||
}
|
||||
companyRepository.save(companyEntity);
|
||||
log.info("Company updated and saved. companyId: {}", companyEntity.getId());
|
||||
|
||||
@@ -311,6 +290,40 @@ public class CompanyDao {
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates only the VAT number for the given company: runs VAT check, sets validVat and json (vat check response), and updates codiceAteco from response.
|
||||
*/
|
||||
public CompanyResponse updateCompanyVatNumber(UserEntity userEntity, Long companyId, String vatNumber) {
|
||||
log.info("Updating company VAT number. companyId: {}, userId: {}", companyId, userEntity.getId());
|
||||
if (StringUtils.isBlank(vatNumber)) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VAT_OR_TAX_CODE_REQUIRED));
|
||||
}
|
||||
CompanyEntity companyEntity = validateCompany(companyId);
|
||||
Long hubId = userEntity.getHub().getId();
|
||||
CompanyEntity existingCompany = companyRepository.findByVatNumberAndHubId(vatNumber, hubId);
|
||||
if (existingCompany != null && !existingCompany.getId().equals(companyId)) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.VATNUMBER_ALREADY_EXISTS));
|
||||
}
|
||||
VatCheckResponseBean vatCheckResponseBean = vatCheckDao.checkVatNumber(vatNumber, hubId);
|
||||
Boolean validVat = Boolean.FALSE;
|
||||
if (vatCheckResponseBean != null && Boolean.TRUE.equals(vatCheckResponseBean.getValid())) {
|
||||
validVat = Boolean.TRUE;
|
||||
}
|
||||
CompanyEntity oldCompanyData = Utils.getClonedEntityForData(companyEntity);
|
||||
companyEntity.setVatNumber(vatNumber);
|
||||
companyEntity.setValidVat(validVat);
|
||||
if (vatCheckResponseBean != null && vatCheckResponseBean.getVatCheckResponse() != null) {
|
||||
companyEntity.setJson(Utils.convertMapIntoJsonString(vatCheckResponseBean.getVatCheckResponse()));
|
||||
updateCodiceAtecoFieldWithNewJson(companyEntity);
|
||||
}
|
||||
companyRepository.save(companyEntity);
|
||||
log.info("Company VAT number updated and saved. companyId: {}", companyEntity.getId());
|
||||
loggingUtil.addVersionHistory(
|
||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCompanyData).newData(companyEntity).build());
|
||||
UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyId);
|
||||
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
|
||||
}
|
||||
|
||||
public CompanyEntity validateCompany(Long companyId) {
|
||||
log.info("Validating company. companyId: {}", companyId);
|
||||
return companyRepository.findById(companyId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
|
||||
Reference in New Issue
Block a user