Skip to content

Conversation

@RajPrakash681
Copy link
Contributor

@RajPrakash681 RajPrakash681 commented Dec 9, 2025

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work is based on designs, which are linked or shown either in the Jira ticket or the description below. (See also: Styleguide)
  • My work includes tests or is validated by existing tests.

Summary

This PR adds client-side validation to prevent users from creating visits that overlap with existing visits for the same patient. Previously, overlapping visits could only be caught by the server, resulting in 400 errors. Now users receive immediate feedback when attempting to create or edit a visit that conflicts with an existing one.

Key changes:

  • Implemented overlap detection algorithm that checks all existing patient visits
  • Added async validation using custom resolver pattern (similar to visit-notes-form)
  • Handles edge cases: ongoing visits, edit mode, and adjacent non-overlapping visits
  • Added comprehensive test coverage (11 new tests)

Screenshots

N/A - This is a validation enhancement with no UI changes. Error messages are displayed inline on existing form fields.

Related Issue

https://openmrs.atlassian.net/browse/O3-4447

Other

@RajPrakash681 RajPrakash681 changed the title (feat)O3-4447: Add client-side validation for overlapping visits (feat)O3-4447: Validate visit start/end time against overlapping visits Dec 9, 2025
Copy link
Contributor

@chibongho chibongho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulling all visits from the patient to check for overlaps on the frontend is inefficient. We should query the backend for visits within the overlapping range instead. I'm not sure if the backend already supports that; if not, we should implement it there.

@RajPrakash681
Copy link
Contributor Author

RajPrakash681 commented Dec 9, 2025

Thanks for the feedback @chibingho!

I looked into the backend API and found that it partially supports date filtering:

The /visit endpoint has a fromStartDate parameter to filter visits starting after a certain date
But there's no toStopDate or overlap-specific filtering

So the backend doesn’t fully support querying visits within an overlapping range.

What would you prefer I do?

Option 1:
I can optimize the current frontend code to use the fromStartDate parameter to reduce the data fetched
Option 2:
I can work on adding a proper backend endpoint for overlap checking

@chibongho
Copy link
Contributor

The /visit endpoint has a fromStartDate parameter to filter visits starting after a certain date But there's no toStopDate or overlap-specific filtering

Where did you see that?

@RajPrakash681
Copy link
Contributor Author

The /visit endpoint has a fromStartDate parameter to filter visits starting after a certain date But there's no toStopDate or overlap-specific filtering

Where did you see that?

image

fromStartDate is documented, but there's no toStopDate, toStartDate, or any overlap-specific parameters available

@chibongho
Copy link
Contributor

chibongho commented Dec 9, 2025

Ahh, sorry, that page is horribly out of date. I usually need to go directly to the Java code to see what's supported:

https://github.com/openmrs/openmrs-module-webservices.rest/blob/92d4f8faf3a095f0dc59e30e4cb1f3ed86cb0345/omod/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java#L323

So fromStartDate, toStartDate, fromStopDate, toStopDate are all available. I think what we want is:

/visit?patient=<patientUuid>&toStartDate=<newVisitStopDate)&fromStopDate=<newVisitStartDate>

@RajPrakash681
Copy link
Contributor Author

Ahh, sorry, that page is horribly out of date. I usually need to go directly to the Java code to see what's supported:

https://github.com/openmrs/openmrs-module-webservices.rest/blob/92d4f8faf3a095f0dc59e30e4cb1f3ed86cb0345/omod/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java#L323

So fromStartDate, toStartDate, fromStopDate, toStopDate are all available. I think what we want is:

/visit?patient=<patientUuid>&toStartDate=<newVisitStopDate)&fromStopDate=<newVisitStartDate>

ok got it!

@RajPrakash681 RajPrakash681 force-pushed the O3-4447-visit-overlap-validation branch from 04dcf55 to 90cfed5 Compare December 9, 2025 19:28
- Query backend with toStartDate and fromStopDate parameters
- Add async validation with custom Zod resolver
- Show error if overlapping visits exist
- Prevent form submission until overlap resolved
@RajPrakash681 RajPrakash681 force-pushed the O3-4447-visit-overlap-validation branch from 90cfed5 to c79e0ba Compare December 9, 2025 19:47
@RajPrakash681
Copy link
Contributor Author

@chibongho hello, can you check now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants