-
Notifications
You must be signed in to change notification settings - Fork 32
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
Changes from 3 commits
1511795
cfb6d33
a6d86fc
9af75a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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/useDispenseStock'; | ||
|
||
interface DispenseFormProps { | ||
medicationDispense: MedicationDispense; | ||
|
@@ -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>(); | ||
|
||
|
@@ -67,6 +77,30 @@ const DispenseForm: React.FC<DispenseFormProps> = ({ | |
medicationRequestBundle, | ||
config.dispenseBehavior.restrictTotalQuantityDispensed, | ||
); | ||
|
||
if (config.enableStockDispense) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is breaking up the logic around fulfiller status, can we break this up into a separate "then" block, or at least move it up to line 73, before the "const newFulfillerStatus..." line? Also, defer to you about the order operations... ie, would you want to move the dispense request before the "saveMedicationRequest" so that you only update the medication request if the stock update is successful? Of course, then you run into the opposite risk: that the stock could be updated and then the dispense fails. Would be great to make this a little more transactional, but, admittedly, that is likely difficult. |
||
const stockDispenseRequestPayload = createStockDispenseRequestPayload( | ||
inventoryItem, | ||
patientUuid, | ||
encounterUuid, | ||
medicationDispensePayload, | ||
); | ||
sendStockDispenseRequest(stockDispenseRequestPayload, abortController).then( | ||
() => { | ||
showToast({ | ||
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 }); | ||
}, | ||
); | ||
} | ||
if (getFulfillerStatus(medicationRequestBundle.request) !== newFulfillerStatus) { | ||
return updateMedicationRequestFulfillerStatus( | ||
getUuidFromReference( | ||
|
@@ -134,7 +168,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) { | ||
|
@@ -159,7 +195,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', | ||
|
@@ -169,18 +204,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', | ||
|
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 './useDispenseStock'; | ||
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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. format this to MM/YYYY There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason why it should marked that way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really but it looks better when shorter There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import useSWR from 'swr'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we've started a pattern of calling something "useDispenseStock" but then including multiple different hooks in it, but I think it would be better to get away from this and call this something like stock.resource.tsx or stock-management.resource.tsx. Do you agree @denniskigen @ibacher ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, that's substantially more consistent with every other app in the ecosystem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @ibacher ! So @donaldkibet yes, let's please change the name of this file. |
||
import { openmrsFetch } from '@openmrs/esm-framework'; | ||
import { type InventoryItem, type MedicationDispense } from '../../types'; | ||
import { getUuidFromReference } from '../../utils'; | ||
|
||
//TODO: Add configuration to retrieve the stock dispense endpoint | ||
/** | ||
* 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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you are setting limit=10, will there be a problem if there are more than 10 matching inventory items? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @slubwama can give us a clear explanation why we need to add this and if its really necessary we find how to pass it from the UI. |
||
const url = `/ws/rest/v1/stockmanagement/stockiteminventory?v=default&limit=10&totalCount=true&drugUuid=${drugUuid}&includeBatchNo=true`; | ||
const { data, error, isLoading } = useSWR<{ data: { results: Array<InventoryItem> } }>(url, openmrsFetch); | ||
return { inventoryItems: data?.data?.results ?? [], error, isLoading }; | ||
}; | ||
|
||
/** | ||
* Interface for the stock dispense request object. | ||
*/ | ||
export type StockDispenseRequest = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to include this here instead of in types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was left by mistake, moved to types file |
||
dispenseLocation: string; | ||
patient: string; | ||
order: string; | ||
encounter: string; | ||
stockItem: string; | ||
stockBatch: string; | ||
stockItemPackagingUOM: string; | ||
quantity: number; | ||
}; | ||
|
||
/** | ||
* 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, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"backendDependencies": { | ||
"fhir2": ">=1.2", | ||
"webservices.rest": "^2.2.0" | ||
"webservices.rest": "^2.2.0", | ||
"stockmanagement": "^1.4.1 " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @donaldkibet won't this mean implementations that don't use stock management will keep getting dependency errors? Is there a middle ground for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with @pirupius , thanks for flagging this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm... We probably do need a way of indicating optional dependencies, though, right, i.e., if this only works with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thing for the backlog: https://openmrs.atlassian.net/browse/O3-3266 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried to relax this with "stockmanagement": ">=1.4.1" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way, for now, we can't have it in backend dependencies, because it will create an annoying pop-up for everyone who's not using stock management, hence the ticket. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it then be removed entirely? If yes, how best can we still have this requirement, at least somewhere in the application? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. For now, I don't think we can. We need this feature to properly support that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this needs to be removed for now. |
||
}, | ||
"pages": [ | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain the same behavior by default, this default value should be false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree