From 35009a6b35e45af0135b818da886bd84a2e44d8e Mon Sep 17 00:00:00 2001 From: Laurent Ouma <98098891+Omoshlawi@users.noreply.github.com> Date: Fri, 10 Jan 2025 12:54:35 +0400 Subject: [PATCH] fix: update interventions and packages hooks to use new API endpoints and response structure (#538) --- .../src/hooks/useInterventions.ts | 44 ++++++++++++++----- .../esm-billing-app/src/hooks/usePackages.ts | 22 +++------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/packages/esm-billing-app/src/hooks/useInterventions.ts b/packages/esm-billing-app/src/hooks/useInterventions.ts index 6ec2383ed..9a56eb621 100644 --- a/packages/esm-billing-app/src/hooks/useInterventions.ts +++ b/packages/esm-billing-app/src/hooks/useInterventions.ts @@ -1,22 +1,42 @@ -import { FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework'; +import { FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import useSWR from 'swr'; -import { BillingConfig } from '../config-schema'; import { SHAIntervension } from '../types'; -import { interventions } from './benefits.mock'; export const useInterventions = (code: string) => { - const { hieBaseUrl } = useConfig(); - - const url = `${hieBaseUrl}/master/benefit-master/code?searchKey=${code}`; - const { isLoading, error, data } = useSWR>(url, async (key) => { - await new Promise((resolve, reject) => { - setTimeout(resolve, 2000); + const fetcher = (url: string) => { + return openmrsFetch(url, { + method: 'POST', + body: { + searchKeyAndValues: { + // scheme_code: 'UHC', + package_code: code, + }, + }, }); - return interventions?.filter((inter) => inter.interventionCode.includes(code)); - }); + }; + const url = `${restBaseUrl}/insuranceclaims/claims/interventions/query?code=${code}`; + const { isLoading, error, data } = useSWR< + FetchResponse<{ + status: string; + data: Array<{ + interventionName: string; + interventionCode: string; + interventionPackage: string; + interventionSubPackage: string; + interventionDescription?: string; + }>; + }> + >(url, fetcher); return { isLoading, - interventions: data ?? [], + interventions: (data?.data?.data ?? []).map( + ({ interventionCode, interventionName, interventionPackage, interventionSubPackage, interventionDescription }) => + ({ + interventionCode, + subCategoryBenefitsPackage: interventionSubPackage, + interventionName, + } as SHAIntervension), + ), error, }; }; diff --git a/packages/esm-billing-app/src/hooks/usePackages.ts b/packages/esm-billing-app/src/hooks/usePackages.ts index 579bb1b9d..372ded629 100644 --- a/packages/esm-billing-app/src/hooks/usePackages.ts +++ b/packages/esm-billing-app/src/hooks/usePackages.ts @@ -1,35 +1,27 @@ -import { FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework'; +import { FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import useSWR from 'swr'; -import { BillingConfig } from '../config-schema'; import { Package } from '../types'; -import { category } from './benefits.mock'; /** * Hook that return a list of sha benefits category/packages * @returns */ const usePackages = () => { - const { hieBaseUrl } = useConfig(); - const url = `${hieBaseUrl}/master/category/all-`; + const url = `${restBaseUrl}/insuranceclaims/claims/benefit-package`; - const { data, isLoading, error } = useSWR>( + const { data, isLoading, error } = useSWR>>( url, - async () => { - await new Promise((resolve, reject) => { - setTimeout(resolve, 2000); - }); - return category; - }, + openmrsFetch, ); return { isLoading, - packages: (data ?? []).map( + packages: (data?.data ?? []).map( (category) => ({ - uuid: `${category.id}`, + uuid: `${category.code}`, packageCode: category.code, - packageName: category.categoryName, + packageName: category.name, } as Package), ), error,