From fcdea2c593566289340aefc983f8041a9367ac5f Mon Sep 17 00:00:00 2001 From: piyushkag Date: Wed, 12 Feb 2025 11:48:06 +0530 Subject: [PATCH] Updated code for appointment creation and added a column in hub for appointment template id.. --- .../constants/AppointmentApiConstant.java | 4 +- .../constants/GepafinConstant.java | 3 + .../tendermanagement/dao/AppointmentDao.java | 64 ++++++++++++------- .../tendermanagement/entities/HubEntity.java | 3 + .../request/AppointmentCreationRequest.java | 2 +- .../feignClient/AppointmentApiService.java | 2 +- .../db/changelog/db.changelog-1.0.0.xml | 8 +++ src/main/resources/message_en.properties | 2 + src/main/resources/message_it.properties | 2 + 9 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java index c82687a5..6e5ed97c 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java @@ -26,7 +26,7 @@ public class AppointmentApiConstant { //Appointment creation request body fields public static final String MOTIVAZIONE = "motivazione"; - public static final String COD_PRODOTTO = "codProdotto"; + public static final String PRODOTTO = "prodotto"; public static final String COD_ABI = "codAbi"; public static final String COD_CAB = "codCab"; public static final String ID_NOTA = "idNota"; @@ -37,5 +37,5 @@ public class AppointmentApiConstant { public static final String COD_FORMATECNICA = "codFormaTecnica"; public static final String COD_OPERAZIONE = "codOperazione"; public static final String MOTIVAZIONE_ID = "id"; - + public static final String PRODOTTO_CODE = "code"; } diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java index 5eb5128b..0b29c773 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java @@ -423,6 +423,9 @@ public class GepafinConstant { public static final String FORMULA_AMOUNT_NOT_MATCHED="formula.amount.not.matches.requested.amount"; public static final String CRITERIA_TABLE_COLUMNS="criteria_table_columns"; + + public static final String APPOINTMENT_CANNOT_BE_CREATED = "appointment.cannot.be.created"; + public static final String APPOINTMENT_CANNOT_BE_CREATED_BY_TEMPLATE = "appointment.cannot.be.created.by.template"; } diff --git a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java index 8bf0b4a4..f6fbd088 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java @@ -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 response = appointmentApiService.getAppointmentTemplateForTemplateCreation(authorizationToken, areaCode); + Long appointmentTemplateId = hub.getAppointmentTemplateId(); + ResponseEntity 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 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); diff --git a/src/main/java/net/gepafin/tendermanagement/entities/HubEntity.java b/src/main/java/net/gepafin/tendermanagement/entities/HubEntity.java index ccd6c325..ff6db1a6 100644 --- a/src/main/java/net/gepafin/tendermanagement/entities/HubEntity.java +++ b/src/main/java/net/gepafin/tendermanagement/entities/HubEntity.java @@ -64,6 +64,9 @@ public class HubEntity extends BaseEntity{ @Column(name = "AREA_CODE") private String areaCode; + @Column(name = "APPOINTMENT_TEMPLATE_ID") + private Long appointmentTemplateId; + @Column(name = "EVALUATION_EXPIRATION_DAYS") private Long evaluationExpirationDays; } diff --git a/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java b/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java index 0bd6b812..a1c0bb12 100644 --- a/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java +++ b/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java @@ -11,7 +11,7 @@ public class AppointmentCreationRequest { @Data public static class Input { - private String id; + private Long id; private String ndg; private List richiestaCliente; } diff --git a/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java b/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java index 1c3a44f7..036a2904 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java +++ b/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java @@ -32,7 +32,7 @@ public interface AppointmentApiService { ResponseEntity getVisuraList(@RequestBody String visuraRequest, @RequestHeader("Authorization") String token); @GetMapping(value = AppointmentApiConstant.GET_APPOINTMENT_TEMPLATE, consumes = MediaType.APPLICATION_JSON_VALUE) - ResponseEntity getAppointmentTemplateForTemplateCreation(@RequestHeader("Authorization") String token, @RequestParam("idAppuntamentoTemplate") int templateId); + ResponseEntity getAppointmentTemplateForTemplateCreation(@RequestHeader("Authorization") String token, @RequestParam("idAppuntamentoTemplate") Long templateId); @PostMapping(value = AppointmentApiConstant.CREATE_APPOINTMENT_FROM_TEMPLATE, consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity createAppointment(@RequestHeader("Authorization") String token, @RequestHeader("context") String context, String appointmentCreationRequest); 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 37c34586..6acccf72 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 @@ -2418,4 +2418,12 @@ + + + + UNIQUE_UUID = 'p4lk3bcx1RStqTaIVVbXs' + + + diff --git a/src/main/resources/message_en.properties b/src/main/resources/message_en.properties index 7aab6f08..87163fb4 100644 --- a/src/main/resources/message_en.properties +++ b/src/main/resources/message_en.properties @@ -371,4 +371,6 @@ validation.required.requested.amount=The Requested Amount configuration should b company.id.not.null=Company ID cannot be null. formula.amount.not.matches.requested.amount= The {0} does not matches to calculated amount. +appointment.cannot.be.created = Appointment cannot be created because call doesn't have the productId. +appointment.cannot.be.created.by.template = Product ID does not match with appointment template. diff --git a/src/main/resources/message_it.properties b/src/main/resources/message_it.properties index c1794a8d..66da42e2 100644 --- a/src/main/resources/message_it.properties +++ b/src/main/resources/message_it.properties @@ -362,4 +362,6 @@ validation.required.requested.amount=La configurazione dell'importo richiesto company.id.not.null=L'ID dell'azienda non pu? essere nullo. formula.amount.not.matches.requested.amount=Il {0} non corrisponde all'importo calcolato. +appointment.cannot.be.created = Impossibile creare l'appuntamento perch� la chiamata non ha l'ID prodotto. +appointment.cannot.be.created.by.template = L'ID prodotto non corrisponde al modello di appuntamento.