From d787fdcd0eccb55608ff98be6f7589efa3044d0e Mon Sep 17 00:00:00 2001 From: piyushkag Date: Tue, 11 Feb 2025 15:33:46 +0530 Subject: [PATCH] Updated Appointment code for creating appointment on external system. --- .../constants/AppointmentApiConstant.java | 16 ++++- .../constants/GepafinConstant.java | 2 +- .../tendermanagement/dao/AppointmentDao.java | 64 ++++++++++++++----- .../request/AppointmentCreationRequest.java | 4 +- .../feignClient/AppointmentApiService.java | 3 +- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java index 15a3444e..c82687a5 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/AppointmentApiConstant.java @@ -6,7 +6,7 @@ public class AppointmentApiConstant { public static final String GET_NDG_BY_VAT_NUMBER = "/WSAnagrafica.getListaNdg"; public static final String CREATE_VISURA = "/WSAnagrafica.createVisura"; public static final String GET_VISURA_LIST = "/WSAnagrafica.getVisuraList"; - public static final String GET_APPOINTMENT_TEMPLATE = "/WSCrmConsulenza.getAppuntamentoTemplate?idAppuntamentoTemplate=7"; + public static final String GET_APPOINTMENT_TEMPLATE = "/WSCrmConsulenza.getAppuntamentoTemplate"; public static final String CREATE_APPOINTMENT_FROM_TEMPLATE = "/WSCrmConsulenza.createAppointmentFromTemplate"; public static final String UPLOAD_APOOINTMENT_DOCUMENT = "/WSDocumentDetail.createStream"; @@ -24,4 +24,18 @@ public class AppointmentApiConstant { public static final boolean IS_FROM_RATING = Boolean.FALSE; public static final boolean IS_ANAGRAFICA_LEGAME = Boolean.FALSE; + //Appointment creation request body fields + public static final String MOTIVAZIONE = "motivazione"; + public static final String COD_PRODOTTO = "codProdotto"; + public static final String COD_ABI = "codAbi"; + public static final String COD_CAB = "codCab"; + public static final String ID_NOTA = "idNota"; + public static final String IMPORTO_AGEVOLATO = "importoAgevolato"; + public static final String IMPORTO_MEDIOLUNGO_TERMINE = "importoMedioLungoTermine"; + public static final String COD_TIPO_PRODOTTO = "codTipoProdotto"; + public static final String COD_CATEGORIA_PRODOTTO = "codCategoriaProdotto"; + public static final String COD_FORMATECNICA = "codFormaTecnica"; + public static final String COD_OPERAZIONE = "codOperazione"; + public static final String MOTIVAZIONE_ID = "id"; + } diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java index f696cdb0..5eb5128b 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java @@ -332,7 +332,7 @@ public class GepafinConstant { public static final String DATA_STRING = "data"; public static final String DOCUMENT_ATTACHMENT_ID_STRING = "documentAttachmentId"; public static final String TEMP_FILE_PATH = "/tmp/"; - public static final String RICHIESTA_CLIENTE_STRING = "richiestaCliente"; + public static final String RICHIESTE_CLIENTE_STRING = "richiesteCliente"; public static final String ID_STRING = "id"; public static final String NULL_STRING = "null"; public static final String NDG_STRING = "ndg"; diff --git a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java index a45b142d..8bf0b4a4 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java @@ -2,6 +2,7 @@ package net.gepafin.tendermanagement.dao; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GetObjectRequest; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import feign.FeignException; @@ -16,18 +17,14 @@ import net.gepafin.tendermanagement.entities.ApplicationEntity; import net.gepafin.tendermanagement.entities.CompanyEntity; import net.gepafin.tendermanagement.entities.DocumentEntity; import net.gepafin.tendermanagement.entities.HubEntity; -import net.gepafin.tendermanagement.entities.UserEntity; import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum; -import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum; import net.gepafin.tendermanagement.enums.NotificationTypeEnum; -import net.gepafin.tendermanagement.enums.RoleStatusEnum; import net.gepafin.tendermanagement.enums.VersionActionTypeEnum; import net.gepafin.tendermanagement.model.request.AppointmentCreationRequest; import net.gepafin.tendermanagement.model.request.AppointmentNdgRequest; import net.gepafin.tendermanagement.model.request.AppointmentVisuraListRequest; import net.gepafin.tendermanagement.model.request.AppointmentVisuraRequest; import net.gepafin.tendermanagement.model.request.CreateAppointmentRequest; -import net.gepafin.tendermanagement.model.request.NotificationReq; import net.gepafin.tendermanagement.model.request.UploadDocToExternalSystemRequest; import net.gepafin.tendermanagement.model.request.VersionHistoryRequest; import net.gepafin.tendermanagement.model.response.AppointmentCreationResponse; @@ -63,7 +60,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -388,7 +384,6 @@ 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 @@ -406,10 +401,15 @@ public class AppointmentDao { } // Handle non-OK response throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN)); + } catch (FeignException.Forbidden forbiddenException) { + logForbiddenError(); + // Regenerate the token and retry + regenerateTokenAndSave(hub); } catch (Exception e) { log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e); throw new RuntimeException("Authentication failed on Odessa. try again", e); } + return null; } private AppointmentLoginResponse retrieveNdgByVatNumber(String vatNumber, String authorizationToken, HubEntity hub, ApplicationEntity application) { @@ -626,7 +626,8 @@ public class AppointmentDao { // Generate authorization token and fetch template data String authorizationToken = getBearerToken(hub); - ResponseEntity response = appointmentApiService.getAppointmentTemplateForTemplateCreation(authorizationToken); + int areaCode = Integer.parseInt(hub.getAreaCode()); + ResponseEntity response = appointmentApiService.getAppointmentTemplateForTemplateCreation(authorizationToken, areaCode); if (response.getStatusCode() != HttpStatus.OK) { log.error("Failed to retrieve appointment template for appointment creation. Status: {}", response.getStatusCode()); @@ -682,33 +683,61 @@ public class AppointmentDao { } public AppointmentCreationRequest parseTemplateResponseData(String jsonResponse) { - try { ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonResponse); - JsonNode richiestaClienteArray = rootNode.path(GepafinConstant.DATA_STRING).path(GepafinConstant.RICHIESTA_CLIENTE_STRING); + JsonNode richiesteClienteArray = rootNode.path(GepafinConstant.DATA_STRING).path(GepafinConstant.RICHIESTE_CLIENTE_STRING); + // Initialize the result object AppointmentCreationRequest appointmentCreationRequest = new AppointmentCreationRequest(); AppointmentCreationRequest.Input input = new AppointmentCreationRequest.Input(); - - // Map `richiestaCliente` array List richiestaClienteList = new ArrayList<>(); - if (richiestaClienteArray.isArray()) { - for (JsonNode richiestaNode : richiestaClienteArray) { - richiestaClienteList.add(objectMapper.treeToValue(richiestaNode, AppointmentCreationRequest.RichiestaCliente.class)); - } + if (!richiesteClienteArray.isArray()) { + log.warn("richiesteCliente array is missing or not an array."); + return new AppointmentCreationRequest(); // Return empty object if array is missing + } + + for (JsonNode richiestaNode : richiesteClienteArray) { + 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)); + + richiestaClienteList.add(richiestaCliente); } input.setRichiestaCliente(richiestaClienteList); appointmentCreationRequest.setInput(input); + return appointmentCreationRequest; + } 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("Error parsing template response: {}", e.getMessage(), 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, AppointmentCreationRequest templateRichiestaData) { @@ -733,7 +762,8 @@ public class AppointmentDao { requestNota.setTitolo(nota.getTitolo()); requestNota.setTesto(nota.getTesto()); richiestaCliente.setNota(requestNota); - + richiestaCliente.setDurataMesiFinanziamento(createAppointmentRequest.getDurataMesiFinanziamento()); + richiestaCliente.setImportoBreveTermine(createAppointmentRequest.getImportoBreveTermine()); richiestaClienteList.add(richiestaCliente); } 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 a877a620..0bd6b812 100644 --- a/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java +++ b/src/main/java/net/gepafin/tendermanagement/model/request/AppointmentCreationRequest.java @@ -20,8 +20,8 @@ public class AppointmentCreationRequest { public static class RichiestaCliente { private String codAbi; private String codCab; - private Integer durataMesiFinanziamento; - private Integer idMotivazione; + private int durataMesiFinanziamento; + private int idMotivazione; private String idNota; private String importoAgevolato; private Double importoBreveTermine; 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 b193713b..1c3a44f7 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java +++ b/src/main/java/net/gepafin/tendermanagement/service/feignClient/AppointmentApiService.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; @@ -31,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); + ResponseEntity getAppointmentTemplateForTemplateCreation(@RequestHeader("Authorization") String token, @RequestParam("idAppuntamentoTemplate") int 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);