diff --git a/src/config-schema.ts b/src/config-schema.ts index 0591add..6421e2f 100644 --- a/src/config-schema.ts +++ b/src/config-schema.ts @@ -124,7 +124,7 @@ export const configSchema = { enableStockDispense: { _type: Type.Boolean, _description: 'To enable or disable the deduction of stock during the dispensing process', - _default: true, + _default: false, }, }; diff --git a/src/forms/dispense-form.component.tsx b/src/forms/dispense-form.component.tsx index 1b129a2..3285f7c 100644 --- a/src/forms/dispense-form.component.tsx +++ b/src/forms/dispense-form.component.tsx @@ -28,7 +28,7 @@ import { 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'; +import { createStockDispenseRequestPayload, sendStockDispenseRequest } from './stock-dispense/stock.resource'; interface DispenseFormProps { medicationDispense: MedicationDispense; @@ -77,30 +77,6 @@ const DispenseForm: React.FC = ({ medicationRequestBundle, config.dispenseBehavior.restrictTotalQuantityDispensed, ); - - if (config.enableStockDispense) { - 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( @@ -112,6 +88,31 @@ const DispenseForm: React.FC = ({ } 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) { diff --git a/src/forms/stock-dispense/stock-dispense.component.tsx b/src/forms/stock-dispense/stock-dispense.component.tsx index 864b7fe..2dd234b 100644 --- a/src/forms/stock-dispense/stock-dispense.component.tsx +++ b/src/forms/stock-dispense/stock-dispense.component.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { ComboBox, InlineLoading, InlineNotification, Layer } from '@carbon/react'; import { type MedicationDispense, type InventoryItem } from '../../types'; -import { useDispenseStock } from './useDispenseStock'; +import { useDispenseStock } from './stock.resource'; import { formatDate } from '@openmrs/esm-framework'; import { useTranslation } from 'react-i18next'; diff --git a/src/forms/stock-dispense/useDispenseStock.tsx b/src/forms/stock-dispense/stock.resource.tsx similarity index 82% rename from src/forms/stock-dispense/useDispenseStock.tsx rename to src/forms/stock-dispense/stock.resource.tsx index b7ec690..015f1c2 100644 --- a/src/forms/stock-dispense/useDispenseStock.tsx +++ b/src/forms/stock-dispense/stock.resource.tsx @@ -1,6 +1,6 @@ import useSWR from 'swr'; -import { openmrsFetch } from '@openmrs/esm-framework'; -import { type InventoryItem, type MedicationDispense } from '../../types'; +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 @@ -11,25 +11,12 @@ import { getUuidFromReference } from '../../utils'; * @returns {Array} - The inventory items. */ export const useDispenseStock = (drugUuid: string) => { - const url = `/ws/rest/v1/stockmanagement/stockiteminventory?v=default&limit=10&totalCount=true&drugUuid=${drugUuid}&includeBatchNo=true`; + 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 } }>(url, openmrsFetch); return { inventoryItems: data?.data?.results ?? [], error, isLoading }; }; -/** - * Interface for the stock dispense request object. - */ -export type StockDispenseRequest = { - 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. * diff --git a/src/types.ts b/src/types.ts index c2db66a..b73ef30 100644 --- a/src/types.ts +++ b/src/types.ts @@ -499,3 +499,14 @@ export type InventoryItem = { conceptName: string | null; resourceVersion: string; }; + +export type StockDispenseRequest = { + dispenseLocation: string; + patient: string; + order: string; + encounter: string; + stockItem: string; + stockBatch: string; + stockItemPackagingUOM: string; + quantity: number; +};