Skip to content

Commit

Permalink
O3-4043: Dispensing: Allow back dating dispense events for retrospect…
Browse files Browse the repository at this point in the history
…ive entryconst {container } =
  • Loading branch information
mogoodrich committed Oct 1, 2024
1 parent fa187f5 commit 1263ff5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 40 deletions.
24 changes: 20 additions & 4 deletions src/components/action-buttons.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getMostRecentMedicationDispenseStatus,
} from '../utils';
import { type PharmacyConfig } from '../config-schema';
import { initiateMedicationDispenseBody } from '../medication-dispense/medication-dispense.resource';
import { initiateMedicationDispenseBody, useProviders } from '../medication-dispense/medication-dispense.resource';
import DispenseForm from '../forms/dispense-form.component';
import PauseDispenseForm from '../forms/pause-dispense-form.component';
import CloseDispenseForm from '../forms/close-dispense-form.component';
Expand All @@ -26,6 +26,7 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ medicationRequestBundle,
const { t } = useTranslation();
const config = useConfig<PharmacyConfig>();
const session = useSession();
const providers = useProviders(config.dispenserProviderRoles);
const mostRecentMedicationDispenseStatus: MedicationDispenseStatus = getMostRecentMedicationDispenseStatus(
medicationRequestBundle.dispenses,
);
Expand Down Expand Up @@ -64,7 +65,12 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ medicationRequestBundle,
<DispenseForm
patientUuid={patientUuid}
encounterUuid={encounterUuid}
medicationDispense={initiateMedicationDispenseBody(medicationRequestBundle.request, session, true)}
medicationDispense={initiateMedicationDispenseBody(
medicationRequestBundle.request,
session,
providers,
true,
)}
medicationRequestBundle={medicationRequestBundle}
quantityRemaining={quantityRemaining}
mode="enter"
Expand All @@ -83,7 +89,12 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ medicationRequestBundle,
<PauseDispenseForm
patientUuid={patientUuid}
encounterUuid={encounterUuid}
medicationDispense={initiateMedicationDispenseBody(medicationRequestBundle.request, session, false)}
medicationDispense={initiateMedicationDispenseBody(
medicationRequestBundle.request,
session,
providers,
false,
)}
mode="enter"
/>,
)
Expand All @@ -100,7 +111,12 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ medicationRequestBundle,
<CloseDispenseForm
patientUuid={patientUuid}
encounterUuid={encounterUuid}
medicationDispense={initiateMedicationDispenseBody(medicationRequestBundle.request, session, false)}
medicationDispense={initiateMedicationDispenseBody(
medicationRequestBundle.request,
session,
providers,
false,
)}
mode="enter"
/>,
)
Expand Down
2 changes: 2 additions & 0 deletions src/forms/dispense-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
const checkIsValid = () => {
if (
medicationDispensePayload &&
medicationDispensePayload.performer &&
medicationDispensePayload.performer[0]?.actor.reference &&
medicationDispensePayload.quantity?.value &&
(!quantityRemaining || medicationDispensePayload?.quantity?.value <= quantityRemaining) &&
medicationDispensePayload.quantity?.code &&
Expand Down
3 changes: 2 additions & 1 deletion src/forms/medication-dispense-review.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jest.mock('@openmrs/esm-framework', () => {
return {
__esModule: true,
...originalModule,
OpenmrsDatePicker: jest.fn(() => <div />),
useConfig: jest.fn(() => ({
dispenseBehavior: {
allowModifyingPrescription: false,
Expand Down Expand Up @@ -129,7 +130,7 @@ describe('Medication Dispense Review Component tests', () => {
};

const mockUpdate: Function = jest.fn();
const { getByText, container } = render(
render(
<MedicationDispenseReview
medicationDispense={medicationDispense}
updateMedicationDispense={mockUpdate}
Expand Down
63 changes: 32 additions & 31 deletions src/forms/medication-dispense-review.component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ComboBox, DatePickerInput, Dropdown, NumberInput, Stack, TextArea } from '@carbon/react';
import { ComboBox, Dropdown, NumberInput, Stack, TextArea } from '@carbon/react';
import { OpenmrsDatePicker, useLayoutType, useConfig, useSession, userHasAccess } from '@openmrs/esm-framework';
import {
getConceptCodingUuid,
Expand Down Expand Up @@ -56,12 +56,11 @@ const MedicationDispenseReview: React.FC<MedicationDispenseReviewProps> = ({
const isTablet = useLayoutType() === 'tablet';

const allowEditing = config.dispenseBehavior.allowModifyingPrescription;
const providerRoles = config.dispenserProviderRoles;

const { orderConfigObject } = useOrderConfig();
const { substitutionTypeValueSet } = useSubstitutionTypeValueSet(config.valueSets.substitutionType.uuid);
const { substitutionReasonValueSet } = useSubstitutionReasonValueSet(config.valueSets.substitutionReason.uuid);
const providers = useProviders(providerRoles);
const providers = useProviders(config.dispenserProviderRoles);

// get the medication request associated with this dispense event
// (we fetch this so that we can use it below to determine if the formulation dispensed varies from what was
Expand Down Expand Up @@ -504,6 +503,7 @@ const MedicationDispenseReview: React.FC<MedicationDispenseReviewProps> = ({

<OpenmrsDatePicker
id="dispenseDate"
labelText={t('dispenseDate', 'Date of Dispense')}
minDate={prescriptionDate ? dayjs(prescriptionDate).startOf('day').toDate() : null}
maxDate={dayjs().toDate()}
onChange={(selectedDate) => {
Expand All @@ -515,36 +515,37 @@ const MedicationDispenseReview: React.FC<MedicationDispenseReviewProps> = ({
: selectedDate.toString(), // to preserve any time component, only update if the day actually changes
});
}}
value={dayjs(medicationDispense.whenHandedOver).toDate()}>
<DatePickerInput id="dispenseDate" labelText={t('dispenseDate', 'Date of Dispense')}></DatePickerInput>
</OpenmrsDatePicker>
value={dayjs(medicationDispense.whenHandedOver).startOf('day').toDate()}></OpenmrsDatePicker>

<ComboBox
id="dispenser"
light={isTablet}
initialSelectedItem={
providers
? providers.find(
(provider) => provider.uuid === medicationDispense?.performer[0].actor.reference.split('/')[1],
)
: null
}
onChange={({ selectedItem }) => {
updateMedicationDispense({
...medicationDispense,
performer: [
{
actor: {
reference: `Practitioner/${selectedItem?.uuid}`,
{providers && (
<ComboBox
id="dispenser"
light={isTablet}
initialSelectedItem={
medicationDispense?.performer[0].actor.reference
? providers.find(
(provider) => provider.uuid === medicationDispense?.performer[0].actor.reference.split('/')[1],
)
: null
}
onChange={({ selectedItem }) => {
updateMedicationDispense({
...medicationDispense,
performer: [
{
actor: {
reference: `Practitioner/${selectedItem?.uuid}`,
},
},
},
],
});
}}
items={providers}
itemToString={(item) => item?.person?.display}
titleText={t('dispensedBy', 'Dispensed by')}
/>
],
});
}}
items={providers}
itemToString={(item) => item?.person?.display}
required
titleText={t('dispensedBy', 'Dispensed by')}
/>
)}
</Stack>
</div>
);
Expand Down
14 changes: 11 additions & 3 deletions src/medication-dispense/medication-dispense.resource.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type MedicationRequest,
MedicationDispenseStatus,
MedicationRequestStatus,
type Provider,
} from '../types';
import dayjs from 'dayjs';

Expand Down Expand Up @@ -47,7 +48,7 @@ describe('Medication Dispense Resource tests', () => {
};
const abortController: AbortController = {
signal: undefined,
abort(reason?: any): void {},
abort(): void {},
};

saveMedicationDispense(medicationDispense, MedicationDispenseStatus.completed, abortController);
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('Medication Dispense Resource tests', () => {
};
const abortController: AbortController = {
signal: undefined,
abort(reason?: any): void {},
abort(): void {},
};

saveMedicationDispense(medicationDispense, MedicationDispenseStatus.completed, abortController);
Expand Down Expand Up @@ -205,11 +206,18 @@ describe('Medication Dispense Resource tests', () => {
links: undefined,
},
};
const medicationRequestExpirationPeriodInDay = 30;

const providers: Provider[] = [
{
uuid: 'ghi789',
person: null,
},
];

const medicationDispense: MedicationDispense = initiateMedicationDispenseBody(
activeMedicationRequest,
session,
providers,
true,
);

Expand Down
9 changes: 8 additions & 1 deletion src/medication-dispense/medication-dispense.resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type MedicationDispenseStatus,
type MedicationRequest,
type OrderConfig,
type Provider,
type ProviderRequestResponse,
type ValueSet,
} from '../types';
Expand Down Expand Up @@ -96,6 +97,7 @@ export function useValueSet(uuid: string) {
export function initiateMedicationDispenseBody(
medicationRequest: MedicationRequest,
session: Session,
providers: Provider[],
populateDispenseInformation: boolean,
): MedicationDispense {
let medicationDispense: MedicationDispense = {
Expand All @@ -113,7 +115,12 @@ export function initiateMedicationDispenseBody(
performer: [
{
actor: {
reference: session?.currentProvider ? `Practitioner/${session.currentProvider.uuid}` : '',
reference:
session?.currentProvider &&
providers &&
providers.some((provider) => provider.uuid == session.currentProvider.uuid)
? `Practitioner/${session.currentProvider.uuid}`
: '',
},
},
],
Expand Down

0 comments on commit 1263ff5

Please sign in to comment.