This repository was archived by the owner on Mar 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpatient-queues.resource.ts
More file actions
373 lines (331 loc) · 9.24 KB
/
patient-queues.resource.ts
File metadata and controls
373 lines (331 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import dayjs from 'dayjs';
import useSWR from 'swr';
import { openmrsFetch, restBaseUrl, usePagination } from '@openmrs/esm-framework';
import { PatientQueue } from '../types/patient-queues';
import { NewVisitPayload, ProviderResponse } from '../types';
import { useEffect, useState } from 'react';
import last from 'lodash-es/last';
import { PageableResult, ResourceFilterCriteria, ResourceRepresentation, toQueryParams } from '../helpers/functions';
export interface PatientQueueFilter extends ResourceFilterCriteria {
status?: string;
parentLocation?: string;
room?: string;
}
export interface NewQueuePayload {
patient: string;
provider: string;
locationFrom: string;
locationTo: string;
status: string;
priority: number;
priorityComment: string;
comment: string;
queueRoom: string;
}
export interface NewCheckInPayload {
patient: string;
provider: string;
currentLocation: string;
locationTo: string;
patientStatus: string;
priority: number;
priorityComment: string;
visitComment: string;
queueRoom: string;
visitType: string;
}
export interface LocationResponse {
uuid: string;
display: string;
name: string;
description: any;
address1: any;
address2: any;
cityVillage: any;
stateProvince: any;
country: any;
postalCode: any;
latitude: any;
longitude: any;
countyDistrict: any;
address3: any;
address4: any;
address5: any;
address6: any;
tags: Tag[];
parentLocation: ParentLocation;
childLocations: ChildLocation[];
retired: boolean;
attributes: any[];
address7: any;
address8: any;
address9: any;
address10: any;
address11: any;
address12: any;
address13: any;
address14: any;
address15: any;
links: Link[];
resourceVersion: string;
}
export interface Tag {
uuid: string;
display: string;
links: Link[];
}
export interface Link {
rel: string;
uri: string;
resourceAlias: string;
}
export interface ParentLocation {
uuid: string;
display: string;
links: Link[];
}
export interface ChildLocation {
uuid: string;
display: string;
links: Link[];
}
// get parentlocation
export function useParentLocation(currentQueueLocationUuid: string) {
const apiUrl = `${restBaseUrl}/location/${currentQueueLocationUuid}`;
const { data, error, isLoading, isValidating, mutate } = useSWR<{ data: LocationResponse }, Error>(
apiUrl,
openmrsFetch,
);
return {
location: data?.data,
isLoading,
isError: error,
isValidating,
mutate,
};
}
export function useChildLocations(parentUuid: string) {
const apiUrl = `${restBaseUrl}/location/${parentUuid}`;
const { data, error, isLoading, isValidating, mutate } = useSWR<{ data: LocationResponse }, Error>(
apiUrl,
openmrsFetch,
);
return {
location: data?.data,
isLoading,
isError: error,
isValidating,
mutate,
};
}
// Fetch providers of a service point
export function useProviders(selectedNextQueueLocation: string) {
const apiUrl = `${restBaseUrl}/provider?q=&v=full`;
const { data, error, isLoading, isValidating, mutate } = useSWR<
{ data: { results: Array<ProviderResponse> } },
Error
>(apiUrl, openmrsFetch);
// Filter providers based on the selected location
const providers =
data?.data?.results?.filter((provider) =>
provider.attributes.some(
(attr) =>
attr.attributeType.display === 'Default Location' &&
typeof attr.value === 'object' &&
attr.value.uuid === selectedNextQueueLocation,
),
) || [];
return {
providers,
error,
isLoading,
isError: Boolean(error),
isValidating,
mutate,
};
}
export async function getCurrentPatientQueueByPatientUuid(patientUuid: string, currentLocation: string) {
const apiUrl = `${restBaseUrl}/incompletequeue?queueRoom=${currentLocation}&patient=${patientUuid}&v=full`;
const abortController = new AbortController();
return await openmrsFetch(apiUrl, {
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
});
}
// create visit
export async function createVisit(payload: NewVisitPayload) {
const abortController = new AbortController();
return await openmrsFetch(`${restBaseUrl}/visit`, {
method: 'POST',
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
body: payload,
});
}
// update Visit
export async function updateVisit(uuid: string, payload: NewVisitPayload) {
const abortController = new AbortController();
return await openmrsFetch(`${restBaseUrl}/visit/${uuid}`, {
method: 'POST',
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
body: payload,
});
}
// get current visit
export async function getCurrentVisit(patient: string, date: string) {
const apiUrl = `${restBaseUrl}/visit?patient=${patient}&includeInactive=false&fromStartDate=${date}&v=default&limit=1`;
const abortController = new AbortController();
return await openmrsFetch(apiUrl, {
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
});
}
export async function checkCurrentVisit(patientUuid) {
const date = dayjs().format('YYYY-MM-DD');
const resp = await getCurrentVisit(patientUuid, date);
return resp.data?.results !== null && resp.data?.results.length > 0;
}
export function usePatientQueues(filter: PatientQueueFilter) {
const apiUrl = `${restBaseUrl}/patientqueue${toQueryParams(filter)}`;
const { data, error, isLoading } = useSWR<
{
data: PageableResult<PatientQueue>;
},
Error
>(apiUrl, openmrsFetch);
return {
items: data?.data || <PageableResult<PatientQueue>>{},
isLoading,
error,
};
}
export function usePatientQueuePages(
currentLocation: string,
currentStatus: string,
isToggled?: boolean,
isClinical?: boolean,
) {
const [patientQueueFilter, setPatientQueueFilter] = useState<PatientQueueFilter>({
v: ResourceRepresentation.Full,
totalCount: true,
parentLocation: isToggled && !isClinical ? currentLocation : '',
status: isToggled ? currentStatus : '',
room: !isToggled ? currentLocation : '',
});
const pageSizes = [10, 20, 30, 40, 50];
const [currentPageSize, setPageSize] = useState(10);
const { items, isLoading, error } = usePatientQueues(patientQueueFilter);
const { goTo, results: paginatedItems, currentPage } = usePagination(items.results, currentPageSize);
useEffect(() => {
setPatientQueueFilter({
v: ResourceRepresentation.Full,
totalCount: true,
parentLocation: isToggled && !isClinical ? currentLocation : '',
status: isToggled ? currentStatus : '',
room: !isToggled ? currentLocation : '',
});
}, [currentPage, currentPageSize, currentLocation, currentStatus, isToggled, isClinical]);
return {
items: paginatedItems,
totalCount: items.totalCount,
currentPageSize,
currentPage,
setPageSize,
goTo,
pageSizes,
isLoading,
error,
};
}
export const getOriginFromPathName = (pathname = '') => {
const from = pathname.split('/');
return last(from);
};
export async function updateQueueEntry(
status: string,
providerUuid: string,
queueUuid: string,
priority: number,
priorityComment: string,
comment: string,
) {
const abortController = new AbortController();
return await openmrsFetch(`${restBaseUrl}/patientqueue/${queueUuid}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
body: {
provider: {
uuid: providerUuid,
},
status: status,
priority: priority ? priority : 0,
priorityComment: priorityComment === 'Urgent' ? 'Priority' : priorityComment,
comment: comment,
},
});
}
export async function addQueueEntry(payload: NewQueuePayload) {
const abortController = new AbortController();
return await openmrsFetch(`${restBaseUrl}/patientqueue`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
body: payload,
});
}
export async function checkInQueue(payload: NewCheckInPayload) {
const abortController = new AbortController();
return await openmrsFetch(`${restBaseUrl}/checkinpatient`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
body: payload,
});
}
export function generateVisitQueueNumber(location: string, patient: string) {
const abortController = new AbortController();
return openmrsFetch(`${restBaseUrl}/queuenumber?patient=${patient}&location=${location}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
});
}
export function getCareProvider(provider: string) {
const abortController = new AbortController();
return openmrsFetch(`${restBaseUrl}/provider?user=${provider}&v=full`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
});
}
export function getLocationByUuid(uuid: string) {
const abortController = new AbortController();
const url = `${restBaseUrl}/location/${uuid}`;
return openmrsFetch(url, {
method: 'GET',
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
});
}