diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java index 734c558bd..04d967525 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java @@ -3089,4 +3089,39 @@ public Object modifyNUPI(HttpServletRequest request, @PathVariable String nupinu return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON).body(ret); } + + /** + * Returns the latest patient Obs value coded concept ID and UUID given the patient UUID and Concept UUID + * @param patientUuid + * @return value coded concept id and uuid + */ + @CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.OPTIONS}) + @RequestMapping(method = RequestMethod.GET, value = "/latestobs") + @ResponseBody + public Object getLatestObs(@RequestParam("patientUuid") String patientUuid, @RequestParam("concept") String conceptIdentifier) { + SimpleObject ret = SimpleObject.create("conceptId", 0, "conceptUuid", ""); + if (StringUtils.isBlank(patientUuid)) { + return new ResponseEntity("You must specify a patientUuid in the request!", + new HttpHeaders(), HttpStatus.BAD_REQUEST); + } + + if (StringUtils.isBlank(conceptIdentifier)) { + return new ResponseEntity("You must specify a concept in the request!", + new HttpHeaders(), HttpStatus.BAD_REQUEST); + } + + Patient patient = Context.getPatientService().getPatientByUuid(patientUuid); + + Concept concept = Dictionary.getConcept(conceptIdentifier); + List obsList = Context.getObsService().getObservationsByPersonAndConcept(patient, concept); + if (obsList.size() > 0) { + // these are in reverse chronological order + Obs currentObs = obsList.get(0); + ret.put("conceptId", currentObs.getValueCoded().getId()); + ret.put("conceptUuid", currentObs.getValueCoded().getUuid()); + } + + return ret; + + } }