|
|
|
|
@@ -545,9 +545,13 @@ public class AppointmentDao {
|
|
|
|
|
String authorizationToken = getBearerToken(hub);
|
|
|
|
|
|
|
|
|
|
// Try retrieving NDG by VAT number
|
|
|
|
|
NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(company.getVatNumber());
|
|
|
|
|
AppointmentLoginResponse ndgResponse=new AppointmentLoginResponse();
|
|
|
|
|
ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application);
|
|
|
|
|
|
|
|
|
|
if (ndganagEntity != null && ndganagEntity.getNdg() != null) {
|
|
|
|
|
ndgResponse.setNdg(ndganagEntity.getNdg());
|
|
|
|
|
}else {
|
|
|
|
|
ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application,company);
|
|
|
|
|
}
|
|
|
|
|
if (isNdgValid(ndgResponse.getNdg())) {
|
|
|
|
|
saveNdg(application, company, ndgResponse.getNdg());
|
|
|
|
|
log.info("NDG successfully generated for applicationId: {}", applicationId);
|
|
|
|
|
@@ -723,20 +727,38 @@ public class AppointmentDao {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AppointmentLoginResponse retrieveNdgByVatNumber(String vatNumber, String authorizationToken, HubEntity hub, ApplicationEntity application) {
|
|
|
|
|
private AppointmentLoginResponse retrieveNdgByVatNumber(String vatNumber, String authorizationToken, HubEntity hub, ApplicationEntity application,CompanyEntity company) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
log.info("Initiating NDG retrieval by VAT number | ApplicationId: {}, HubId: {}, VAT: {}", application.getId(), hub.getId(), vatNumber);
|
|
|
|
|
// Prepare the NDG request
|
|
|
|
|
String responseJson = getNdgFromExternalService(vatNumber, authorizationToken);
|
|
|
|
|
// Parse and return the NDG response
|
|
|
|
|
return parseNdgResponse(responseJson);
|
|
|
|
|
AppointmentLoginResponse loginResponse=parseNdgResponse(responseJson);
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
JsonNode rootNode = null;
|
|
|
|
|
try {
|
|
|
|
|
rootNode = objectMapper.readTree(responseJson);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
JsonNode dataArray = rootNode.get(GepafinConstant.DATA_STRING);
|
|
|
|
|
JsonNode firstDataEntry = dataArray.get(0);
|
|
|
|
|
NdganagEntity ndganagEntity=new NdganagEntity();
|
|
|
|
|
ndganagEntity.setNdg(loginResponse.getNdg());
|
|
|
|
|
ndganagEntity.setVatNumber(loginResponse.getVatNumber());
|
|
|
|
|
ndganagEntity.setCodiceFiscale(loginResponse.getCodecFiscale());
|
|
|
|
|
ndganagEntity.setJson(firstDataEntry.toString());
|
|
|
|
|
ndganagEntity.setIsDeleted(Boolean.FALSE);
|
|
|
|
|
ndganagEntity.setCompanyName(company.getCompanyName());
|
|
|
|
|
ndganagRepository.save(ndganagEntity);
|
|
|
|
|
return loginResponse;
|
|
|
|
|
} catch (FeignException.Forbidden forbiddenException) {
|
|
|
|
|
log.error("403 Forbidden during NDG retrieval | ApplicationId: {}, HubId: {}", application.getId(), hub.getId());
|
|
|
|
|
logForbiddenError();
|
|
|
|
|
// Regenerate the token and retry
|
|
|
|
|
String newAuthorizationToken = regenerateTokenAndSave(hub, application);
|
|
|
|
|
return retrieveNdgByVatNumber(vatNumber, newAuthorizationToken, hub, application);
|
|
|
|
|
return retrieveNdgByVatNumber(vatNumber, newAuthorizationToken, hub, application,company);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error during NDG retrieval | ApplicationId: {}, HubId: {}, Message: {}", application.getId(), hub.getId(), e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("NDG retrieval failed.", e);
|
|
|
|
|
@@ -886,6 +908,7 @@ public class AppointmentDao {
|
|
|
|
|
public AppointmentLoginResponse parseNdgResponse(String jsonResponse) {
|
|
|
|
|
AppointmentLoginResponse loginResponse = new AppointmentLoginResponse();
|
|
|
|
|
String ndg=extractNdg(jsonResponse);
|
|
|
|
|
|
|
|
|
|
if (ndg==null){ return null;}
|
|
|
|
|
else {
|
|
|
|
|
loginResponse.setNdg(ndg);
|
|
|
|
|
@@ -1444,7 +1467,7 @@ public class AppointmentDao {
|
|
|
|
|
log.info("Initiating NDG retrieval by VAT number | HubId: {}, VAT: {}", hub.getId(), vatNumber);
|
|
|
|
|
// Prepare the NDG request
|
|
|
|
|
jsonResponse=getNdgFromExternalService(vatNumber, authorizationToken);
|
|
|
|
|
checkAndSaveNdg(jsonResponse, ndgResponse);
|
|
|
|
|
checkAndSaveNdg(jsonResponse, ndgResponse,vatNumber);
|
|
|
|
|
// Parse and return the NDG response
|
|
|
|
|
} catch (FeignException.Forbidden forbiddenException) {
|
|
|
|
|
log.error("403 Forbidden during NDG retrieval | HubId: {}", hub.getId());
|
|
|
|
|
@@ -1452,7 +1475,7 @@ public class AppointmentDao {
|
|
|
|
|
// Regenerate the token and retry
|
|
|
|
|
String newAuthorizationToken = regenerateTokenAndSave(hub, null);
|
|
|
|
|
jsonResponse= getNdgFromExternalService(vatNumber,newAuthorizationToken);
|
|
|
|
|
if (checkAndSaveNdg(jsonResponse, ndgResponse)) return null;
|
|
|
|
|
if (checkAndSaveNdg(jsonResponse, ndgResponse,vatNumber)) return null;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error during NDG retrieval |, HubId: {}, Message: {}", hub.getId(), e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("NDG retrieval failed.", e);
|
|
|
|
|
@@ -1463,8 +1486,25 @@ public class AppointmentDao {
|
|
|
|
|
return ndgResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean checkAndSaveNdg(String jsonResponse, NdgResponse ndgResponse) {
|
|
|
|
|
private boolean checkAndSaveNdg(String jsonResponse, NdgResponse ndgResponse,String vatNumber) {
|
|
|
|
|
String ndg=extractNdg(jsonResponse);
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
JsonNode rootNode = null;
|
|
|
|
|
try {
|
|
|
|
|
rootNode = objectMapper.readTree(jsonResponse);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
JsonNode dataArray = rootNode.get(GepafinConstant.DATA_STRING);
|
|
|
|
|
JsonNode firstDataEntry = dataArray.get(0);
|
|
|
|
|
String comapnyName=normalizeNullValue(firstDataEntry.get(GepafinConstant.COMPANY_NAME_JSON).asText());
|
|
|
|
|
NdganagEntity ndganagEntity=new NdganagEntity();
|
|
|
|
|
ndganagEntity.setNdg(ndg);
|
|
|
|
|
ndganagEntity.setVatNumber(vatNumber);
|
|
|
|
|
ndganagEntity.setJson(firstDataEntry.toString());
|
|
|
|
|
ndganagEntity.setIsDeleted(Boolean.FALSE);
|
|
|
|
|
ndganagEntity.setCompanyName(comapnyName);
|
|
|
|
|
ndganagRepository.save(ndganagEntity);
|
|
|
|
|
if (ndg==null){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|