Sunday, February 13, 2022

Get Map of Case Status | RestTemplate

@PostMapping("/internal/api/v1/case-status")
public ResponseEntity<Response<?>> getCaseStatus(@RequestBody CaseStatusRequest request) {
return ResponseEntity.ok(new Response<Map<Long, String>>(new Date(), HttpStatus.OK.value(),
"Case status fetched successfully.", customerService.getCaseStatus(request)));
}
public Map<Long, String> getCaseStatus(CaseStatusRequest request) {
log.info("Inside getCaseStatus {}", getObjectAsString(request));
List<CustomerDetail> byUserIdIn = customerDetailsRepository.findByUserIdIn(request.getUserIds());
if (byUserIdIn.isEmpty()) {
return new HashMap<>();
}
return byUserIdIn.stream().collect(Collectors.toMap(CustomerDetail::getUserId, e -> e.getAgentName() == null ? "Pick" : e.getAgentName()));
}
List<Long> userIds = invoiceDtoList.stream().map(InvoiceDto::getConsumerId).collect(Collectors.toList());
Map<Long, String> map = consumerService.getCaseStatus(userIds);
invoiceDtoList.forEach(e -> e.setAgentName(map.get(e.getConsumerId())));
public Map<Long, String> getCaseStatus(List<Long> userIds) {
int status;
String url = URLConstants.GET_ALL_ORDER_CASE_STATUS;
String finalUrl = customerRestBaseUrl + url;
log.info("finalUrl " + finalUrl);
CaseStatusRequest request = new CaseStatusRequest();
request.setUserIds(userIds);

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = getHttpHeaders();
HttpEntity<CaseStatusRequest> requestEntity = new HttpEntity<>(request, headers);

try {
ResponseEntity<JsonNode> responseEntity = restTemplate.exchange(finalUrl, HttpMethod.POST, requestEntity, JsonNode.class);
JsonNode response = responseEntity.getBody();
status = response.get(DevIdeaConstant.STATUS_CODE).intValue();
String errorMsg = response.get(DevIdeaConstant.MESSAGE).asText();
log.error("Error msg " + errorMsg);

if (!ObjectUtils.isEmpty(response.get(DevIdeaConstant.DATA))) {
String data = response.get(DevIdeaConstant.DATA).toString();
return OBJECT_MAPPER.readValue(data, new TypeReference<Map<Long, String>>() {
});
}
} catch (HttpClientErrorException ex) {
log.error("error while getting case status from customer service {}", ex.getMessage());
} catch (Exception ex) {
log.error("error while getting case status from customer service {}", ex.getMessage());
}
return null;
}





 

No comments:

Post a Comment