diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java
index 71c63a9f..888a3ed5 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";
public static final String USER_REQUEST_COMPLETED="user.request.completed";
public static final String END_DATE_GREATER_THAN_NOW="end.date.greater.than.now";
diff --git a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java
index f9570895..4567d8b2 100644
--- a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java
+++ b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java
@@ -1800,7 +1800,6 @@ public class ApplicationDao {
if (expression.matches(".*\\{.*\\}.*")) {
return 0;
}
-
// Step 4: Evaluate the mathematical expression
return Utils.evaluateExpression(expression);
}
diff --git a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java
index ef5779fa..fa7b9ae8 100644
--- a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java
+++ b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java
@@ -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;
}
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 48131d63..6337ecde 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
@@ -2987,6 +2987,13 @@
+
+ select
+ setval('gepafin_schema.ndganag_id_seq', (select
+ max(id)+1
+ from gepafin_schema.ndganag), false)
+
+