Skip to content

Commit

Permalink
code reviews changes
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldkibet committed May 24, 2024
1 parent a6d86fc commit 008f671
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down
51 changes: 26 additions & 25 deletions src/forms/dispense-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,30 +77,6 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
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(
Expand All @@ -112,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
2 changes: 1 addition & 1 deletion src/forms/stock-dispense/stock-dispense.component.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<InventoryItem> } }>(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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"backendDependencies": {
"fhir2": ">=1.2",
"webservices.rest": "^2.2.0",
"stockmanagement": "^1.4.1 "
"stockmanagement": ">=1.4.1"
},
"pages": [
{
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 008f671

Please sign in to comment.