diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java index 3874fc83..1aaf53ec 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java @@ -574,6 +574,7 @@ public class GepafinConstant { public static final String CREATE_NDG="CHECK_OR_CREATE_NDG_CODE"; public static final String NDG_NOT_FOUND="ndg.not.found"; public static final String EMAIL_PEC_REQUIRED="email.pec.cannot.null"; + public static final String COMPANY_NAME_JSON="denominazione"; } diff --git a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java index 76a7fcff..9889993e 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java @@ -540,10 +540,10 @@ public class AppointmentDao { // Try retrieving NDG by VAT number NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(company.getVatNumber()); AppointmentLoginResponse ndgResponse=new AppointmentLoginResponse(); - if (ndganagEntity != null || ndganagEntity.getNdg() != null) { + if (ndganagEntity != null && ndganagEntity.getNdg() != null) { ndgResponse.setNdg(ndganagEntity.getNdg()); }else { - ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application); + ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application,company); } if (isNdgValid(ndgResponse.getNdg())) { saveNdg(application, company, ndgResponse.getNdg()); @@ -720,20 +720,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); @@ -883,6 +901,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); @@ -1441,7 +1460,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()); @@ -1449,7 +1468,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); @@ -1460,8 +1479,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; } diff --git a/src/main/resources/db/changelog/db.changelog-1.0.0.xml b/src/main/resources/db/changelog/db.changelog-1.0.0.xml index 147dd7f7..78c3d4a2 100644 --- a/src/main/resources/db/changelog/db.changelog-1.0.0.xml +++ b/src/main/resources/db/changelog/db.changelog-1.0.0.xml @@ -2980,4 +2980,11 @@ + + select + setval('gepafin_schema.ndganag_id_seq', (select + max(id)+1 + from gepafin_schema.ndganag), false) + +