1
- import { openmrsFetch } from '@openmrs/esm-framework' ;
1
+ import { fhirBaseUrl , openmrsFetch , restBaseUrl } 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 { 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' ;
6
+ import { type ReportDataArray } from './types' ;
14
7
15
8
export function fetchLastVisit ( uuid : string ) {
16
- return openmrsFetch ( `/ws/fhir2/R4 /Encounter?patient=${ uuid } &_sort=-date&_count=1` ) ;
9
+ return openmrsFetch ( `${ fhirBaseUrl } /Encounter?patient=${ uuid } &_sort=-date&_count=1` ) ;
17
10
}
18
11
19
12
export function fetchPatientList ( offSet : number = 0 , pageSize : number = 10 ) {
20
- return openmrsFetch ( `/ws/fhir2/R4 /Patient?_getpagesoffset=${ offSet } &_count=${ pageSize } &_summary=data` ) ;
13
+ return openmrsFetch ( `${ fhirBaseUrl } /Patient?_getpagesoffset=${ offSet } &_count=${ pageSize } &_summary=data` ) ;
21
14
}
22
15
23
16
export function fetchTodayClients ( ) {
24
17
let date = dayjs ( ) . format ( 'YYYY-MM-DD' ) ;
25
- return openmrsFetch ( `/ws/fhir2/R4 /Encounter?date=${ date } ` ) . then ( ( { data } ) => {
18
+ return openmrsFetch ( `${ fhirBaseUrl } /Encounter?date=${ date } ` ) . then ( ( { data } ) => {
26
19
if ( data . entry ?. length ) {
27
20
return cleanDuplicatePatientReferences ( data ) ;
28
21
}
@@ -35,7 +28,7 @@ export function fetchPatientsFromObservationCodeConcept(codeConcept: string, val
35
28
let startDate = dayjs ( ) . subtract ( cutOffDays , 'day' ) . format ( 'YYYY-MM-DD' ) ;
36
29
37
30
return openmrsFetch (
38
- `/ws/fhir2/R4 /Observation?code=${ codeConcept } ${ valueConcept ? `&value-concept=${ valueConcept } ` : '' } ${
31
+ `${ fhirBaseUrl } /Observation?code=${ codeConcept } ${ valueConcept ? `&value-concept=${ valueConcept } ` : '' } ${
39
32
cutOffDays ? `&_lastUpdated=ge${ startDate } &_lastUpdated=le${ endDate } ` : ''
40
33
} `,
41
34
) . then ( ( { data } ) => {
@@ -54,13 +47,13 @@ function cleanDuplicatePatientReferences(data) {
54
47
patientRefs = Array . from ( patientRefs ) ;
55
48
return Promise . all (
56
49
patientRefs . map ( ( ref ) => {
57
- return openmrsFetch ( BASE_FHIR_API_URL + ref ) ;
50
+ return openmrsFetch ( fhirBaseUrl + `/` + ref ) ;
58
51
} ) ,
59
52
) ;
60
53
}
61
54
62
55
export function performPatientSearch ( query , objectVersion ) {
63
- return openmrsFetch ( `${ BASE_WS_API_URL } /patient?q=${ query } ${ objectVersion ? `&v=${ objectVersion } ` : '' } ` , {
56
+ return openmrsFetch ( `${ restBaseUrl } /patient?q=${ query } ${ objectVersion ? `&v=${ objectVersion } ` : '' } ` , {
64
57
method : 'GET' ,
65
58
} ) ;
66
59
}
@@ -77,27 +70,25 @@ export function getPatients(searchPhrase?: string, offset?: number, pageSize: nu
77
70
}
78
71
79
72
export async function getCohort ( cohortUuid : string , version ?: string ) {
80
- const { data } = await openmrsFetch (
81
- BASE_WS_API_URL + `cohortm/cohort/${ cohortUuid } ${ version ? `?v=${ version } ` : `` } ` ,
82
- ) ;
73
+ const { data } = await openmrsFetch ( restBaseUrl + `/cohortm/cohort/${ cohortUuid } ${ version ? `?v=${ version } ` : `` } ` ) ;
83
74
data . cohortMembers = data . cohortMembers . filter ( ( member ) => ! member . voided ) ;
84
75
return data ;
85
76
}
86
77
87
78
export async function getReportingCohort ( cohortUuid : string , queryParams ?: string [ ] ) {
88
79
const params = queryParams ? queryParams . join ( '&' ) : '' ;
89
- const url = params ? `reportingrest/cohort/${ cohortUuid } ?${ params } ` : `reportingrest/cohort/${ cohortUuid } ` ;
90
- const { data } = await openmrsFetch ( BASE_WS_API_URL + url ) ;
80
+ const url = params ? `/ reportingrest/cohort/${ cohortUuid } ?${ params } ` : `/ reportingrest/cohort/${ cohortUuid } ` ;
81
+ const { data } = await openmrsFetch ( restBaseUrl + url ) ;
91
82
return data ;
92
83
}
93
84
94
85
export async function getReportingCohortMembers ( cohortUuid : string , queryParams ?: string [ ] ) {
95
86
const params = queryParams ? queryParams . join ( '&' ) : '' ;
96
- const url = params ? `reportingrest/cohort/${ cohortUuid } ?${ params } ` : `reportingrest/cohort/${ cohortUuid } ` ;
97
- const { data } = await openmrsFetch ( BASE_WS_API_URL + url ) ;
87
+ const url = params ? `/ reportingrest/cohort/${ cohortUuid } ?${ params } ` : `/ reportingrest/cohort/${ cohortUuid } ` ;
88
+ const { data } = await openmrsFetch ( restBaseUrl + url ) ;
98
89
return Promise . all (
99
90
data . members . map ( ( member ) => {
100
- return openmrsFetch ( BASE_WS_API_URL + `patient/${ member . uuid } ?v=full` ) ;
91
+ return openmrsFetch ( restBaseUrl + `/ patient/${ member . uuid } ?v=full` ) ;
101
92
} ) ,
102
93
) ;
103
94
}
@@ -106,8 +97,7 @@ export async function getCohorts(cohortTypeUuid?: string) {
106
97
const {
107
98
data : { results, error } ,
108
99
} = await openmrsFetch (
109
- BASE_WS_API_URL +
110
- `cohortm/cohort?v=custom:(uuid,name,voided)${ cohortTypeUuid ? `&cohortType=${ cohortTypeUuid } ` : '' } ` ,
100
+ restBaseUrl + `/cohortm/cohort?v=custom:(uuid,name,voided)${ cohortTypeUuid ? `&cohortType=${ cohortTypeUuid } ` : '' } ` ,
111
101
) ;
112
102
if ( error ) {
113
103
throw error ;
@@ -116,7 +106,7 @@ export async function getCohorts(cohortTypeUuid?: string) {
116
106
}
117
107
118
108
export function addPatientToCohort ( patientUuid : string , cohortUuid : string ) {
119
- return openmrsFetch ( `${ BASE_WS_API_URL } cohortm/cohortmember` , {
109
+ return openmrsFetch ( `${ restBaseUrl } / cohortm/cohortmember` , {
120
110
method : 'POST' ,
121
111
headers : {
122
112
'Content-Type' : 'application/json' ,
@@ -130,13 +120,13 @@ export function addPatientToCohort(patientUuid: string, cohortUuid: string) {
130
120
}
131
121
132
122
export function evictCohortMembership ( membershipUuid : string ) {
133
- return openmrsFetch ( `${ BASE_WS_API_URL } cohortm/cohortmember/${ membershipUuid } ` , { method : 'DELETE' } ) ;
123
+ return openmrsFetch ( `${ restBaseUrl } / cohortm/cohortmember/${ membershipUuid } ` , { method : 'DELETE' } ) ;
134
124
}
135
125
136
126
export async function getPatientListsForPatient ( patientUuid : string ) {
137
127
const {
138
128
data : { results, error } ,
139
- } = await openmrsFetch ( `${ BASE_WS_API_URL } cohortm/cohortmember?patient=${ patientUuid } &v=full` ) ;
129
+ } = await openmrsFetch ( `${ restBaseUrl } / cohortm/cohortmember?patient=${ patientUuid } &v=full` ) ;
140
130
if ( error ) {
141
131
throw error ;
142
132
}
@@ -145,7 +135,7 @@ export async function getPatientListsForPatient(patientUuid: string) {
145
135
146
136
export function fetchPatientsFinalHIVStatus ( patientUUID : string ) {
147
137
return openmrsFetch (
148
- `/ws/fhir2/R4/ Observation?code=${ finalHIVCodeConcept } &value-concept=${ finalPositiveHIVValueConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
138
+ `${ fhirBaseUrl } / Observation?code=${ configSchema . obsConcepts . _default . finalHIVCodeConcept } &value-concept=${ configSchema . obsConcepts . _default . finalPositiveHIVValueConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
149
139
) . then ( ( { data } ) => {
150
140
if ( data . entry ?. length ) {
151
141
return data . entry [ 0 ] . resource . valueCodeableConcept . coding [ 0 ] . display ;
@@ -160,13 +150,13 @@ export function fetchPatientObservationFromEncounter(
160
150
observationCode : string ,
161
151
) {
162
152
return openmrsFetch (
163
- `/ws/fhir2/R4 /Observation?patient=${ patientUUID } &encounter=${ encounterUUID } &code=${ observationCode } &_sort=-date&_count=1` ,
153
+ `${ fhirBaseUrl } /Observation?patient=${ patientUUID } &encounter=${ encounterUUID } &code=${ observationCode } &_sort=-date&_count=1` ,
164
154
) ;
165
155
}
166
156
167
157
export function fetchPatientComputedConcept_HIV_Status ( patientUUID : string ) {
168
158
return openmrsFetch (
169
- `/ws/fhir2/R4/ Observation?code=${ computedHIV_StatusConcept } &value-concept=${ computedHIV_StatusConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
159
+ `${ fhirBaseUrl } / Observation?code=${ configSchema . obsConcepts . _default . computedHIV_StatusConcept } &value-concept=${ configSchema . obsConcepts . _default . computedHIV_StatusConcept } &patient=${ patientUUID } &_sort=-date&_count=1` ,
170
160
) . then ( ( { data } ) => {
171
161
if ( data . entry ?. length ) {
172
162
return data . entry [ 0 ] . resource . valueCodeableConcept . coding [ 0 ] . display ;
@@ -177,7 +167,7 @@ export function fetchPatientComputedConcept_HIV_Status(patientUUID: string) {
177
167
178
168
export function fetchPatientLastEncounter ( patientUuid : string , encounterType ) {
179
169
const query = `encounterType=${ encounterType } &patient=${ patientUuid } ` ;
180
- return openmrsFetch ( `/ws/rest/v1 /encounter?${ query } &v=${ encounterRepresentation } ` ) . then ( ( { data } ) => {
170
+ return openmrsFetch ( `${ restBaseUrl } /encounter?${ query } &v=${ encounterRepresentation } ` ) . then ( ( { data } ) => {
181
171
if ( data . results . length ) {
182
172
const sortedEncounters = data . results . sort (
183
173
( firstEncounter , secondEncounter ) =>
@@ -190,26 +180,8 @@ export function fetchPatientLastEncounter(patientUuid: string, encounterType) {
190
180
} ) ;
191
181
}
192
182
193
- export function fetchPatientCovidOutcome ( ) {
194
- return openmrsFetch ( `/ws/rest/v1/reportingrest/cohort/${ covidOutcomesCohortUUID } ` ) . then ( ( { data } ) => {
195
- if ( data . members ?. length ) {
196
- let patientRefs = data . members . map ( ( member ) => {
197
- return member . uuid ;
198
- } ) ;
199
- patientRefs = new Set ( [ ...patientRefs ] ) ;
200
- patientRefs = Array . from ( patientRefs ) ;
201
- return Promise . all (
202
- patientRefs . map ( ( ref ) => {
203
- return openmrsFetch ( BASE_FHIR_API_URL + '/Person/' + ref ) ;
204
- } ) ,
205
- ) ;
206
- }
207
- return [ ] ;
208
- } ) ;
209
- }
210
-
211
183
export function fetchConceptNameByUuid ( conceptUuid : string ) {
212
- return openmrsFetch ( `/ws/rest/v1 /concept/${ conceptUuid } /name?limit=1` ) . then ( ( { data } ) => {
184
+ return openmrsFetch ( `${ restBaseUrl } /concept/${ conceptUuid } /name?limit=1` ) . then ( ( { data } ) => {
213
185
if ( data . results . length ) {
214
186
const concept = data . results [ data . results . length - 1 ] ;
215
187
return concept . display ;
@@ -219,7 +191,7 @@ export function fetchConceptNameByUuid(conceptUuid: string) {
219
191
}
220
192
221
193
export function fetchPatientRelationships ( patientUuid : string ) {
222
- return openmrsFetch ( `${ BASE_WS_API_URL } relationship?person=${ patientUuid } &v=full` ) . then ( ( { data } ) => {
194
+ return openmrsFetch ( `${ restBaseUrl } / relationship?person=${ patientUuid } &v=full` ) . then ( ( { data } ) => {
223
195
if ( data . results . length ) {
224
196
return data . results ;
225
197
}
@@ -228,35 +200,15 @@ export function fetchPatientRelationships(patientUuid: string) {
228
200
}
229
201
230
202
export function fetchOpenMRSForms ( formNames : string [ ] ) {
231
- const fetch = ( name ) => openmrsFetch ( `/ws/rest/v1 /form?q=${ name } &v=full` ) ;
203
+ const fetch = ( name ) => openmrsFetch ( `${ restBaseUrl } /form?q=${ name } &v=full` ) ;
232
204
return Promise . all ( formNames . map ( ( name ) => fetch ( name ) ) ) ;
233
205
}
234
206
235
207
export function fetchFormsClobData ( valueReferences : string [ ] ) {
236
- const fetch = ( ref : string ) => openmrsFetch ( `/ws/rest/v1 /clobdata/${ ref } ` ) ;
208
+ const fetch = ( ref : string ) => openmrsFetch ( `${ restBaseUrl } /clobdata/${ ref } ` ) ;
237
209
return Promise . all ( valueReferences ?. map ( ( ref ) => fetch ( ref ) ) ) ;
238
210
}
239
211
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
212
export function fetchEtlData (
261
213
reportType : 'fetchMambaAncData' | 'MotherHivStatus' ,
262
214
reportId ?: string ,
@@ -285,10 +237,10 @@ export function fetchEtlData(
285
237
let endpoint = '' ;
286
238
switch ( reportType ) {
287
239
case 'fetchMambaAncData' :
288
- endpoint = `/ws/rest/v1 /mamba/report?report_id=${ reportId } &ptracker_id=${ pTrackerId } &person_uuid=${ patientUuid } ` ;
240
+ endpoint = `${ restBaseUrl } /mamba/report?report_id=${ reportId } &ptracker_id=${ pTrackerId } &person_uuid=${ patientUuid } ` ;
289
241
break ;
290
242
case 'MotherHivStatus' :
291
- endpoint = `/ws/rest/v1 /mamba/report?report_id=${ reportId } &ptracker_id=${ pTrackerId } &person_uuid=${ patientUuid } ` ;
243
+ endpoint = `${ restBaseUrl } /mamba/report?report_id=${ reportId } &ptracker_id=${ pTrackerId } &person_uuid=${ patientUuid } ` ;
292
244
break ;
293
245
default :
294
246
throw new Error ( 'Invalid report type' ) ;
@@ -323,19 +275,19 @@ export async function getCohortList(
323
275
) {
324
276
const params = queryParams ? queryParams . join ( '&' ) : '' ;
325
277
const cohortMembersUrl = params
326
- ? `reportingrest/cohort/${ cohortUuid } ?${ params } `
327
- : `reportingrest/cohort/${ cohortUuid } ` ;
328
- const cohortUrl = `cohortm/cohort/${ cohortUuid } ?v=full` ;
278
+ ? `/ reportingrest/cohort/${ cohortUuid } ?${ params } `
279
+ : `/ reportingrest/cohort/${ cohortUuid } ` ;
280
+ const cohortUrl = `/ cohortm/cohort/${ cohortUuid } ?v=full` ;
329
281
330
282
const url = isReportingCohort ? cohortMembersUrl : cohortUrl ;
331
283
332
- const { data } = await openmrsFetch ( BASE_WS_API_URL + url ) ;
284
+ const { data } = await openmrsFetch ( restBaseUrl + url ) ;
333
285
334
286
if ( data ?. members ) {
335
287
return Promise . all (
336
288
data . members . map ( ( member ) => {
337
289
return openmrsFetch (
338
- `/ws/rest/v1 /encounter?encounterType=${ encounterType } &patient=${ member . uuid } &v=${ encounterRepresentation } ` ,
290
+ `${ restBaseUrl } /encounter?encounterType=${ encounterType } &patient=${ member . uuid } &v=${ encounterRepresentation } ` ,
339
291
) . then ( ( { data } ) => {
340
292
if ( data . results . length ) {
341
293
const sortedEncounters = data . results . sort (
@@ -355,15 +307,14 @@ export async function getCohortList(
355
307
return Promise . all (
356
308
data . cohortMembers . map ( ( member ) => {
357
309
return openmrsFetch (
358
- `/ws/rest/v1 /encounter?encounterType=${ encounterType } &patient=${ member . patient . uuid } &v=${ encounterRepresentation } ` ,
310
+ `${ restBaseUrl } /encounter?encounterType=${ encounterType } &patient=${ member . patient . uuid } &v=${ encounterRepresentation } ` ,
359
311
) . then ( ( { data } ) => {
360
312
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
- ) ;
313
+ const sortedEncounters = data . results . sort (
314
+ ( firstEncounter , secondEncounter ) =>
315
+ new Date ( secondEncounter . encounterDatetime ) . getTime ( ) -
316
+ new Date ( firstEncounter . encounterDatetime ) . getTime ( ) ,
317
+ ) ;
367
318
return sortedEncounters [ 0 ] ;
368
319
}
369
320
return null ;
@@ -372,3 +323,16 @@ export async function getCohortList(
372
323
) ;
373
324
}
374
325
}
326
+
327
+ export function useMambaReportData ( reportId : string ) {
328
+ const { data, error, isLoading } = useSWR < { data : { results : ReportDataArray } } , Error > (
329
+ `${ restBaseUrl } /mamba/report?report_id=${ reportId } ` ,
330
+ openmrsFetch ,
331
+ ) ;
332
+
333
+ return {
334
+ data : data ?. data ?. results [ 0 ] ?. record [ 0 ] ?. value ?? 0 ,
335
+ error,
336
+ isLoading,
337
+ } ;
338
+ }
0 commit comments