From 1d8a3ff0d6d3eb7cb329673afb6ddbddce5450c9 Mon Sep 17 00:00:00 2001 From: riyaTw <126869458+riyaTw@users.noreply.github.com> Date: Mon, 18 Dec 2023 19:43:08 +0530 Subject: [PATCH] Add. Accession number for lab test performed in OpenElis to orders table (#238) * BAH-3416 | Add. Accession number for lab test performed in OpenElis to orders table | [Riya/Sweety] * BAH-3416 | Refactor. Optimised code | [Riya/Sweety] * BAH-3416 | Refactor. Optimised code | [Riya/Sweety] --- .../worker/OpenElisAccessionEventWorker.java | 17 ++++++++ .../OpenElisAccessionEventWorkerTest.java | 43 +++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/openmrs-elis-atomfeed-client-omod/src/main/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorker.java b/openmrs-elis-atomfeed-client-omod/src/main/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorker.java index 74d1c75c8b..e1e5d15066 100644 --- a/openmrs-elis-atomfeed-client-omod/src/main/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorker.java +++ b/openmrs-elis-atomfeed-client-omod/src/main/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorker.java @@ -250,6 +250,23 @@ private Obs createObsWith(String textValue, Concept concept, Date obsDateTime) { protected Set associateTestResultsToOrder(OpenElisAccession openElisAccession) throws ParseException { Encounter orderEncounter = encounterService.getEncounterByUuid(openElisAccession.getAccessionUuid()); + + if (!orderEncounter.getOrders().isEmpty()) { + for (OpenElisTestDetail testDetail : openElisAccession.getTestDetails()) { + if (testDetail == null || testDetail.getStatus() == null) { + continue; + } + for (Order order : orderEncounter.getOrders()) { + if (testDetail.getTestUuid().equals(order.getConcept().getUuid())) { + if ("Canceled".equals(testDetail.getStatus())) { + continue; + } + order.setAccessionNumber(openElisAccession.getAccessionUuid()); + } + } + } + } + final EncounterType labResultEncounterType = getLabResultEncounterType(); final Set allTests = openElisAccession.getTestDetails(); diff --git a/openmrs-elis-atomfeed-client-omod/src/test/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorkerTest.java b/openmrs-elis-atomfeed-client-omod/src/test/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorkerTest.java index c87d993d9e..5192bd818d 100644 --- a/openmrs-elis-atomfeed-client-omod/src/test/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorkerTest.java +++ b/openmrs-elis-atomfeed-client-omod/src/test/java/org/bahmni/module/elisatomfeedclient/api/worker/OpenElisAccessionEventWorkerTest.java @@ -1,7 +1,5 @@ package org.bahmni.module.elisatomfeedclient.api.worker; -import java.util.HashMap; -import java.util.Map; import org.bahmni.module.elisatomfeedclient.api.Constants; import org.bahmni.module.elisatomfeedclient.api.ElisAtomFeedProperties; import org.bahmni.module.elisatomfeedclient.api.builder.OpenElisAccessionBuilder; @@ -35,8 +33,13 @@ import java.util.Arrays; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; - +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.mockito.Matchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -143,6 +146,38 @@ public void shouldUpdateEncounterWhenAccessionHasNewOrder() throws Exception { verify(bahmniVisitAttributeSaveCommand).save(previousEncounter); } + @Test + public void shouldUpdateEncounterWithAccessionNumberWhenItsGenerated() throws Exception { + Encounter orderEncounter = getEncounterWithTests("test1", "test2", "test3"); + final Visit visit = new Visit(); + visit.setId(1); + orderEncounter.setVisit(visit); + visit.setEncounters(new HashSet<>(Arrays.asList(orderEncounter))); + OpenElisTestDetail test1 = new OpenElisTestDetailBuilder().withTestUuid("test1").build(); + OpenElisTestDetail test2 = new OpenElisTestDetailBuilder().withTestUuid("test2").build(); + OpenElisTestDetail test3 = new OpenElisTestDetailBuilder().withTestUuid("test3").build(); + test1.setStatus("Not started"); + test2.setStatus("Canceled"); + test3.setStatus("Not started"); + OpenElisAccession openElisAccession = new OpenElisAccessionBuilder().withTestDetails(new HashSet<>(Arrays.asList(test1, test2, test3))).build(); + + stubAccession(openElisAccession); + when(encounterService.getEncounterByUuid(openElisAccession.getAccessionUuid())).thenReturn(orderEncounter); + when(accessionMapper.findOrInitializeVisit(any(Patient.class), any(Date.class), any(String.class))).thenReturn(visit); + when(encounterService.saveEncounter(orderEncounter)).thenReturn(orderEncounter); + accessionEventWorker.associateTestResultsToOrder(openElisAccession); + + List orderAccessionNumbers = orderEncounter.getOrders().stream() + .map(Order::getAccessionNumber) + .collect(Collectors.toList()); + + verify(encounterService, times(1)).getEncounterByUuid(openElisAccession.getAccessionUuid()); + + assertEquals("1234", orderAccessionNumbers.get(0)); + assertNotEquals("1234", orderAccessionNumbers.get(1)); + assertEquals("1234", orderAccessionNumbers.get(2)); + } + @Test public void shouldUpdateEncounterWhenAccessionHasRemovedOrderFromPreviousEncounter() throws Exception { Encounter previousEncounter = getEncounterWithTests("test1", "test2", "test3"); @@ -228,8 +263,10 @@ private Encounter getEncounterWithTests(String... testUuids) { for (String testUuid : testUuids) { Order order = new Order(); Concept concept = new Concept(); + concept.setId(123); concept.setUuid(testUuid); order.setConcept(concept); + order.setAccessionNumber(null); encounter.addOrder(order); encounter.setEncounterType(new EncounterType()); Patient patient = new Patient();