Done ticket GEPAFINBE-205 Handled the old and new vatCheck company json through versioning.
This commit is contained in:
@@ -136,8 +136,9 @@ public class GepafinConstant {
|
||||
public static final String UPDATING_FORM_VALUE_IMPACT_ON_FLOW = "updating.form.value.impact.on.flow";
|
||||
public static final String APPLICATION_IS_INCOMPLETE_MSG = "application.is.incomplete";
|
||||
public static final String AUTHORIZATION = "Authorization";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL = "https://imprese.openapi.it/advance";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE = "https://company.openapi.com/IT-advanced";
|
||||
public static final String CHECK_VATNUMBER_URL_V1 = "https://imprese.openapi.it/advance";
|
||||
public static final String CHECK_VATNUMBER_URL_V2 = "https://company.openapi.com/IT-advanced";
|
||||
public static final String VAT_CHECK_API_VERSION = "VAT_CHECK_API_VERSION";
|
||||
public static final String VALIDATION_FIELD_CUSTOM = "validation.field.custom";
|
||||
public static final String VALIDATION_CODICE_FISCALE = "validation.codice.fiscale";
|
||||
public static final String VALIDATION_CAP = "validation.cap";
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.data.domain.Pageable; // Correct package
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -20,7 +18,6 @@ import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
||||
import net.gepafin.tendermanagement.repositories.FaqRepository;
|
||||
import net.gepafin.tendermanagement.service.feignClient.VatCheckService;
|
||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -37,7 +34,6 @@ import net.gepafin.tendermanagement.model.response.CompanyResponse;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
|
||||
import static net.gepafin.tendermanagement.util.Utils.convertObjectToJsonString;
|
||||
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
|
||||
|
||||
@Component
|
||||
@@ -73,10 +69,6 @@ public class CompanyDao {
|
||||
private HttpServletRequest request;
|
||||
|
||||
|
||||
@Autowired
|
||||
private VatCheckService vatCheckService; // Service to call VAT API
|
||||
|
||||
|
||||
private static final String NOT_FOUND_JSON = "{\"data\": \"not found\"}";
|
||||
|
||||
public CompanyResponse createCompany(UserEntity userEntity, CompanyRequest companyRequest) {
|
||||
@@ -146,8 +138,6 @@ public class CompanyDao {
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(userWithCompany).build());
|
||||
if (StringUtils.isEmpty(companyEntity.getJson())) {
|
||||
companyEntity.setJson(Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse()));
|
||||
Map<String, Object> vatCheckResponse = companyRequest.getVatCheckResponse();
|
||||
Map<String, Object> data = (Map<String, Object>) vatCheckResponse.get("data");
|
||||
updateCodiceAtecoFieldWithNewJson(companyEntity);
|
||||
companyEntity = companyRepository.save(companyEntity);
|
||||
|
||||
@@ -178,7 +168,7 @@ public class CompanyDao {
|
||||
entity.setAnnualRevenue(request.getAnnualRevenue());
|
||||
entity.setHub(userEntity.getHub());
|
||||
entity.setJson(Utils.convertMapIntoJsonString(request.getVatCheckResponse()));
|
||||
updateCodiceAtecoFieldWithNewJson(entity);
|
||||
updateCodiceAtecoFieldWithNewJson(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
@@ -437,7 +427,6 @@ public class CompanyDao {
|
||||
log.info("Company ID {}: JSON field updated successfully.", company.getId());
|
||||
|
||||
// Extract and set codiceAteco field
|
||||
// updateCodiceAtecoField(company);
|
||||
updateCodiceAtecoFieldWithNewJson(company);
|
||||
|
||||
successfulUpdates++;
|
||||
@@ -472,50 +461,38 @@ public class CompanyDao {
|
||||
private void updateCodiceAtecoField(CompanyEntity company) {
|
||||
Map<String, Object> vatCheckResponse = Utils.convertJsonStringToMap(company.getJson());
|
||||
|
||||
if (vatCheckResponse != null && vatCheckResponse.containsKey("data")) {
|
||||
Object dataObj = vatCheckResponse.get("data");
|
||||
|
||||
if (dataObj instanceof Map) {
|
||||
Map<String, Object> dataMap = (Map<String, Object>) dataObj;
|
||||
log.info("Company ID {}: Available keys inside 'data' -> {}", company.getId(), dataMap.keySet());
|
||||
|
||||
if (dataMap.containsKey("dettaglio")) {
|
||||
Object dettaglioObj = dataMap.get("dettaglio");
|
||||
|
||||
if (dettaglioObj instanceof Map) {
|
||||
Map<String, Object> dettaglio = (Map<String, Object>) dettaglioObj;
|
||||
|
||||
if (dettaglio.containsKey("codice_ateco")) {
|
||||
Object codiceAtecoObj = dettaglio.get("codice_ateco");
|
||||
|
||||
if (codiceAtecoObj instanceof String) {
|
||||
String codiceAteco = (String) codiceAtecoObj;
|
||||
|
||||
if (codiceAteco != null && !codiceAteco.isEmpty()) {
|
||||
company.setCodiceAteco(codiceAteco);
|
||||
log.info("Company ID {}: codiceAteco updated to {}", company.getId(), codiceAteco);
|
||||
} else {
|
||||
log.warn("Company ID {}: codiceAteco is null or empty in the response.", company.getId());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'codice_ateco' is not a string, actual type: {}", company.getId(), codiceAtecoObj.getClass());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'dettaglio' does not contain 'codice_ateco' key.", company.getId());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'dettaglio' is not a Map, actual type: {}", company.getId(), dettaglioObj.getClass());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'dettaglio' section is missing inside 'data'!", company.getId());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'data' is not a Map, actual type: {}", company.getId(), dataObj.getClass());
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'data' section missing in the JSON response.", company.getId());
|
||||
if (vatCheckResponse == null) {
|
||||
log.warn("Company ID {}: Invalid JSON response.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
Object dataObj = vatCheckResponse.get("data");
|
||||
if (!(dataObj instanceof Map<?, ?> dataMap)) {
|
||||
log.warn("Company ID {}: 'data' is missing or not a valid object.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dataMap.containsKey("dettaglio")) {
|
||||
log.warn("Company ID {}: 'dettaglio' not present inside 'data'. Skipping codiceAteco update.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
Object dettaglioObj = dataMap.get("dettaglio");
|
||||
if (!(dettaglioObj instanceof Map<?, ?> dettaglio)) {
|
||||
log.warn("Company ID {}: 'dettaglio' is not a valid object.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
Object codiceAtecoObj = dettaglio.get("codice_ateco");
|
||||
if (!(codiceAtecoObj instanceof String codiceAteco) || codiceAteco.isEmpty()) {
|
||||
log.warn("Company ID {}: 'codice_ateco' is missing, empty, or not a string.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
company.setCodiceAteco(codiceAteco);
|
||||
logCodiceAtecoUpdate(company, codiceAteco);
|
||||
}
|
||||
|
||||
private void updateCodiceAtecoFieldWithNewJson(CompanyEntity company) {
|
||||
if (company == null || company.getJson() == null || company.getJson().isEmpty()) {
|
||||
log.warn("Company is null or JSON data is empty.");
|
||||
@@ -530,32 +507,43 @@ public class CompanyDao {
|
||||
|
||||
// Extract 'data' section
|
||||
Map<String, Object> dataMap = Utils.extractMap(companyDataMap, "data");
|
||||
Object dataObj = companyDataMap.get("data");
|
||||
|
||||
if (dataMap == null) {
|
||||
log.warn("Company ID {}: 'data' section is missing or invalid in the JSON.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract 'atecoClassification' section
|
||||
Map<String, Object> atecoClassificationMap = Utils.extractMap(dataMap, "atecoClassification");
|
||||
if (atecoClassificationMap == null) {
|
||||
log.warn("Company ID {}: 'atecoClassification' section is missing or invalid.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract 'ateco' section
|
||||
Map<String, Object> atecoMap = Utils.extractMap(atecoClassificationMap, "ateco");
|
||||
if (atecoMap == null) {
|
||||
log.warn("Company ID {}: 'ateco' section is missing or invalid.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract and set 'code'
|
||||
String atecoCode = Utils.extractString(atecoMap, "code");
|
||||
if (atecoCode != null && !atecoCode.isEmpty()) {
|
||||
company.setCodiceAteco(atecoCode);
|
||||
log.info("Company ID {}: codiceAteco updated to {}", company.getId(), atecoCode);
|
||||
if (dataObj instanceof Map<?, ?>) {
|
||||
// if data is a single object
|
||||
updateCodiceAtecoField(company);
|
||||
} else {
|
||||
log.warn("Company ID {}: 'code' inside 'ateco' is empty or missing.", company.getId());
|
||||
// Extract 'atecoClassification' section
|
||||
Map<String, Object> atecoClassificationMap = Utils.extractMap(dataMap, "atecoClassification");
|
||||
if (atecoClassificationMap == null) {
|
||||
log.warn("Company ID {}: 'atecoClassification' section is missing or invalid.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract 'ateco' section
|
||||
Map<String, Object> atecoMap = Utils.extractMap(atecoClassificationMap, "ateco");
|
||||
if (atecoMap == null) {
|
||||
log.warn("Company ID {}: 'ateco' section is missing or invalid.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract and set 'code'
|
||||
String atecoCode = Utils.extractString(atecoMap, "code");
|
||||
if (atecoCode != null && !atecoCode.isEmpty()) {
|
||||
company.setCodiceAteco(atecoCode);
|
||||
logCodiceAtecoUpdate(company, atecoCode);
|
||||
} else {
|
||||
log.warn("Company ID {}: 'code' inside 'ateco' is empty or missing.", company.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void logCodiceAtecoUpdate(CompanyEntity company, String atecoCode) {
|
||||
|
||||
log.info("Company ID {}: codiceAteco updated to {}", company.getId(), atecoCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,7 +511,7 @@ public class FormDao {
|
||||
// Map<String, Object> customData=null;
|
||||
try {
|
||||
// Map<String, Object> vatCheckResponse = vatCheckDao.checkVatNumberApi(value);
|
||||
vatCheckDao.checkVatNumberApi(value);
|
||||
vatCheckDao.checkVatNumber(value);
|
||||
// if (Boolean.FALSE.equals(CollectionUtils.isEmpty(vatCheckResponse))) {
|
||||
// customData = vatCheckResponse;
|
||||
// }
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import feign.FeignException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.CompanyEntity;
|
||||
import net.gepafin.tendermanagement.enums.VatCheckVersionTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.VatCheckResponseBean;
|
||||
import net.gepafin.tendermanagement.service.feignClient.VatCheckService;
|
||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.repositories.GlobalConfigRepository;
|
||||
import net.gepafin.tendermanagement.service.feignClient.VatCheckV1Service;
|
||||
import net.gepafin.tendermanagement.service.feignClient.VatCheckV2Service;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,8 +21,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,104 +31,103 @@ import java.util.Map;
|
||||
public class VatCheckDao {
|
||||
|
||||
@Autowired
|
||||
private VatCheckService vatCheckService;
|
||||
private VatCheckV2Service vatCheckV2Service;
|
||||
|
||||
@Value("${vatCheckNewToken}")
|
||||
public String vatCheckNewToken;
|
||||
@Autowired
|
||||
private VatCheckV1Service vatCheckV1Service;
|
||||
|
||||
@Value("${vatCheckTokenV2}")
|
||||
public String vatCheckTokenV2;
|
||||
|
||||
@Value("${vatCheckTokenV1}")
|
||||
public String vatCheckTokenV1;
|
||||
|
||||
@Value("${isVatCheckGloballyDisabled}")
|
||||
public String isVatCheckGloballyDisabled;
|
||||
|
||||
@Autowired
|
||||
private GlobalConfigRepository globalConfigRepository;
|
||||
|
||||
public final Logger log = LoggerFactory.getLogger(VatCheckDao.class);
|
||||
|
||||
@Autowired
|
||||
private LoggingUtil loggingUtil;
|
||||
public VatCheckResponseBean checkVatNumberV1(String vatNumber) {
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
if (Boolean.TRUE.equals(Boolean.parseBoolean(isVatCheckGloballyDisabled))) {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckTokenV1);
|
||||
headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_URL_V1);
|
||||
ResponseEntity<Map<String, Object>> response = vatCheckV1Service.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
|
||||
// public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
|
||||
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
// vatCheckResponseBean.setValid(false);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// if (Boolean.TRUE.equals(Boolean.parseBoolean(isVatCheckGloballyDisabled))) {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// Map<String, Object> responseBody = new HashMap<>();
|
||||
// try {
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
// headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
// headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
|
||||
// headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
|
||||
//
|
||||
// URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL);
|
||||
// ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
//
|
||||
//
|
||||
// if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
|
||||
// log.info("Successfully checked vat number");
|
||||
// Map<String, Object> responseMap = response.getBody();
|
||||
// processValidResponse(responseMap, vatCheckResponseBean);
|
||||
// }
|
||||
// } catch (FeignException ex) {
|
||||
// if (ex.status() == 406) {
|
||||
// try {
|
||||
// Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
|
||||
// processValidResponse(errorResponse, vatCheckResponseBean);
|
||||
// } catch (Exception parseEx) {
|
||||
// log.error("Failed to parse 406 error response: {0}", parseEx);
|
||||
// }
|
||||
// } else {
|
||||
// log.error("Exception occurred while checking vat number: {0}", ex);
|
||||
// Utils.callException(ex.status(), ex);
|
||||
// }
|
||||
// }
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
|
||||
// if (responseMap != null && responseMap.containsKey("data")) {
|
||||
// Map<String, Object> responseBody = (Map<String, Object>) responseMap.get("data");
|
||||
//
|
||||
// if (responseBody != null) {
|
||||
// responseBody.remove("timestamp_creation");
|
||||
// responseBody.remove("timestamp_last_update");
|
||||
// responseBody.remove("data_iscrizione");
|
||||
// responseBody.remove("id");
|
||||
//
|
||||
// Map<String, Object> data = new LinkedHashMap<>();
|
||||
// data.put("data", responseBody);
|
||||
//
|
||||
// vatCheckResponseBean.setValid(true);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
|
||||
// vatCheckResponseBean.setVatCheckResponse(data);
|
||||
// } else {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// }
|
||||
// } else {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public VatCheckResponseBean checkVatNumber(String vatNumber) {
|
||||
// try {
|
||||
// return checkVatNumberApi(vatNumber);
|
||||
// } catch (Exception e) {
|
||||
// log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
// vatCheckResponseBean.setValid(false);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// }
|
||||
if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
|
||||
logSuccess();
|
||||
Map<String, Object> responseMap = response.getBody();
|
||||
if (responseMap != null) {
|
||||
processValidResponseV1(responseMap, vatCheckResponseBean);
|
||||
} else {
|
||||
log.warn("Response map or vatCheckResponseBean is null.");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (FeignException ex) {
|
||||
if (ex.status() == 406) {
|
||||
try {
|
||||
Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
|
||||
processValidResponseV1(errorResponse, vatCheckResponseBean);
|
||||
} catch (Exception parseEx) {
|
||||
log.error("Failed to parse 406 error response: {0}", parseEx);
|
||||
}
|
||||
} else {
|
||||
log.error("Exception occurred while checking vat number: {0}", ex);
|
||||
Utils.callException(ex.status(), ex);
|
||||
}
|
||||
}
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
|
||||
public static void processValidResponseV1(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
|
||||
Object dataObj = responseMap.get("data");
|
||||
if (dataObj instanceof Map<?, ?> rawDataMap) {
|
||||
Map<String, Object> responseBody = new LinkedHashMap<>();
|
||||
rawDataMap.forEach((k, v) -> {
|
||||
if (k instanceof String strKey) {
|
||||
responseBody.put(strKey, v);
|
||||
}
|
||||
});
|
||||
|
||||
List.of("timestamp_creation", "timestamp_last_update", "id", "data_iscrizione").forEach(responseBody.keySet()::remove);
|
||||
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("data", responseBody);
|
||||
|
||||
vatCheckResponseBean.setValid(true);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
|
||||
vatCheckResponseBean.setVatCheckResponse(data);
|
||||
} else {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public VatCheckResponseBean checkVatNumber(String vatNumber) {
|
||||
|
||||
try {
|
||||
return checkVatNumberApi(vatNumber);
|
||||
String vatApiVersion = getVatCheckVersion();
|
||||
if(!isVatCheckApiV2(vatApiVersion)){
|
||||
return checkVatNumberV1(vatNumber);
|
||||
}
|
||||
return checkVatNumberV2(vatNumber);
|
||||
} catch (Exception e) {
|
||||
log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
logErrorMessage(e);
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
@@ -135,7 +135,7 @@ public class VatCheckDao {
|
||||
}
|
||||
}
|
||||
|
||||
public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
|
||||
public VatCheckResponseBean checkVatNumberV2(String vatNumber) {
|
||||
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
@@ -147,13 +147,13 @@ public class VatCheckDao {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
|
||||
headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckTokenV2);
|
||||
|
||||
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE);
|
||||
ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_URL_V2);
|
||||
ResponseEntity<Map<String, Object>> response = vatCheckV2Service.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
|
||||
log.info("Successfully checked vat number");
|
||||
logSuccess();
|
||||
Map<String, Object> responseMap = response.getBody();
|
||||
processValidResponse(responseMap, vatCheckResponseBean);
|
||||
}
|
||||
@@ -172,33 +172,65 @@ public class VatCheckDao {
|
||||
}
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
|
||||
public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
|
||||
|
||||
if (responseMap != null && responseMap.containsKey("data")) {
|
||||
Object dataObject = responseMap.get("data");
|
||||
|
||||
if (dataObject instanceof List && !((List<?>) dataObject).isEmpty()) {
|
||||
dataObject = ((List<?>) dataObject).get(0);
|
||||
}
|
||||
|
||||
if (dataObject instanceof Map) {
|
||||
Map<String, Object> responseBody = (Map<String, Object>) dataObject;
|
||||
|
||||
responseBody.remove("creationTimestamp");
|
||||
responseBody.remove("lastUpdateTimestamp");
|
||||
responseBody.remove("id");
|
||||
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("data", responseBody);
|
||||
|
||||
vatCheckResponseBean.setValid(true);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
|
||||
vatCheckResponseBean.setVatCheckResponse(data);
|
||||
} else {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
}
|
||||
} else {
|
||||
if (responseMap == null || !responseMap.containsKey("data")) {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return;
|
||||
}
|
||||
|
||||
Object dataObject = responseMap.get("data");
|
||||
if (!(dataObject instanceof List<?> dataList) || dataList.isEmpty()) {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> processedList = new ArrayList<>();
|
||||
|
||||
for (Object item : dataList) {
|
||||
if (item instanceof Map<?, ?> mapItem) {
|
||||
Map<String, Object> responseBody = new LinkedHashMap<>();
|
||||
mapItem.forEach((key, value) -> {
|
||||
if (key instanceof String strKey) {
|
||||
responseBody.put(strKey, value);
|
||||
}
|
||||
});
|
||||
|
||||
List.of("creationTimestamp", "lastUpdateTimestamp", "id").forEach(responseBody.keySet()::remove);
|
||||
|
||||
processedList.add(responseBody);
|
||||
}
|
||||
}
|
||||
|
||||
if (processedList.isEmpty()) {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return;
|
||||
}
|
||||
|
||||
vatCheckResponseBean.setValid(true);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
|
||||
vatCheckResponseBean.setVatCheckResponse(Map.of("data", processedList));
|
||||
}
|
||||
|
||||
private void logSuccess() {
|
||||
|
||||
log.info("Successfully checked vat number");
|
||||
}
|
||||
|
||||
private static boolean isVatCheckApiV2(String vatApiVersion) {
|
||||
boolean isNotBlank = StringUtils.isNotBlank(vatApiVersion);
|
||||
boolean isNotEmpty = StringUtils.isNotEmpty(vatApiVersion);
|
||||
return isNotBlank && isNotEmpty && Boolean.TRUE.equals(vatApiVersion.equals(VatCheckVersionTypeEnum.V2.getValue()));
|
||||
}
|
||||
|
||||
private String getVatCheckVersion() {
|
||||
|
||||
return globalConfigRepository.findContentByTypeAndIsDeletedFalse(GepafinConstant.VAT_CHECK_API_VERSION);
|
||||
}
|
||||
|
||||
private void logErrorMessage(Exception e) {
|
||||
|
||||
log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.gepafin.tendermanagement.entities;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "GLOBAL_CONFIG")
|
||||
public class GlobalConfigEntity extends BaseEntity{
|
||||
|
||||
@Column(name = "CONTENT")
|
||||
private String content;
|
||||
|
||||
@Column(name = "TYPE")
|
||||
private String type;
|
||||
|
||||
@Column(name = "IS_DELETED")
|
||||
private Boolean isDeleted = false;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.gepafin.tendermanagement.enums;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum VatCheckVersionTypeEnum {
|
||||
V1("V1"),
|
||||
V2("V2");
|
||||
private String value;
|
||||
|
||||
VatCheckVersionTypeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.gepafin.tendermanagement.repositories;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.GlobalConfigEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface GlobalConfigRepository extends JpaRepository<GlobalConfigEntity, Long> {
|
||||
|
||||
@Query("SELECT gc.content FROM GlobalConfigEntity gc WHERE gc.type = :vatCheckApiVersion AND gc.isDeleted = false")
|
||||
String findContentByTypeAndIsDeletedFalse(String vatCheckApiVersion);
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package net.gepafin.tendermanagement.service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.gepafin.tendermanagement.model.request.LimitRequest;
|
||||
import net.gepafin.tendermanagement.model.response.VatCheckResponseBean;
|
||||
|
||||
@@ -11,13 +11,9 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(value = "vat-check-service", url = GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE)
|
||||
public interface VatCheckService {
|
||||
|
||||
@FeignClient(value = "vat-check-v1-service", url = GepafinConstant.CHECK_VATNUMBER_URL_V1)
|
||||
public interface VatCheckV1Service {
|
||||
|
||||
@GetMapping("/{vatNumber}")
|
||||
ResponseEntity<Map<String, Object>> checkVatNumber(URI baseUrl,
|
||||
@PathVariable("vatNumber") String vatNumber,
|
||||
@RequestHeader HttpHeaders headers
|
||||
);
|
||||
ResponseEntity<Map<String, Object>> checkVatNumber(URI baseUrl, @PathVariable("vatNumber") String vatNumber, @RequestHeader HttpHeaders headers);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.gepafin.tendermanagement.service.feignClient;
|
||||
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(value = "vat-check-v2-service", url = GepafinConstant.CHECK_VATNUMBER_URL_V2)
|
||||
public interface VatCheckV2Service {
|
||||
|
||||
@GetMapping("/{vatNumber}")
|
||||
ResponseEntity<Map<String, Object>> checkVatNumber(URI baseUrl, @PathVariable("vatNumber") String vatNumber, @RequestHeader HttpHeaders headers);
|
||||
}
|
||||
@@ -47,7 +47,8 @@ fe.base.url=https://bandi-staging.memento.credit
|
||||
spring.main.allow-circular-references=true
|
||||
|
||||
isVatCheckGloballyDisabled = true
|
||||
vatCheckNewToken: 671916c76c6e822f660774d4
|
||||
vatCheckTokenV1: 66026bd891a51044e90e08c4
|
||||
vatCheckTokenV2: 671916c76c6e822f660774d4
|
||||
|
||||
#SPID configuration
|
||||
spid.ipd.base.url=https://federatest.umbriadigitale.it
|
||||
|
||||
@@ -2695,4 +2695,31 @@
|
||||
path="db/dump/create_application_amendment_request_view.sql"/>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="03-04-2025_PK_152010" author="Piyush kag">
|
||||
<createTable tableName="global_config">
|
||||
<column name="id" type="INTEGER" autoIncrement="true">
|
||||
<constraints primaryKey="true" primaryKeyName="global_config_pkey" nullable="false"/>
|
||||
</column>
|
||||
<column name="content" type="TEXT"/>
|
||||
<column name="type" type="TEXT"/>
|
||||
<column name="is_deleted" type="BOOLEAN" defaultValueBoolean="false"/>
|
||||
<column name="created_date" type="TIMESTAMP WITHOUT TIME ZONE">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="updated_date" type="TIMESTAMP WITHOUT TIME ZONE">
|
||||
<constraints nullable="true"/>
|
||||
</column>
|
||||
</createTable>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="03-04-2025_PK_154010" author="Piyush kag">
|
||||
<insert tableName="global_config">
|
||||
<column name="type" value="VAT_CHECK_API_VERSION"/>
|
||||
<column name="content" value="V2"/>
|
||||
<column name="created_date" value="2025-04-03 05:00:00"/>
|
||||
<column name="updated_date" value="2024-04-03 05:00:00"/>
|
||||
<column name="is_deleted" value="false"/>
|
||||
</insert>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
|
||||
Reference in New Issue
Block a user