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,
|
||||
|
||||
@@ -60,6 +60,7 @@ public enum UserActionContextEnum {
|
||||
CREATE_COMPANY("CREATE_COMPANY"),
|
||||
GET_COMPANY("GET_COMPANY"),
|
||||
UPDATE_COMPANY("UPDATE_COMPANY"),
|
||||
UPDATE_COMPANY_VAT_NUMBER("UPDATE_COMPANY_VAT_NUMBER"),
|
||||
DELETE_COMPANY("DELETE_COMPANY"),
|
||||
UPLOAD_COMPANY_DELEGATION("UPLOAD_COMPANY_DELEGATION"),
|
||||
DOWNLOAD_COMPANY_DELEGATION_TEMPLATE("DOWNLOAD_COMPANY_DELEGATION_TEMPLATE"),
|
||||
|
||||
@@ -25,4 +25,5 @@ public class CompanyResponse extends BaseBean{
|
||||
private String contactName;
|
||||
private String contactEmail;
|
||||
private String codiceAteco;
|
||||
private Boolean validVat;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ public interface CompanyService {
|
||||
|
||||
CompanyResponse updateCompany(HttpServletRequest request, Long companyId, CompanyRequest companyRequest);
|
||||
|
||||
CompanyResponse updateCompanyVatNumber(HttpServletRequest request, Long companyId, String vatNumber);
|
||||
|
||||
CompanyResponse getCompany(HttpServletRequest request, Long companyId);
|
||||
|
||||
void deleteCompany(HttpServletRequest request, Long companyId);
|
||||
|
||||
@@ -56,6 +56,14 @@ public class CompanyServiceImpl implements CompanyService {
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
return companyDao.updateCompany(userEntity, companyId, companyRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public CompanyResponse updateCompanyVatNumber(HttpServletRequest request, Long companyId, String vatNumber) {
|
||||
UserEntity userEntity = validator.validateUser(request);
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
return companyDao.updateCompanyVatNumber(userEntity, companyId, vatNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
|
||||
@@ -55,6 +55,19 @@ public interface CompanyApi {
|
||||
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId,
|
||||
@Parameter(description = "Company request object", required = true) @RequestBody CompanyRequest companyRequest);
|
||||
|
||||
@Operation(summary = "Api to update only company VAT number", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
||||
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
|
||||
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
|
||||
@PutMapping(value = "/{companyId}/vatNumber", produces = { "application/json" })
|
||||
ResponseEntity<Response<CompanyResponse>> updateCompanyVatNumber(HttpServletRequest request,
|
||||
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId,
|
||||
@Parameter(description = "VAT number", required = true) @RequestParam("vatNumber") String vatNumber);
|
||||
|
||||
|
||||
@Operation(summary = "Api to delete company", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
||||
|
||||
@@ -73,6 +73,15 @@ public class CompanyApiController implements CompanyApi{
|
||||
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_UPDATED_SUCCESS_MSG)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Response<CompanyResponse>> updateCompanyVatNumber(HttpServletRequest request, Long companyId, String vatNumber) {
|
||||
log.info("Update company VAT number with companyId: {}, vatNumber: {}", companyId, vatNumber);
|
||||
loggingUtil.logUserAction(UserActionRequest.builder().request(request).actionType(UserActionLogsEnum.UPDATE).actionContext(UserActionContextEnum.UPDATE_COMPANY_VAT_NUMBER).build());
|
||||
CompanyResponse data = companyService.updateCompanyVatNumber(request, companyId, vatNumber);
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_UPDATED_SUCCESS_MSG)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Response<CompanyResponse>> getCompany(HttpServletRequest request, Long companyId) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user