Skip to content

Commit

Permalink
TRUNK-6278: Skip visit overlap validation when ending a visit (#4835)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasanka-sack authored Nov 14, 2024
1 parent 2f906f0 commit ddba2b4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
9 changes: 8 additions & 1 deletion api/src/main/java/org/openmrs/validator/VisitValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ public void validate(Object target, Errors errors) {
// Skipping validation if the patient is not set or not yet persisted
boolean nonExistingPatient = visit.getPatient() == null || visit.getPatient().getId() == null;

Visit existingVisit = Context.getVisitService().getVisitByUuid(visit.getUuid());

boolean endingVisit = existingVisit != null &&
existingVisit.getStopDatetime() == null &&
visit.getStopDatetime() != null
&& existingVisit.getStartDatetime().equals(visit.getStartDatetime());

// check for overlapping visits
if (!nonExistingPatient && disallowOverlappingVisits()) {
if (!nonExistingPatient && disallowOverlappingVisits() && !endingVisit) {
VisitSearchCriteria visitSearchCriteria = new VisitSearchCriteriaBuilder()
.patient(visit.getPatient())
.maxStartDatetime(visit.getStopDatetime())
Expand Down
62 changes: 62 additions & 0 deletions api/src/test/java/org/openmrs/validator/VisitValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,68 @@ public void validate_shouldRejectOverlappingActiveVisits() {
assertTrue(errors.hasFieldErrors("startDatetime"));
}

/**
* @see VisitValidator#validate(Object, org.springframework.validation.Errors)
* Should skip validation if it is an end visit call and start date is not changed
*/
@Test
public void validate_shouldNotRejectOnVisitOverlapDuringEndVisit() {

// active visit: |---------------->
// overlapping visit: |--|
// ended active visit: |----------|

String activeVisitUuid = "c2639863-cbbe-44bb-986d-8a4820f8ae14";
Visit activeVisit = Context.getVisitService().getVisitByUuid(activeVisitUuid);

String newStopDateTime = "2014-02-06T00:00:00";

// make a clone and update the visit with an end date
Visit updatedVisit = new Visit();
updatedVisit.setVisitId(activeVisit.getVisitId());
updatedVisit.setUuid(activeVisit.getUuid());
updatedVisit.setPatient(activeVisit.getPatient());
updatedVisit.setStartDatetime(activeVisit.getStartDatetime());
updatedVisit.setVisitType(activeVisit.getVisitType());
updatedVisit.setStopDatetime(parseIsoDate(newStopDateTime));

Errors errors = new BindException(updatedVisit, "visit");
new VisitValidator().validate(updatedVisit, errors);

assertFalse(errors.hasFieldErrors("startDatetime"));
}

/**
* @see VisitValidator#validate(Object, org.springframework.validation.Errors)
* Should skip validation if it is an end visit call and start date is not changed
*/
@Test
public void validate_shouldRejectOnVisitOverlapDuringEndVisitWithStartDateUpdated() {
// active visit: |---------------->
// overlapping visit: |--|
// ended active visit: |----------|

String activeVisitUuid = "c2639863-cbbe-44bb-986d-8a4820f8ae14";
Visit activeVisit = Context.getVisitService().getVisitByUuid(activeVisitUuid);

String newStartDateTime = "2014-02-04T00:00:00";
String newStopDateTime = "2014-02-06T00:00:00";

// make a clone and update the visit with an end date
Visit updatedVisit = new Visit();
updatedVisit.setVisitId(activeVisit.getVisitId());
updatedVisit.setUuid(activeVisit.getUuid());
updatedVisit.setPatient(activeVisit.getPatient());
updatedVisit.setVisitType(activeVisit.getVisitType());
updatedVisit.setStartDatetime(parseIsoDate(newStartDateTime));
updatedVisit.setStopDatetime(parseIsoDate(newStopDateTime));

Errors errors = new BindException(updatedVisit, "visit");
new VisitValidator().validate(updatedVisit, errors);

assertTrue(errors.hasFieldErrors("startDatetime"));
}

/**
* @see VisitValidator#validate(Object, org.springframework.validation.Errors)
* This test verifies that updating an existing visit does not result in a rejection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
<visit visit_id="10" patient_id="42" visit_type_id="1" date_started="2014-01-04 10:00:00.0" date_stopped="2014-01-04 14:00:00.0" location_id="1" indication_concept_id="5497" creator="1" date_created="2014-01-04 10:00:00.0" voided="false" uuid="7EC61082-9C62-4BBB-9F1B-F4BEA9C2893E"/>
<visit visit_id="11" patient_id="42" visit_type_id="1" date_started="2014-02-05 00:00:00.0" date_stopped="2014-02-11 00:00:00.0" location_id="1" indication_concept_id="5497" creator="1" date_created="2014-02-05 00:00:00.0" voided="true" uuid="3CBB36EA-654C-4C26-9B01-C2D2A1B0426D"/>
<visit visit_id="12" patient_id="43" visit_type_id="1" date_started="2014-01-04 10:00:00.0" location_id="1" indication_concept_id="5497" creator="1" date_created="2014-01-04 10:00:00.0" voided="false" uuid="47841268-0f09-4af9-9033-d547c6adec54"/>

<visit visit_id="13" patient_id="43" visit_type_id="1" date_started="2014-02-05 00:00:00.0" date_stopped="2014-02-11 00:00:00.0" location_id="1" indication_concept_id="5497" creator="1" date_created="2014-02-05 00:00:00.0" voided="false" uuid="05b7edf2-035b-4256-837e-8c9712612fde"/>
<visit visit_id="14" patient_id="43" visit_type_id="1" date_started="2014-02-02 00:00:00.0" location_id="1" indication_concept_id="5497" creator="1" date_created="2014-02-05 00:00:00.0" voided="false" uuid="c2639863-cbbe-44bb-986d-8a4820f8ae14"/>
</dataset>

0 comments on commit ddba2b4

Please sign in to comment.