Merge pull request #338 from Kitzanos/fixed-ndg-sequence-issue

Fixed NDG sequence issue
This commit is contained in:
Rinaldo
2025-08-01 12:11:23 +02:00
committed by GitHub
4 changed files with 56 additions and 9 deletions

View File

@@ -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";

View File

@@ -1800,7 +1800,6 @@ public class ApplicationDao {
if (expression.matches(".*\\{.*\\}.*")) {
return 0;
}
// Step 4: Evaluate the mathematical expression
return Utils.evaluateExpression(expression);
}

View File

@@ -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;
}

View File

@@ -2987,6 +2987,13 @@
</addColumn>
</changeSet>
<changeSet id="26-06-2025_RK_110723" author="Rajesh Khore">
<sql dbms="postgresql">select
setval('gepafin_schema.ndganag_id_seq', (select
max(id)+1
from gepafin_schema.ndganag), false)
</sql>
</changeSet>
<changeSet id="27-06-2025_RK_155914" author="Rajesh Khore">
<sqlFile dbms="postgresql"
path="db/dump/insert_system_email_template_technical_evaluation_rejected.sql"/>