Done ticket GEPAFINBE-223 & GEPAFINBE-224
This commit is contained in:
@@ -138,6 +138,9 @@ public class AppointmentDao {
|
||||
@Autowired
|
||||
private ApplicationEvaluationDao applicationEvaluationDao;
|
||||
|
||||
@Autowired
|
||||
private NdganagRepository ndganagRepository;
|
||||
|
||||
private final Map<Long, ScheduledExecutorService> executorMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@@ -535,7 +538,13 @@ public class AppointmentDao {
|
||||
String authorizationToken = getBearerToken(hub);
|
||||
|
||||
// Try retrieving NDG by VAT number
|
||||
AppointmentLoginResponse ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application);
|
||||
NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(company.getVatNumber());
|
||||
AppointmentLoginResponse ndgResponse=new AppointmentLoginResponse();
|
||||
if (ndganagEntity != null || ndganagEntity.getNdg() != null) {
|
||||
ndgResponse.setNdg(ndganagEntity.getNdg());
|
||||
}else {
|
||||
ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application);
|
||||
}
|
||||
if (isNdgValid(ndgResponse.getNdg())) {
|
||||
saveNdg(application, company, ndgResponse.getNdg());
|
||||
log.info("NDG successfully generated for applicationId: {}", applicationId);
|
||||
@@ -716,10 +725,7 @@ public class AppointmentDao {
|
||||
try {
|
||||
log.info("Initiating NDG retrieval by VAT number | ApplicationId: {}, HubId: {}, VAT: {}", application.getId(), hub.getId(), vatNumber);
|
||||
// Prepare the NDG request
|
||||
AppointmentNdgRequest ndgRequest = getAppointmentNdgRequest(vatNumber);
|
||||
// Call the API to retrieve NDG
|
||||
ResponseEntity<Object> response = appointmentApiService.getNdgByVatNumber(ndgRequest, authorizationToken);
|
||||
String responseJson = Utils.convertObjectToJson(response.getBody());
|
||||
String responseJson = getNdgFromExternalService(vatNumber, authorizationToken);
|
||||
// Parse and return the NDG response
|
||||
return parseNdgResponse(responseJson);
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
@@ -875,30 +881,38 @@ 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);
|
||||
}
|
||||
return loginResponse;
|
||||
}
|
||||
|
||||
private String extractNdg(String jsonResponse) {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(jsonResponse);
|
||||
JsonNode dataArray = rootNode.get(GepafinConstant.DATA_STRING);
|
||||
if (dataArray == null || !dataArray.isArray() || dataArray.isEmpty()) {
|
||||
log.info("NDG data is empty or missing in the response.");
|
||||
AppointmentLoginResponse emptyResponse = new AppointmentLoginResponse();
|
||||
emptyResponse.setNdg(null);
|
||||
return emptyResponse;
|
||||
return null;
|
||||
}
|
||||
JsonNode firstDataEntry = dataArray.get(0);
|
||||
AppointmentLoginResponse response = new AppointmentLoginResponse();
|
||||
if (firstDataEntry.has(GepafinConstant.NDG_STRING)) {
|
||||
response.setNdg(normalizeNullValue(firstDataEntry.get(GepafinConstant.NDG_STRING).asText()));
|
||||
String ndg=normalizeNullValue(firstDataEntry.get(GepafinConstant.NDG_STRING).asText());
|
||||
return ndg;
|
||||
}
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to parse response: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Failed to parse NDG response.", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String normalizeNullValue(String value) {
|
||||
public String normalizeNullValue(String value) {
|
||||
|
||||
return (value == null || GepafinConstant.NULL_STRING.equalsIgnoreCase(value.trim())) ? null : value;
|
||||
}
|
||||
@@ -1415,5 +1429,54 @@ public class AppointmentDao {
|
||||
futureRef.set(future);
|
||||
}
|
||||
|
||||
public NdgResponse getNdgByVatNumber(String vatNumber, UserEntity userEntity) {
|
||||
HubEntity hub=userEntity.getHub();
|
||||
NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(vatNumber);
|
||||
NdgResponse ndgResponse=new NdgResponse();
|
||||
String jsonResponse=null;
|
||||
String authorizationToken = hub.getAppointmentAuthTokenId();
|
||||
if (ndganagEntity == null || ndganagEntity.getNdg() == null) {
|
||||
|
||||
try {
|
||||
log.info("Initiating NDG retrieval by VAT number | HubId: {}, VAT: {}", hub.getId(), vatNumber);
|
||||
// Prepare the NDG request
|
||||
jsonResponse=getNdgFromExternalService(vatNumber, authorizationToken);
|
||||
checkAndSaveNdg(jsonResponse, ndgResponse);
|
||||
// Parse and return the NDG response
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
log.error("403 Forbidden during NDG retrieval | HubId: {}", hub.getId());
|
||||
logForbiddenError();
|
||||
// Regenerate the token and retry
|
||||
String newAuthorizationToken = regenerateTokenAndSave(hub, null);
|
||||
jsonResponse= getNdgFromExternalService(vatNumber,newAuthorizationToken);
|
||||
if (checkAndSaveNdg(jsonResponse, ndgResponse)) 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);
|
||||
}
|
||||
}else {
|
||||
ndgResponse.setNdg(ndganagEntity.getNdg());
|
||||
}
|
||||
return ndgResponse;
|
||||
}
|
||||
|
||||
private boolean checkAndSaveNdg(String jsonResponse, NdgResponse ndgResponse) {
|
||||
String ndg=extractNdg(jsonResponse);
|
||||
if (ndg==null){
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
ndgResponse.setNdg(ndg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getNdgFromExternalService(String vatNumber, String authorizationToken) {
|
||||
AppointmentNdgRequest ndgRequest = getAppointmentNdgRequest(vatNumber);
|
||||
// Call the API to retrieve NDG
|
||||
ResponseEntity<Object> response = appointmentApiService.getNdgByVatNumber(ndgRequest, authorizationToken);
|
||||
String responseJson = Utils.convertObjectToJson(response.getBody());
|
||||
return responseJson;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user