Updated code for appointment creation and added a column in hub for appointment template id..
This commit is contained in:
@@ -384,6 +384,7 @@ public class AppointmentDao {
|
||||
// Validate and save token
|
||||
if (parsedResponse.getTokenId() != null) {
|
||||
hub.setAppointmentAuthTokenId(parsedResponse.getTokenId());
|
||||
hub.setAreaCode(parsedResponse.getAreaCode());
|
||||
hubRepository.save(hub);
|
||||
|
||||
// /** This code is responsible for adding a version history log for the "inserting token and areaCode from login odessa response for
|
||||
@@ -626,8 +627,8 @@ public class AppointmentDao {
|
||||
|
||||
// Generate authorization token and fetch template data
|
||||
String authorizationToken = getBearerToken(hub);
|
||||
int areaCode = Integer.parseInt(hub.getAreaCode());
|
||||
ResponseEntity<Object> response = appointmentApiService.getAppointmentTemplateForTemplateCreation(authorizationToken, areaCode);
|
||||
Long appointmentTemplateId = hub.getAppointmentTemplateId();
|
||||
ResponseEntity<Object> response = appointmentApiService.getAppointmentTemplateForTemplateCreation(authorizationToken, appointmentTemplateId);
|
||||
|
||||
if (response.getStatusCode() != HttpStatus.OK) {
|
||||
log.error("Failed to retrieve appointment template for appointment creation. Status: {}", response.getStatusCode());
|
||||
@@ -636,10 +637,10 @@ public class AppointmentDao {
|
||||
|
||||
// Parse template data
|
||||
String responseDataForTemplate = Utils.convertObjectToJson(response.getBody());
|
||||
AppointmentCreationRequest templateRichiestaData = parseTemplateResponseData(responseDataForTemplate);
|
||||
AppointmentCreationRequest templateRichiestaData = parseTemplateResponseData(responseDataForTemplate, application.getCall().getProductId());
|
||||
|
||||
// Build the appointment request body
|
||||
AppointmentCreationRequest appointmentCreationRequest = buildAppointmentCreationRequest(applicationId, createAppointmentRequest, hub.getAreaCode(),
|
||||
AppointmentCreationRequest appointmentCreationRequest = buildAppointmentCreationRequest(applicationId, createAppointmentRequest, hub.getAppointmentTemplateId(),
|
||||
templateRichiestaData);
|
||||
String appointmentRequestBody = Utils.convertObjectToJson(appointmentCreationRequest);
|
||||
|
||||
@@ -682,8 +683,12 @@ public class AppointmentDao {
|
||||
return null;
|
||||
}
|
||||
|
||||
public AppointmentCreationRequest parseTemplateResponseData(String jsonResponse) {
|
||||
public AppointmentCreationRequest parseTemplateResponseData(String jsonResponse, Long productId) {
|
||||
|
||||
try {
|
||||
if (productId == null) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPOINTMENT_CANNOT_BE_CREATED));
|
||||
}
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(jsonResponse);
|
||||
JsonNode richiesteClienteArray = rootNode.path(GepafinConstant.DATA_STRING).path(GepafinConstant.RICHIESTE_CLIENTE_STRING);
|
||||
@@ -694,26 +699,38 @@ public class AppointmentDao {
|
||||
List<AppointmentCreationRequest.RichiestaCliente> richiestaClienteList = new ArrayList<>();
|
||||
if (!richiesteClienteArray.isArray()) {
|
||||
log.warn("richiesteCliente array is missing or not an array.");
|
||||
return new AppointmentCreationRequest(); // Return empty object if array is missing
|
||||
return new AppointmentCreationRequest();
|
||||
}
|
||||
|
||||
boolean isMatchedAtLeastOneId = false;
|
||||
for (JsonNode richiestaNode : richiesteClienteArray) {
|
||||
if (richiestaNode.isNull()) continue;
|
||||
if (richiestaNode.isNull())
|
||||
continue;
|
||||
|
||||
AppointmentCreationRequest.RichiestaCliente richiestaCliente = new AppointmentCreationRequest.RichiestaCliente();
|
||||
richiestaCliente.setIdMotivazione(getIntValue(richiestaNode));
|
||||
richiestaCliente.setCodProdotto(getTextValue(richiestaNode, AppointmentApiConstant.COD_PRODOTTO));
|
||||
richiestaCliente.setCodAbi(getTextValue(richiestaNode, AppointmentApiConstant.COD_ABI));
|
||||
richiestaCliente.setCodCab(getTextValue(richiestaNode, AppointmentApiConstant.COD_CAB));
|
||||
richiestaCliente.setIdNota(getTextValue(richiestaNode, AppointmentApiConstant.ID_NOTA));
|
||||
richiestaCliente.setImportoAgevolato(getTextValue(richiestaNode, AppointmentApiConstant.IMPORTO_AGEVOLATO));
|
||||
richiestaCliente.setImportoMedioLungoTermine(getTextValue(richiestaNode, AppointmentApiConstant.IMPORTO_MEDIOLUNGO_TERMINE));
|
||||
richiestaCliente.setCodTipoProdotto(getTextValue(richiestaNode, AppointmentApiConstant.COD_TIPO_PRODOTTO));
|
||||
richiestaCliente.setCodCategoriaProdotto(getTextValue(richiestaNode, AppointmentApiConstant.COD_CATEGORIA_PRODOTTO));
|
||||
richiestaCliente.setCodFormaTecnica(getTextValue(richiestaNode, AppointmentApiConstant.COD_FORMATECNICA));
|
||||
richiestaCliente.setCodOperazione(getTextValue(richiestaNode, AppointmentApiConstant.COD_OPERAZIONE));
|
||||
JsonNode prodottoNode = richiestaNode.path(AppointmentApiConstant.PRODOTTO);
|
||||
String prodottoCode = prodottoNode.path(AppointmentApiConstant.PRODOTTO_CODE).asText();
|
||||
|
||||
richiestaClienteList.add(richiestaCliente);
|
||||
if (productId.toString().equals(prodottoCode)) {
|
||||
isMatchedAtLeastOneId = true;
|
||||
richiestaCliente.setCodProdotto(prodottoCode);
|
||||
richiestaCliente.setIdMotivazione(getIntValue(richiestaNode));
|
||||
richiestaCliente.setCodProdotto(productId.toString());
|
||||
richiestaCliente.setCodAbi(getTextValue(richiestaNode, AppointmentApiConstant.COD_ABI));
|
||||
richiestaCliente.setCodCab(getTextValue(richiestaNode, AppointmentApiConstant.COD_CAB));
|
||||
richiestaCliente.setIdNota(getTextValue(richiestaNode, AppointmentApiConstant.ID_NOTA));
|
||||
richiestaCliente.setImportoAgevolato(getTextValue(richiestaNode, AppointmentApiConstant.IMPORTO_AGEVOLATO));
|
||||
richiestaCliente.setImportoMedioLungoTermine(getTextValue(richiestaNode, AppointmentApiConstant.IMPORTO_MEDIOLUNGO_TERMINE));
|
||||
richiestaCliente.setCodTipoProdotto(getTextValue(richiestaNode, AppointmentApiConstant.COD_TIPO_PRODOTTO));
|
||||
richiestaCliente.setCodCategoriaProdotto(getTextValue(richiestaNode, AppointmentApiConstant.COD_CATEGORIA_PRODOTTO));
|
||||
richiestaCliente.setCodFormaTecnica(getTextValue(richiestaNode, AppointmentApiConstant.COD_FORMATECNICA));
|
||||
richiestaCliente.setCodOperazione(getTextValue(richiestaNode, AppointmentApiConstant.COD_OPERAZIONE));
|
||||
|
||||
richiestaClienteList.add(richiestaCliente);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isMatchedAtLeastOneId) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPOINTMENT_CANNOT_BE_CREATED_BY_TEMPLATE));
|
||||
}
|
||||
|
||||
input.setRichiestaCliente(richiestaClienteList);
|
||||
@@ -724,21 +741,20 @@ public class AppointmentDao {
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("JSON processing error: {}", e.getMessage(), e);
|
||||
throw new IllegalStateException("Invalid JSON structure in template response", e);
|
||||
} catch (Exception e) {
|
||||
log.error("Unexpected error while parsing template response: {}", e.getMessage(), e);
|
||||
throw new IllegalStateException("Failed to parse template response", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String getTextValue(JsonNode node, String fieldName) {
|
||||
|
||||
return node.path(fieldName).isTextual() ? node.path(fieldName).asText() : null;
|
||||
}
|
||||
|
||||
private int getIntValue(JsonNode node) {
|
||||
|
||||
return node.path(AppointmentApiConstant.MOTIVAZIONE).path(AppointmentApiConstant.MOTIVAZIONE_ID).asInt();
|
||||
}
|
||||
|
||||
public AppointmentCreationRequest buildAppointmentCreationRequest(Long applicationId, CreateAppointmentRequest createAppointmentRequest, String areaCode,
|
||||
public AppointmentCreationRequest buildAppointmentCreationRequest(Long applicationId, CreateAppointmentRequest createAppointmentRequest, Long areaCode,
|
||||
AppointmentCreationRequest templateRichiestaData) {
|
||||
|
||||
ApplicationEntity application = applicationService.validateApplication(applicationId);
|
||||
|
||||
Reference in New Issue
Block a user