Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) O3-3982: Validate against starting a visit at a future time #2013

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

njiddasalifu
Copy link
Contributor

@njiddasalifu njiddasalifu commented Sep 17, 2024

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 conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR adds validation logic that ensures a visit cannot be started at a time in the future. It does so by adding a refinement to the visitFormSchema zod schema that compares the value of the visitStartTime field with the current timestamp. If the visitStartTime is in the future, a validation error message will be shown under the field, and the form will not be submitted. The error message shown reads "Visit start time cannot be in the future".

Screenshots

visit-start-time.mp4

Related Issue

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

Other

@njiddasalifu njiddasalifu marked this pull request as draft September 17, 2024 12:01
@njiddasalifu
Copy link
Contributor Author

Hi @denniskigen here is the PR. we can follow from

@denniskigen denniskigen self-requested a review September 17, 2024 12:03
@denniskigen denniskigen changed the title (fix): O3-3982 Prevent future time for start visits (fix) O3-3982: Validate against starting a visit at a future time Sep 24, 2024
@@ -464,70 +445,66 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
.pipe(first())
.subscribe({
next: (response) => {
if (response.status === 201) {
Copy link
Member

Choose a reason for hiding this comment

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

openmrsFetch handles checking the response headers for us, so explicitly checking the status as we're doing here isn't necessary.

Copy link
Contributor Author

@njiddasalifu njiddasalifu left a comment

Choose a reason for hiding this comment

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

LGTM!
thanks for comments and addons @denniskigen

@njiddasalifu njiddasalifu marked this pull request as ready for review September 25, 2024 13:32
@denniskigen denniskigen marked this pull request as draft September 25, 2024 20:38
@@ -121,59 +121,120 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
});

const displayVisitStopDateTimeFields = useMemo(
() => visitToEdit?.stopDatetime || showVisitEndDateTimeFields,
() => Boolean(visitToEdit?.stopDatetime) || showVisitEndDateTimeFields,
Copy link
Member

@denniskigen denniskigen Sep 25, 2024

Choose a reason for hiding this comment

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

This ensures displayVisitStopDateTimeFields always returns a boolean.

onChange={([date]) => onChange(date)}
value={value ? dayjs(value).format('DD/MM/YYYY') : null}
>
<DatePickerInput
id={`${dateFieldName}Input`}
invalid={Boolean(errors[dateFieldName])}
Copy link
Member

Choose a reason for hiding this comment

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

The invalid prop expects a boolean.

@@ -78,26 +78,24 @@ const VisitDateTimeField: React.FC<VisitDateTimeFieldProps> = ({
render={({ field: { onBlur, onChange, value } }) => (
<TimePicker
id={timeFieldName}
invalid={Boolean(errors[timeFieldName])}
Copy link
Member

Choose a reason for hiding this comment

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

Likewise here.

@@ -121,58 +121,73 @@ const StartVisitForm: React.FC<StartVisitFormProps> = ({
});

const displayVisitStopDateTimeFields = useMemo(
() => visitToEdit?.stopDatetime || showVisitEndDateTimeFields,
() => Boolean(visitToEdit?.stopDatetime || showVisitEndDateTimeFields),
Copy link
Member

Choose a reason for hiding this comment

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

We want to ensure that this variable is always a boolean.

Comment on lines +187 to +190
.refine((data) => validateStartTime(data), {
message: t('futureStartTime', 'Visit start time cannot be in the future'),
path: ['visitStartTime'],
});
Copy link
Member

Choose a reason for hiding this comment

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

This is the key change. It runs whatever value gets filled into the Visit start time input against validateStartTime. If the start time is in the future, the validation error message above is rendered.

Comment on lines +234 to +235
// TODO: Figure out why this test is failing
xit('displays an error message when the visit start time is in the future', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

I don't know why this test is failing. It mimics the user interaction of filling in the field with a value that ought to trigger the validation (similar to the test just above).

@denniskigen denniskigen marked this pull request as ready for review September 27, 2024 08:00
This PR adds validation logic that ensures a visit cannot be started at a time in the future. It does so by adding a refinement to the visitFormSchema zod schema that compares the value of the visitStartTime field with the current timestamp. If the visitStartTime is in the future, a validation error message will be shown under the field, and the form will not be submitted. The error message shown reads "Visit start time cannot be in the future".

Additionally, this PR refactors the zod schema, centralizing some of the validation logic into reusable helper functions that are hopefully more easier to maintain. It also adds some test coverage for the new validation logic.
@denniskigen
Copy link
Member

Thanks for the review, @vasharma05!

@denniskigen denniskigen merged commit fd714ea into openmrs:main Oct 2, 2024
6 checks passed
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.

3 participants