From 327c11bf4f1dfc4782d96162ce18ee34fc78799d Mon Sep 17 00:00:00 2001 From: mogoodrich Date: Mon, 18 May 2026 16:10:50 -0400 Subject: [PATCH] RA-2108: CoreApps: Have way to prevent adding a past visit at a different visit location --- .../RetrospectiveVisitFragmentController.java | 58 +++++++++++--- omod/src/main/resources/config.xml | 10 +++ ...rospectiveVisitFragmentControllerTest.java | 80 +++++++++++++++++-- 3 files changed, 131 insertions(+), 17 deletions(-) diff --git a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java index 94197c5a8..1cc38aff6 100644 --- a/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java +++ b/omod/src/main/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentController.java @@ -3,6 +3,7 @@ import org.joda.time.DateTime; import org.openmrs.Location; import org.openmrs.Patient; +import org.openmrs.api.AdministrationService; import org.openmrs.module.appui.AppUiConstants; import org.openmrs.module.emrapi.adt.AdtService; import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException; @@ -22,9 +23,13 @@ public class RetrospectiveVisitFragmentController { + static final String ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP = + "coreapps.addPastVisitsAllowOverlappingVisitAtAnotherLocation"; + private final Logger log = LoggerFactory.getLogger(getClass()); public Object create(@SpringBean("adtService") AdtService adtService, + @SpringBean("adminService") AdministrationService administrationService, @RequestParam("patientId") Patient patient, @RequestParam("locationId") Location location, @RequestParam("startDate") Date startDate, @@ -48,7 +53,15 @@ public Object create(@SpringBean("adtService") AdtService adtService, } } + String propValue = administrationService.getGlobalProperty(ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP); + boolean allowOverlappingAtAnotherLocation = !"false".equalsIgnoreCase(propValue); + if (!allowOverlappingAtAnotherLocation) { + List conflictingVisits = getConflictsAcrossAllLocations(adtService, patient, startDate, stopDate, ui); + if (!conflictingVisits.isEmpty()) { + return conflictingVisits; + } + } try { VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate); @@ -62,24 +75,16 @@ public Object create(@SpringBean("adtService") AdtService adtService, catch (ExistingVisitDuringTimePeriodException e) { // if there are existing visit(s), return these existing visits - List simpleVisits = new ArrayList(); + List conflictingVisits = new ArrayList(); List visits = adtService.getVisits(patient, location, startDate, stopDate); if (visits != null) { for (VisitDomainWrapper visit : visits) { - if(ui.convertTimezones()){ - simpleVisits.add(SimpleObject.create("startDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStartDatetime()).toDate()), - "stopDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStopDatetime()).toDate()), - "id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid())); - }else{ - simpleVisits.add(SimpleObject.create("startDate", ui.format(new DateTime(visit.getVisit().getStartDatetime()).toDateMidnight().toDate()), - "stopDate", ui.format(new DateTime(visit.getVisit().getStopDatetime()).toDateMidnight().toDate()), - "id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid())); - } + conflictingVisits.add(formatVisit(visit, ui)); } } - return simpleVisits; + return conflictingVisits; } catch (Exception e) { log.error("Unable to add retrospective visit", e); @@ -87,4 +92,35 @@ public Object create(@SpringBean("adtService") AdtService adtService, } } + private List getConflictsAcrossAllLocations(AdtService adtService, Patient patient, + Date startDate, Date stopDate, UiUtils ui) { + List conflictingVisits = new ArrayList(); + List allLocations = adtService.getAllLocationsThatSupportVisits(); + if (allLocations != null) { + for (Location loc : allLocations) { + List visits = adtService.getVisits(patient, loc, startDate, stopDate); + if (visits != null) { + for (VisitDomainWrapper visit : visits) { + conflictingVisits.add(formatVisit(visit, ui)); + } + } + } + } + return conflictingVisits; + } + + private SimpleObject formatVisit(VisitDomainWrapper visit, UiUtils ui) { + if (ui.convertTimezones()) { + return SimpleObject.create( + "startDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStartDatetime()).toDate()), + "stopDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStopDatetime()).toDate()), + "id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid()); + } else { + return SimpleObject.create( + "startDate", ui.format(new DateTime(visit.getVisit().getStartDatetime()).toDateMidnight().toDate()), + "stopDate", ui.format(new DateTime(visit.getVisit().getStopDatetime()).toDateMidnight().toDate()), + "id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid()); + } + } + } diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index e9fa291cc..c1c08df0e 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -170,6 +170,16 @@ + + coreapps.addPastVisitsAllowOverlappingVisitAtAnotherLocation + true + + When adding a past (retrospective) visit, controls whether a visit is allowed if the patient already has + a visit at a different location during that time period. When set to false, the patient must have no visits + at any location during the requested time period. + + + coreapps.defaultPatientIdentifierLocation diff --git a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java index be869217a..9873d0d9c 100644 --- a/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/coreapps/fragment/controller/visit/RetrospectiveVisitFragmentControllerTest.java @@ -7,6 +7,7 @@ import org.openmrs.Location; import org.openmrs.Patient; import org.openmrs.Visit; +import org.openmrs.api.AdministrationService; import org.openmrs.module.appui.AppUiConstants; import org.openmrs.module.emrapi.adt.AdtService; import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException; @@ -16,6 +17,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -38,6 +40,8 @@ public class RetrospectiveVisitFragmentControllerTest { AdtService adtService; + AdministrationService administrationService; + UiUtils ui; HttpServletRequest request; @@ -48,11 +52,15 @@ public class RetrospectiveVisitFragmentControllerTest { public void setup() { controller = new RetrospectiveVisitFragmentController(); adtService = mock(AdtService.class); + administrationService = mock(AdministrationService.class); ui = mock(UiUtils.class); request = mock(HttpServletRequest.class); session = mock(HttpSession.class); when(request.getSession()).thenReturn(session); + // default: property not set, so overlapping at another location is allowed + when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP)) + .thenReturn(null); } @Test @@ -74,7 +82,7 @@ public void test_shouldCreateNewRetrospectiveVisit() throws Exception { when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit)); - SimpleObject result = (SimpleObject) controller.create(adtService, patient, location, startDate, stopDate, request, ui); + SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, stopDate, request, ui); verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.retrospectiveVisit.addedVisitMessage")); @@ -102,7 +110,7 @@ public void test_shouldCreateNewRetrospectiveVisit_whenNoStopDateSpecified() thr when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit)); - SimpleObject result = (SimpleObject) controller.create(adtService, patient, location, startDate, null, request, ui); + SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, null, request, ui); verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.retrospectiveVisit.addedVisitMessage")); @@ -136,7 +144,7 @@ public void test_shouldReturnConflictingVisits() throws Exception { when(ui.format(any())).thenReturn("someDate"); - List result = (List) controller.create(adtService, patient, location, startDate, null, request, ui); + List result = (List) controller.create(adtService, administrationService, patient, location, startDate, null, request, ui); assertThat(result.size(), is(1)); assertThat(result.get(0).toJson(), is("{\"startDate\":\"someDate\",\"stopDate\":\"someDate\",\"id\":null,\"uuid\":\"" + conflictingVisit.getUuid() + "\"}")); @@ -159,7 +167,7 @@ public void test_shouldSetEndDateToCurrentTimeIfEndDateIsCurrentDay() throws Exc when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message Date expectedMinDateValue = new Date(); - controller.create(adtService, patient, location, startDate, endDate, request, ui); + controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui); Date expectedMaxDateValue = new Date(); verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), argThat(new IsWithinDateRange(expectedMinDateValue, expectedMaxDateValue))); @@ -184,7 +192,7 @@ public void test_shouldCreateNewRetrospectiveVisitWithTimezoneAndDontChangeEndDa when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message - controller.create(adtService, patient, location, startDate, endDate, request, ui); + controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui); verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), eq(endDate)); @@ -207,12 +215,72 @@ public void test_shouldNotSetEndDateToCurrentTimeIfEndDateIsCurrentDay() throws when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message - controller.create(adtService, patient, location, startDate, endDate, request, ui); + controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui); verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), eq(endDate)); } + @Test + public void test_shouldReturnConflictingVisitsAtAnyLocationWhenPropertySetToFalse() throws Exception { + + when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP)) + .thenReturn("false"); + + Patient patient = new Patient(); + Location requestedLocation = new Location(); + Location otherLocation = new Location(); + Date startDate = new DateTime(2012, 1, 1, 12, 12, 12).toDate(); + + Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate(); + Date expectedStopDate = null; + + Visit conflictingVisit = new Visit(); + conflictingVisit.setStartDatetime(new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate()); + conflictingVisit.setStopDatetime(new DateTime(2012, 1, 3, 0, 0, 0, 999).toDate()); + + when(adtService.getAllLocationsThatSupportVisits()).thenReturn(Arrays.asList(requestedLocation, otherLocation)); + when(adtService.getVisits(patient, requestedLocation, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList()); + when(adtService.getVisits(patient, otherLocation, expectedStartDate, expectedStopDate)) + .thenReturn(Collections.singletonList(new VisitDomainWrapper(conflictingVisit))); + + when(ui.format(any())).thenReturn("someDate"); + + List result = (List) controller.create(adtService, administrationService, patient, requestedLocation, startDate, null, request, ui); + + assertThat(result.size(), is(1)); + assertThat(result.get(0).toJson(), is("{\"startDate\":\"someDate\",\"stopDate\":\"someDate\",\"id\":null,\"uuid\":\"" + conflictingVisit.getUuid() + "\"}")); + } + + @Test + public void test_shouldCreateVisitWhenPropertySetToFalseAndNoConflictAtAnyLocation() throws Exception { + + when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP)) + .thenReturn("false"); + when(ui.message("coreapps.retrospectiveVisit.addedVisitMessage")).thenReturn("success message"); + + Patient patient = createPatient(); + Location location = new Location(); + Location otherLocation = new Location(); + Date startDate = new DateTime(2012, 1, 1, 12, 12, 12).toDate(); + Date stopDate = new DateTime(2012, 1, 2, 13, 13, 13).toDate(); + + Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate(); + Date expectedStopDate = new DateTime(2012, 1, 2, 23, 59, 59, 999).toDate(); + + Visit visit = createVisit(); + + when(adtService.getAllLocationsThatSupportVisits()).thenReturn(Arrays.asList(location, otherLocation)); + when(adtService.getVisits(patient, location, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList()); + when(adtService.getVisits(patient, otherLocation, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList()); + when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit)); + + SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, stopDate, request, ui); + + assertTrue((Boolean) result.get("success")); + assertThat((String) result.get("id"), is(visit.getId().toString())); + } + private Visit createVisit() { Visit visit = new Visit(); visit.setId(1);