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

(feat) O3-3259 Add ability to deduct stock items while performing dispensing medication #107

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ export const configSchema = {
},
},
},
enableStockDispense: {
_type: Type.Boolean,
_description: 'Enable or disable stock deduction during the dispensing process. Requires the stock management module to be installed and configured.',
_default: false,
},
};

export interface PharmacyConfig {
Expand Down Expand Up @@ -162,4 +167,5 @@ export interface PharmacyConfig {
uuid: string;
};
};
enableStockDispense: boolean;
}
63 changes: 54 additions & 9 deletions src/forms/dispense-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
import { Button, FormLabel, InlineLoading } from '@carbon/react';
import styles from './forms.scss';
import { closeOverlay } from '../hooks/useOverlay';
import { type MedicationDispense, MedicationDispenseStatus, type MedicationRequestBundle } from '../types';
import {
type MedicationDispense,
MedicationDispenseStatus,
type MedicationRequestBundle,
type InventoryItem,
} from '../types';
import { saveMedicationDispense } from '../medication-dispense/medication-dispense.resource';
import MedicationDispenseReview from './medication-dispense-review.component';
import {
Expand All @@ -22,6 +27,8 @@ import {
} from '../utils';
import { updateMedicationRequestFulfillerStatus } from '../medication-request/medication-request.resource';
import { type PharmacyConfig } from '../config-schema';
import StockDispense from './stock-dispense/stock-dispense.component';
import { createStockDispenseRequestPayload, sendStockDispenseRequest } from './stock-dispense/stock.resource';

interface DispenseFormProps {
medicationDispense: MedicationDispense;
Expand All @@ -45,6 +52,9 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
const { patient, isLoading } = usePatient(patientUuid);
const config = useConfig<PharmacyConfig>();

// Keep track of inventory item
const [inventoryItem, setInventoryItem] = useState<InventoryItem>();

// Keep track of medication dispense payload
const [medicationDispensePayload, setMedicationDispensePayload] = useState<MedicationDispense>();

Expand Down Expand Up @@ -78,6 +88,31 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
}
return response;
})
.then((response) => {
const { status } = response;
if ((config.enableStockDispense && status === 201) || status === 200) {
const stockDispenseRequestPayload = createStockDispenseRequestPayload(
inventoryItem,
patientUuid,
encounterUuid,
medicationDispensePayload,
);
sendStockDispenseRequest(stockDispenseRequestPayload, abortController).then(
() => {
showToast({
critical: true,
title: t('stockDispensed', 'Stock dispensed'),
kind: 'success',
description: t('stockDispensedSuccessfully', 'Stock dispensed successfully and batch level updated.'),
});
},
(error) => {
showToast({ title: 'Stock dispense error', kind: 'error', description: error?.message });
},
);
}
return response;
})
.then(
({ status }) => {
if (status === 201 || status === 200) {
Expand Down Expand Up @@ -134,7 +169,9 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
useEffect(() => setMedicationDispensePayload(medicationDispense), [medicationDispense]);

// check is valid on any changes
useEffect(checkIsValid, [medicationDispensePayload, quantityRemaining]);
useEffect(checkIsValid, [medicationDispensePayload, quantityRemaining, inventoryItem]);

const isButtonDisabled = (config.enableStockDispense ? !inventoryItem : false) || !isValid || isSubmitting;

const bannerState = useMemo(() => {
if (patient) {
Expand All @@ -159,7 +196,6 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
)}
{patient && <ExtensionSlot name="patient-header-slot" state={bannerState} />}
<section className={styles.formGroup}>
{/* <span style={{ marginTop: "1rem" }}>1. {t("drug", "Drug")}</span>*/}
<FormLabel>
{t(
config.dispenseBehavior.allowModifyingPrescription ? 'drugHelpText' : 'drugHelpTextNoEdit',
Expand All @@ -169,18 +205,27 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
)}
</FormLabel>
{medicationDispensePayload ? (
<MedicationDispenseReview
medicationDispense={medicationDispensePayload}
updateMedicationDispense={setMedicationDispensePayload}
quantityRemaining={quantityRemaining}
/>
<div>
<MedicationDispenseReview
medicationDispense={medicationDispensePayload}
updateMedicationDispense={setMedicationDispensePayload}
quantityRemaining={quantityRemaining}
/>
{config.enableStockDispense && (
<StockDispense
inventoryItem={inventoryItem}
medicationDispense={medicationDispense}
updateInventoryItem={setInventoryItem}
/>
)}
</div>
) : null}
</section>
<section className={styles.buttonGroup}>
<Button disabled={isSubmitting} onClick={() => closeOverlay()} kind="secondary">
{t('cancel', 'Cancel')}
</Button>
<Button disabled={!isValid || isSubmitting} onClick={handleSubmit}>
<Button disabled={isButtonDisabled} onClick={handleSubmit}>
{t(
mode === 'enter' ? 'dispensePrescription' : 'saveChanges',
mode === 'enter' ? 'Dispense prescription' : 'Save changes',
Expand Down
63 changes: 63 additions & 0 deletions src/forms/stock-dispense/stock-dispense.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { ComboBox, InlineLoading, InlineNotification, Layer } from '@carbon/react';
import { type MedicationDispense, type InventoryItem } from '../../types';
import { useDispenseStock } from './stock.resource';
import { formatDate } from '@openmrs/esm-framework';
import { useTranslation } from 'react-i18next';

type StockDispenseProps = {
medicationDispense: MedicationDispense;
updateInventoryItem: (inventoryItem: InventoryItem) => void;
inventoryItem: InventoryItem;
};

const StockDispense: React.FC<StockDispenseProps> = ({ medicationDispense, updateInventoryItem }) => {
const { t } = useTranslation();
const drugUuid = medicationDispense?.medicationReference?.reference?.split('/')[1];
const { inventoryItems, error, isLoading } = useDispenseStock(drugUuid);

const toStockDispense = (inventoryItems) => {
return t(
'stockDispenseDetails',
'Batch: {{batchNumber}} - Quantity: {{quantity}} ({{quantityUoM}}) - Expiry: {{expiration}}',
{
batchNumber: inventoryItems.batchNumber,
quantity: Math.floor(inventoryItems.quantity),
quantityUoM: inventoryItems.quantityUoM,
expiration: formatDate(new Date(inventoryItems.expiration)),
Copy link

Choose a reason for hiding this comment

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

format this to MM/YYYY

Copy link
Member Author

Choose a reason for hiding this comment

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

Any specific reason why it should marked that way?

Copy link

Choose a reason for hiding this comment

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

not really but it looks better when shorter

Copy link
Member

Choose a reason for hiding this comment

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

Date formatting should remain consistent within the application unless there's a strong reason not to, so I think formatDate() is more likely to be correct here.

Copy link
Member

Choose a reason for hiding this comment

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

Tend to agree with @ibacher

},
);
};

if (error) {
return (
<InlineNotification
aria-label="closes notification"
kind="error"
lowContrast={true}
statusIconDescription="notification"
subtitle={t('errorLoadingInventoryItems', 'Error fetching inventory items')}
title={t('error', 'Error')}
/>
);
}

if (isLoading) {
return <InlineLoading description={t('loadingInventoryItems', 'Loading inventory items...')} />;
}

return (
<Layer>
<ComboBox
id="stockDispense"
items={inventoryItems}
onChange={({ selectedItem }) => updateInventoryItem(selectedItem)}
itemToString={(item) => (item ? toStockDispense(item) : '')}
titleText={t('stockDispense', 'Stock Dispense')}
placeholder={t('selectStockDispense', 'Select stock to dispense from')}
/>
</Layer>
);
};

export default StockDispense;
67 changes: 67 additions & 0 deletions src/forms/stock-dispense/stock.resource.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import useSWR from 'swr';
import { openmrsFetch, useSession } from '@openmrs/esm-framework';
import { type StockDispenseRequest, type InventoryItem, type MedicationDispense } from '../../types';
import { getUuidFromReference } from '../../utils';

//TODO: Add configuration to retrieve the stock dispense endpoint
// For stock dispense to work, stock management module should be installed and configured
/**
* Fetches the inventory items for a given drug UUID.
*
* @param {string} drugUuid - The UUID of the drug.
* @returns {Array} - The inventory items.
*/
export const useDispenseStock = (drugUuid: string) => {
const session = useSession();
const url = `/ws/rest/v1/stockmanagement/stockiteminventory?v=default&totalCount=true&drugUuid=${drugUuid}&includeBatchNo=true&groupBy=LocationStockItemBatchNo&dispenseLocationUuid=${session?.sessionLocation?.uuid}&includeStrength=1&includeConceptRefIds=1&emptyBatch=1&emptyBatchLocationUuid=${session?.sessionLocation?.uuid}&dispenseAtLocation=1`;
Copy link
Member

Choose a reason for hiding this comment

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

Not to weigh into this after the fact, but, thinking about the TAC call we are on right now, it would be great if, long-term, the stock management module support an FHIR endpoint so that it would hopefully be more standardized.

const { data, error, isLoading } = useSWR<{ data: { results: Array<InventoryItem> } }>(url, openmrsFetch);
return { inventoryItems: data?.data?.results ?? [], error, isLoading };
};

/**
* Sends a POST request to the inventory dispense endpoint with the provided stock dispense request.
*
* @param {AbortController} abortController - The AbortController used to cancel the request.
* @returns {Promise<Response>} - A Promise that resolves to the response of the POST request.
*/
export async function sendStockDispenseRequest(
stockDispenseRequest,
abortController: AbortController,
): Promise<Response> {
const url = '/ws/rest/v1/stockmanagement/dispenserequest';
return await openmrsFetch(url, {
method: 'POST',
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ dispenseItems: [stockDispenseRequest] }),
});
}

/**
* Creates a stock dispense request payload.
*
* @param inventoryItem - The inventory item to dispense.
* @param patientUuid - The UUID of the patient.
* @param encounterUuid - The UUID of the encounter.
* @param medicationDispensePayload - The medication dispense payload.
* @returns The stock dispense request payload.
*/
export const createStockDispenseRequestPayload = (
inventoryItem: InventoryItem,
patientUuid: string,
encounterUuid: string,
medicationDispensePayload: MedicationDispense,
): StockDispenseRequest => {
return {
dispenseLocation: inventoryItem.locationUuid,
patient: patientUuid,
order: getUuidFromReference(medicationDispensePayload.authorizingPrescription[0].reference),
encounter: encounterUuid,
stockItem: inventoryItem?.stockItemUuid,
stockBatch: inventoryItem.stockBatchUuid,
stockItemPackagingUOM: inventoryItem.quantityUoMUuid,
quantity: medicationDispensePayload.quantity.value,
};
};
1 change: 1 addition & 0 deletions src/location/location.resource.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const pharmacyConfig: PharmacyConfig = {
substitutionReason: { uuid: '' },
substitutionType: { uuid: '' },
},
enableStockDispense: false,
};

describe('Location Resource tests', () => {
Expand Down
35 changes: 35 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,38 @@ export interface ValueSet {
];
};
}

export type InventoryItem = {
partyUuid: string;
locationUuid: string;
partyName: string;
stockItemUuid: string | null;
drugId: string | null;
drugUuid: string | null;
drugStrength: string | null;
conceptId: string | null;
conceptUuid: string | null;
stockBatchUuid: string;
batchNumber: string;
quantity: number;
quantityUoM: string;
quantityFactor: number;
quantityUoMUuid: string;
expiration: string;
commonName: string | null;
acronym: string | null;
drugName: string | null;
conceptName: string | null;
resourceVersion: string;
};

export type StockDispenseRequest = {
dispenseLocation: string;
patient: string;
order: string;
encounter: string;
stockItem: string;
stockBatch: string;
stockItemPackagingUOM: string;
quantity: number;
};
Loading