generated from openmrs/openmrs-esm-template-app
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) O3-3259 Add ability to deduct stock items while performing dis…
…pensing medication (#107) * (chore) upgrade `@openmrs/esm-framework` version * (feat) O3-3259 Add ability to deduct stock items while performing dispensing * code reviews changes updated * code reviews changes
- Loading branch information
1 parent
784c3d1
commit 347959b
Showing
6 changed files
with
226 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
}, | ||
); | ||
}; | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters