1
1
import { openmrsFetch } from '@openmrs/esm-framework' ;
2
2
import dayjs from 'dayjs' ;
3
- import {
4
- finalHIVCodeConcept ,
5
- finalPositiveHIVValueConcept ,
6
- computedHIV_StatusConcept ,
7
- encounterRepresentation ,
8
- covidOutcomesCohortUUID ,
9
- } from './constants' ;
3
+ import { BASE_FHIR_API_URL , BASE_WS_API_URL , encounterRepresentation } from './constants' ;
10
4
import useSWR from 'swr' ;
11
-
12
- const BASE_WS_API_URL = '/ws/rest/v1/' ;
13
- const BASE_FHIR_API_URL = '/ws/fhir2/R4/' ;
5
+ import { configSchema } from './config-schema' ;
14
6
15
7
export function fetchLastVisit ( uuid : string ) {
16
8
return openmrsFetch ( `/ws/fhir2/R4/Encounter?patient=${ uuid } &_sort=-date&_count=1` ) ;
@@ -145,7 +137,7 @@ export async function getPatientListsForPatient(patientUuid: string) {
145
137
146
138
export function fetchPatientsFinalHIVStatus ( patientUUID : string ) {
147
139
return openmrsFetch (
148
- `/ws/fhir2/R4/Observation?code=${ finalHIVCodeConcept } &value-concept=${ finalPositiveHIVValueConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
140
+ `/ws/fhir2/R4/Observation?code=${ configSchema . obsConcepts . _default . finalHIVCodeConcept } &value-concept=${ configSchema . obsConcepts . _default . finalPositiveHIVValueConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
149
141
) . then ( ( { data } ) => {
150
142
if ( data . entry ?. length ) {
151
143
return data . entry [ 0 ] . resource . valueCodeableConcept . coding [ 0 ] . display ;
@@ -166,7 +158,7 @@ export function fetchPatientObservationFromEncounter(
166
158
167
159
export function fetchPatientComputedConcept_HIV_Status ( patientUUID : string ) {
168
160
return openmrsFetch (
169
- `/ws/fhir2/R4/Observation?code=${ computedHIV_StatusConcept } &value-concept=${ computedHIV_StatusConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
161
+ `/ws/fhir2/R4/Observation?code=${ configSchema . obsConcepts . _default . computedHIV_StatusConcept } &value-concept=${ configSchema . obsConcepts . _default . computedHIV_StatusConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
170
162
) . then ( ( { data } ) => {
171
163
if ( data . entry ?. length ) {
172
164
return data . entry [ 0 ] . resource . valueCodeableConcept . coding [ 0 ] . display ;
@@ -191,7 +183,9 @@ export function fetchPatientLastEncounter(patientUuid: string, encounterType) {
191
183
}
192
184
193
185
export function fetchPatientCovidOutcome ( ) {
194
- return openmrsFetch ( `/ws/rest/v1/reportingrest/cohort/${ covidOutcomesCohortUUID } ` ) . then ( ( { data } ) => {
186
+ return openmrsFetch (
187
+ `/ws/rest/v1/reportingrest/cohort/${ configSchema . obsConcepts . _default . covidOutcomesCohortUUID } ` ,
188
+ ) . then ( ( { data } ) => {
195
189
if ( data . members ?. length ) {
196
190
let patientRefs = data . members . map ( ( member ) => {
197
191
return member . uuid ;
@@ -237,26 +231,6 @@ export function fetchFormsClobData(valueReferences: string[]) {
237
231
return Promise . all ( valueReferences ?. map ( ( ref ) => fetch ( ref ) ) ) ;
238
232
}
239
233
240
- export async function fetchMambaReportData ( reportId : string ) {
241
- try {
242
- const response = await openmrsFetch ( `ws/rest/v1/mamba/report?report_id=${ reportId } ` ) ;
243
- const data = await response . json ( ) ;
244
-
245
- if ( data && data . results && data . results . length > 0 ) {
246
- const record = data . results [ 0 ] . record ;
247
-
248
- for ( const item of record ) {
249
- return item . value ? parseInt ( item . value , 10 ) : 0 ;
250
- }
251
- }
252
-
253
- return 0 ;
254
- } catch ( error ) {
255
- console . error ( `Error fetching data for report_id=${ reportId } : ` , error ) ;
256
- throw new Error ( `Error fetching data for report_id=${ reportId } : ${ error } ` ) ;
257
- }
258
- }
259
-
260
234
export function fetchEtlData (
261
235
reportType : 'fetchMambaAncData' | 'MotherHivStatus' ,
262
236
reportId ?: string ,
@@ -358,12 +332,11 @@ export async function getCohortList(
358
332
`/ws/rest/v1/encounter?encounterType=${ encounterType } &patient=${ member . patient . uuid } &v=${ encounterRepresentation } ` ,
359
333
) . then ( ( { data } ) => {
360
334
if ( data . results . length ) {
361
- const sortedEncounters = data . results
362
- . sort (
363
- ( firstEncounter , secondEncounter ) =>
364
- new Date ( secondEncounter . encounterDatetime ) . getTime ( ) -
365
- new Date ( firstEncounter . encounterDatetime ) . getTime ( ) ,
366
- ) ;
335
+ const sortedEncounters = data . results . sort (
336
+ ( firstEncounter , secondEncounter ) =>
337
+ new Date ( secondEncounter . encounterDatetime ) . getTime ( ) -
338
+ new Date ( firstEncounter . encounterDatetime ) . getTime ( ) ,
339
+ ) ;
367
340
return sortedEncounters [ 0 ] ;
368
341
}
369
342
return null ;
@@ -372,3 +345,16 @@ export async function getCohortList(
372
345
) ;
373
346
}
374
347
}
348
+
349
+ export function useMambaReportData ( reportId : string ) {
350
+ const { data, error, isLoading } = useSWR < { data : { results : any } } , Error > (
351
+ `${ BASE_WS_API_URL } /mamba/report?report_id=${ reportId } ` ,
352
+ openmrsFetch ,
353
+ ) ;
354
+
355
+ return {
356
+ data : data ?. data ? data ?. data ?. results : '0' ,
357
+ error,
358
+ isLoading,
359
+ } ;
360
+ }
0 commit comments