generated from openmrs/openmrs-esm-template-app
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1511795
(chore) upgrade `@openmrs/esm-framework` version
donaldkibet cfb6d33
(feat) O3-3259 Add ability to deduct stock items while performing dis…
donaldkibet a6d86fc
code reviews changes updated
donaldkibet 9af75a9
code reviews changes
donaldkibet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`; | ||
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 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, | ||
}; | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
format this to MM/YYYY
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.
Any specific reason why it should marked that way?
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.
not really but it looks better when shorter
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.
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.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.
Tend to agree with @ibacher