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

O3-2396: Appointments: Refine logic for whether a patient is checked … #1111

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button } from '@carbon/react';
import { TaskComplete } from '@carbon/react/icons';
import { useTranslation } from 'react-i18next';
import { navigate, showModal, useConfig } from '@openmrs/esm-framework';
import { type Appointment } from '../../types';
import { type Appointment, AppointmentStatus } from '../../types';
import { type ConfigObject } from '../../config-schema';
import { useTodaysVisits } from '../../hooks/useTodaysVisits';
import CheckInButton from './checkin-button.component';
Expand All @@ -22,13 +22,9 @@ interface AppointmentsActionsProps {
const AppointmentsActions: React.FC<AppointmentsActionsProps> = ({ appointment }) => {
const { t } = useTranslation();
const { checkInButton, checkOutButton } = useConfig<ConfigObject>();
const { visits, mutateVisit } = useTodaysVisits();
const { mutateVisit } = useTodaysVisits();
const patientUuid = appointment.patient.uuid;
const visitDate = dayjs(appointment.startDateTime);
const hasActiveVisitToday = visits?.some((visit) => visit?.patient?.uuid === patientUuid && visit?.startDatetime);
const hasCheckedOutToday = visits?.some(
(visit) => visit?.patient?.uuid === patientUuid && visit?.startDatetime && visit?.stopDatetime,
);
Copy link
Member

Choose a reason for hiding this comment

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

As we discovered when looking at this today, this change can't be made in isolation of the changes to the checkout functionality, which currently will end the active visit. Always ending the active visit makes sense when your checkout logic is based on the presence of an active visit. But once you change the check-out logic to be independent of the visit status, you'll need to check that the visit is not already ended and/or prompt if the user wants to also close the visit, when you issue the modal dialog and perform the operation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I likely want to tweak this.

const isTodaysAppointment = visitDate.isToday();

const handleCheckout = () => {
Expand All @@ -47,19 +43,19 @@ const AppointmentsActions: React.FC<AppointmentsActionsProps> = ({ appointment }

const renderVisitStatus = () => {
switch (true) {
case hasCheckedOutToday && isTodaysAppointment:
case isTodaysAppointment && appointment.status === AppointmentStatus.COMPLETED:
return (
<Button kind="ghost" renderIcon={TaskComplete} iconDescription={t('checkedOut', 'Checked out')} size="sm">
{t('checkedOut', 'Checked out')}
</Button>
);
case checkOutButton.enabled && hasActiveVisitToday && isTodaysAppointment:
case checkOutButton.enabled && isTodaysAppointment && appointment.status === AppointmentStatus.CHECKEDIN:
return (
<Button onClick={handleCheckout} kind="danger--tertiary" size="sm">
{t('checkOut', 'Check out')}
</Button>
);
case checkInButton.enabled && !hasActiveVisitToday && isTodaysAppointment: {
case checkInButton.enabled && isTodaysAppointment && appointment.status === AppointmentStatus.SCHEDULED: {
return <CheckInButton patientUuid={patientUuid} appointment={appointment} />;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { render } from '@testing-library/react';
import React from 'react';
import AppointmentActions from './appointments-actions.component';
import { useTodaysVisits } from '../../hooks/useTodaysVisits';
import { type Appointment } from '../../types';
import { type Appointment, AppointmentKind, AppointmentStatus } from '../../types';
import { useConfig } from '@openmrs/esm-framework';

const appointment: Appointment = {
let appointment: Appointment = {
endDateTime: undefined,
uuid: '7cd38a6d-377e-491b-8284-b04cf8b8c6d8',
appointmentNumber: '0000',
patient: {
Expand Down Expand Up @@ -37,8 +37,8 @@ const appointment: Appointment = {
},
location: { name: 'HIV Clinic', uuid: '2131aff8-2e2a-480a-b7ab-4ac53250262b' },
startDateTime: new Date().toISOString(),
appointmentKind: 'WalkIn',
status: 'Scheduled',
appointmentKind: AppointmentKind.WALKIN,
status: null,
comments: 'Some comments',
additionalInfo: null,
providers: [{ uuid: '24252571-dd5a-11e6-9d9c-0242ac150002', display: 'Dr James Cook' }],
Expand All @@ -48,15 +48,6 @@ const appointment: Appointment = {
extensions: [],
};

jest.mock('../../hooks/useTodaysVisits', () => {
const originalModule = jest.requireActual('../../hooks/useTodaysVisits');

return {
...originalModule,
useTodaysVisits: jest.fn(),
};
});

jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
Expand All @@ -77,120 +68,63 @@ describe('AppointmentActions', () => {
jest.useRealTimers();
});

it('renders the check in button when appointment is today and the patient has not checked in and check in button enabled', () => {
it('renders the check in button when appointment is today and appointment status is scheduled and check in button enabled', () => {
appointment.status = AppointmentStatus.SCHEDULED;
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [],
}));
const props = { ...defaultProps };
const { getByText } = render(<AppointmentActions {...props} />);
const button = getByText(/check in/i);
expect(button).toBeInTheDocument();
});

it('does not renders the check in button when appointment is today and the patient has not checked in but the check-in button is disabled', () => {
it('does not renders the check in button when appointment is today and and appointment status is scheduled an in but the check-in button is disabled', () => {
appointment.status = AppointmentStatus.SCHEDULED;
useConfig.mockImplementation(() => ({
checkInButton: { enabled: false },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [],
}));
const props = { ...defaultProps };
const { queryByText } = render(<AppointmentActions {...props} />);
const button = queryByText('Check In');
expect(button).not.toBeInTheDocument();
});

it('renders the checked out button when the patient has checked out', () => {
it('renders the checked out status when appointment is completed', () => {
appointment.status = AppointmentStatus.COMPLETED;
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [
{
patient: { uuid: '8673ee4f-e2ab-4077-ba55-4980f408773e' },
startDatetime: new Date().toISOString(),
stopDatetime: new Date().toISOString(),
},
],
}));
const props = { ...defaultProps };
const { getByText } = render(<AppointmentActions {...props} />);
const button = getByText('Checked out');
expect(button).toBeInTheDocument();
});

it('renders the check out button when the patient has an active visit and today is the appointment date and the check out button enabled', () => {
it('renders the check out button when the patient is checked in it and today is the appointment date and the check out button enabled', () => {
appointment.status = AppointmentStatus.CHECKEDIN;
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [
{
patient: { uuid: '8673ee4f-e2ab-4077-ba55-4980f408773e' },
startDatetime: new Date().toISOString(),
stopDatetime: null,
},
],
}));
const props = { ...defaultProps, scheduleType: 'Scheduled' };
const { getByText } = render(<AppointmentActions {...props} />);
const button = getByText('Check out');
expect(button).toBeInTheDocument();
});

it('does not render check out button when the patient has an active visit and today is the appointment date but the check out button is disabled', () => {
it('does not render check out button when the patient id checed in and today is the appointment date but the check out button is disabled', () => {
appointment.status = AppointmentStatus.CHECKEDIN;
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: false },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [
{
patient: { uuid: '8673ee4f-e2ab-4077-ba55-4980f408773e' },
startDatetime: new Date().toISOString(),
stopDatetime: null,
},
],
}));
const props = { ...defaultProps, scheduleType: 'Scheduled' };
const { queryByText } = render(<AppointmentActions {...props} />);
const button = queryByText('Check out');
expect(button).not.toBeInTheDocument();
});

// commenting these tests out as this functionality is not implemented yet so not sure how they would have ever passed?
/*it('renders the correct button when today is the appointment date and the schedule type is pending', () => {
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [],
}));
const props = { ...defaultProps, scheduleType: 'Pending' };
render(<AppointmentActions {...props} />);
const button = screen.getByRole('button', { name: /Checked out/i });
expect(button).toBeInTheDocument();
});

it('renders the correct button when today is the appointment date and the schedule type is not pending', () => {
useConfig.mockImplementation(() => ({
checkInButton: { enabled: true },
checkOutButton: { enabled: true },
}));
useTodaysVisits.mockImplementation(() => ({
visits: [],
}));
const props = { ...defaultProps, scheduleType: 'Confirmed' };
render(<AppointmentActions {...props} />);
const button = screen.getByRole('button', { name: /Checked out/i });
expect(button).toBeInTheDocument();
});*/
});
27 changes: 17 additions & 10 deletions packages/esm-appointments-app/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ export interface AppointmentLocation {
name: string;
}

// note that the API supports two other statuses that we are not currently supporting: "Requested" and "WaitList"
export enum AppointmentStatus {
SCHEDULED = 'Scheduled',
CANCELLED = 'Cancelled',
MISSED = 'Missed',
CHECKEDIN = 'CheckedIn',
COMPLETED = 'Completed',
}

export enum AppointmentKind {
SCHEDULED = 'Scheduled',
WALKIN = 'WalkIn',
VIRTUAL = 'Virtual',
}

// TODO: remove interface elements that aren't actually present on the Appointment object returned from the Appointment API
export interface Appointment {
appointmentKind: string;
appointmentKind: AppointmentKind;
appointmentNumber: string;
comments: string;
endDateTime: Date | number | any;
Expand All @@ -33,7 +48,7 @@ export interface Appointment {
recurring: boolean;
service: AppointmentService;
startDateTime: string | any;
status: string;
status: AppointmentStatus;
uuid: string;
additionalInfo?: string | null;
serviceTypes?: Array<ServiceTypes> | null;
Expand Down Expand Up @@ -142,14 +157,6 @@ export enum DurationPeriod {
daily,
}

export enum AppointmentTypes {
SCHEDULED = 'scheduled',
CANCELLED = 'cancelled',
MISSED = 'missed',
CHECKEDIN = 'checkedin',
COMPLETED = 'completed',
}

export interface Identifier {
identifier: string;
identifierName?: string;
Expand Down