From 348be863b93adfb7442fa2c3ddf9b822f00f8b57 Mon Sep 17 00:00:00 2001 From: Pius Rubangakene Date: Thu, 25 Apr 2024 11:26:48 +0300 Subject: [PATCH] (fix) Updates sort params and switches to restBaseUrl (#218) * (fix) Updates sort params and switches to restBaseUrl * fixes typo --- src/api/api.ts | 38 ++++++++++++------------- src/form-engine.test.tsx | 8 +++--- src/hooks/useClobData.tsx | 4 +-- src/hooks/useConcepts.tsx | 4 +-- src/hooks/useEncounter.tsx | 4 +-- src/hooks/useEncounterRole.tsx | 4 +-- src/utils/common-utils.ts | 4 +-- src/zscore-tests/bmi-age.test.tsx | 6 ++-- src/zscore-tests/height-age.test.tsx | 6 ++-- src/zscore-tests/weight-height.test.tsx | 6 ++-- 10 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index e91fc7c9e..fa33546bd 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,12 +1,10 @@ -import { openmrsFetch } from '@openmrs/esm-framework'; +import { fhirBaseUrl, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import { encounterRepresentation } from '../constants'; import { OpenmrsForm, ProgramEnrollmentPayload } from '../types'; import { isUuid } from '../utils/boolean-utils'; -const BASE_WS_API_URL = '/ws/rest/v1/'; - export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) { - const url = encounterUuid ? `/ws/rest/v1/encounter/${encounterUuid}?v=full` : `/ws/rest/v1/encounter?v=full`; + const url = encounterUuid ? `${restBaseUrl}/encounter/${encounterUuid}?v=full` : `${restBaseUrl}/encounter?v=full`; return openmrsFetch(url, { headers: { @@ -19,7 +17,7 @@ export function saveEncounter(abortController: AbortController, payload, encount } export function saveAttachment(patientUuid, field, conceptUuid, date, encounterUUID, abortController) { - const url = '/ws/rest/v1/attachment'; + const url = `${restBaseUrl}/attachment`; const content = field?.value.value; const cameraUploadType = typeof content === 'string' && content?.split(';')[0].split(':')[1].split('/')[1]; @@ -47,30 +45,30 @@ export function saveAttachment(patientUuid, field, conceptUuid, date, encounterU } export function getAttachmentByUuid(patientUuid: string, encounterUuid: string, abortController: AbortController) { - const attachmentUrl = '/ws/rest/v1/attachment'; + const attachmentUrl = `${restBaseUrl}/attachment`; return openmrsFetch(`${attachmentUrl}?patient=${patientUuid}&encounter=${encounterUuid}`, { signal: abortController.signal, }).then((response) => response.data); } export function getConcept(conceptUuid: string, v: string) { - return openmrsFetch(`/ws/rest/v1/concept/${conceptUuid}?v=${v}`).then(({ data }) => data.results); + return openmrsFetch(`${restBaseUrl}/concept/${conceptUuid}?v=${v}`).then(({ data }) => data.results); } export function getLocationsByTag(tag: string) { - return openmrsFetch(`/ws/rest/v1/location?tag=${tag}&v=custom:(uuid,display)`).then(({ data }) => data.results); + return openmrsFetch(`${restBaseUrl}/location?tag=${tag}&v=custom:(uuid,display)`).then(({ data }) => data.results); } export function getAllLocations() { - return openmrsFetch<{ results }>(`/ws/rest/v1/location?v=custom:(uuid,display)`).then(({ data }) => data.results); + return openmrsFetch<{ results }>(`${restBaseUrl}/location?v=custom:(uuid,display)`).then(({ data }) => data.results); } export async function getPreviousEncounter(patientUuid: string, encounterType: string) { - const query = `patient=${patientUuid}&_sort=-_lastUpdated&_count=1&type=${encounterType}`; - let response = await openmrsFetch(`/ws/fhir2/R4/Encounter?${query}`); + const query = `patient=${patientUuid}&_sort=-date&_count=1&type=${encounterType}`; + let response = await openmrsFetch(`${fhirBaseUrl}/Encounter?${query}`); if (response?.data?.entry?.length) { const latestEncounter = response.data.entry[0].resource.id; - response = await openmrsFetch(`/ws/rest/v1/encounter/${latestEncounter}?v=${encounterRepresentation}`); + response = await openmrsFetch(`${restBaseUrl}/encounter/${latestEncounter}?v=${encounterRepresentation}`); return response.data; } return null; @@ -81,8 +79,8 @@ export function getLatestObs(patientUuid: string, conceptUuid: string, encounter encounterTypeUuid ? `&encounter.type=${encounterTypeUuid}` : '' }`; // the latest obs - params += '&_sort=-_lastUpdated&_count=1'; - return openmrsFetch(`/ws/fhir2/R4/Observation?${params}`).then(({ data }) => { + params += '&_sort=-date&_count=1'; + return openmrsFetch(`${fhirBaseUrl}/Observation?${params}`).then(({ data }) => { return data.entry?.length ? data.entry[0].resource : null; }); } @@ -98,8 +96,8 @@ export async function fetchOpenMRSForm(nameOrUUID: string): Promise { return null; } - const clobDataUrl = `/ws/rest/v1/clobdata/${jsonSchemaResource.valueReference}`; + const clobDataUrl = `${restBaseUrl}/clobdata/${jsonSchemaResource.valueReference}`; const { data: clobDataResponse } = await openmrsFetch(clobDataUrl); return clobDataResponse; @@ -149,7 +147,7 @@ function dataURItoFile(dataURI: string) { //Program Enrollment export function getPatientEnrolledPrograms(patientUuid: string) { return openmrsFetch( - `${BASE_WS_API_URL}programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`, + `${restBaseUrl}/programenrollment?patient=${patientUuid}&v=custom:(uuid,display,program,dateEnrolled,dateCompleted,location:(uuid,display))`, ).then(({ data }) => { if (data) { return data; @@ -163,7 +161,7 @@ export function createProgramEnrollment(payload: ProgramEnrollmentPayload, abort throw new Error('Program enrollment cannot be created because no payload is supplied'); } const { program, patient, dateEnrolled, dateCompleted, location } = payload; - return openmrsFetch(`${BASE_WS_API_URL}programenrollment`, { + return openmrsFetch(`${restBaseUrl}/programenrollment`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -182,7 +180,7 @@ export function updateProgramEnrollment( throw new Error('Program enrollment cannot be edited without a payload or a program Uuid'); } const { dateEnrolled, dateCompleted, location } = payload; - return openmrsFetch(`${BASE_WS_API_URL}programenrollment/${programEnrollmentUuid}`, { + return openmrsFetch(`${restBaseUrl}/programenrollment/${programEnrollmentUuid}`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/form-engine.test.tsx b/src/form-engine.test.tsx index 98cc2b63b..8b0ea5f07 100644 --- a/src/form-engine.test.tsx +++ b/src/form-engine.test.tsx @@ -10,7 +10,7 @@ import { waitForElementToBeRemoved, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { showToast } from '@openmrs/esm-framework'; +import { restBaseUrl, showToast } from '@openmrs/esm-framework'; import { when } from 'jest-when'; import dayjs from 'dayjs'; import * as api from '../src/api/api'; @@ -49,8 +49,8 @@ const mockShowToast = showToast as jest.Mock; const patientUUID = '8673ee4f-e2ab-4077-ba55-4980f408773e'; const visit = mockVisit; const mockOpenmrsFetch = jest.fn(); -const formsResourcePath = when((url: string) => url.includes('/ws/rest/v1/form/')); -const clobdataResourcePath = when((url: string) => url.includes('/ws/rest/v1/clobdata/')); +const formsResourcePath = when((url: string) => url.includes(`${restBaseUrl}/form/`)); +const clobdataResourcePath = when((url: string) => url.includes(`${restBaseUrl}/clobdata/`)); global.ResizeObserver = require('resize-observer-polyfill'); when(mockOpenmrsFetch).calledWith(formsResourcePath).mockReturnValue({ data: demoHtsOpenmrsForm }); @@ -544,7 +544,7 @@ describe('Form component', () => { describe('Concept references', () => { const conceptResourcePath = when((url: string) => - url.includes('/ws/rest/v1/concept?references=PIH:Occurrence of trauma,PIH:Yes,PIH:No,PIH:COUGH'), + url.includes(`${restBaseUrl}/concept?references=PIH:Occurrence of trauma,PIH:Yes,PIH:No,PIH:COUGH`), ); when(mockOpenmrsFetch).calledWith(conceptResourcePath).mockReturnValue({ data: mockConceptsForm }); diff --git a/src/hooks/useClobData.tsx b/src/hooks/useClobData.tsx index 5d9bbedc5..67706d43a 100644 --- a/src/hooks/useClobData.tsx +++ b/src/hooks/useClobData.tsx @@ -1,4 +1,4 @@ -import { openmrsFetch } from '@openmrs/esm-framework'; +import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import { useMemo } from 'react'; import { FormSchema, OpenmrsForm } from '../types'; import useSWRImmutable from 'swr/immutable'; @@ -9,7 +9,7 @@ export function useClobdata(form: OpenmrsForm) { [form], ); const { data, error } = useSWRImmutable<{ data: FormSchema }, Error>( - valueReferenceUuid ? `/ws/rest/v1/clobdata/${valueReferenceUuid}` : null, + valueReferenceUuid ? `${restBaseUrl}/clobdata/${valueReferenceUuid}` : null, openmrsFetch, ); diff --git a/src/hooks/useConcepts.tsx b/src/hooks/useConcepts.tsx index a334b65b1..286934903 100644 --- a/src/hooks/useConcepts.tsx +++ b/src/hooks/useConcepts.tsx @@ -1,5 +1,5 @@ import useSWRImmutable from 'swr/immutable'; -import { openmrsFetch, OpenmrsResource } from '@openmrs/esm-framework'; +import { openmrsFetch, OpenmrsResource, restBaseUrl } from '@openmrs/esm-framework'; const conceptRepresentation = 'custom:(uuid,display,conceptClass:(uuid,display),answers:(uuid,display),conceptMappings:(conceptReferenceTerm:(conceptSource:(name),code)))'; @@ -7,7 +7,7 @@ const conceptRepresentation = export function useConcepts(references: Set) { // TODO: handle paging (ie when number of concepts greater than default limit per page) const { data, error, isLoading } = useSWRImmutable<{ data: { results: Array } }, Error>( - `/ws/rest/v1/concept?references=${Array.from(references).join(',')}&v=${conceptRepresentation}`, + `${restBaseUrl}/concept?references=${Array.from(references).join(',')}&v=${conceptRepresentation}`, openmrsFetch, ); return { concepts: data?.data.results, error, isLoading }; diff --git a/src/hooks/useEncounter.tsx b/src/hooks/useEncounter.tsx index 7637dde7f..30c91303e 100644 --- a/src/hooks/useEncounter.tsx +++ b/src/hooks/useEncounter.tsx @@ -1,4 +1,4 @@ -import { openmrsFetch } from '@openmrs/esm-framework'; +import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import useSWR from 'swr'; import { FormSchema, OpenmrsEncounter } from '../types'; import { encounterRepresentation } from '../constants'; @@ -8,7 +8,7 @@ export function useEncounter(formJson: FormSchema) { const encounterObjectCache = getEncounterObjIfPresent(formJson.encounter); const url = encObjectOrUuid && !encounterObjectCache - ? `/ws/rest/v1/encounter/${encObjectOrUuid}?v=${encounterRepresentation}` + ? `${restBaseUrl}/encounter/${encObjectOrUuid}?v=${encounterRepresentation}` : null; const { data, error } = useSWR<{ data: OpenmrsEncounter }, Error>(url, openmrsFetch); diff --git a/src/hooks/useEncounterRole.tsx b/src/hooks/useEncounterRole.tsx index 85f788dad..ff5db2426 100644 --- a/src/hooks/useEncounterRole.tsx +++ b/src/hooks/useEncounterRole.tsx @@ -1,9 +1,9 @@ -import { OpenmrsResource, openmrsFetch } from '@openmrs/esm-framework'; +import { OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import useSWRImmutable from 'swr/immutable'; export function useEncounterRole() { const { data, error, isLoading } = useSWRImmutable<{ data: { results: Array } }, Error>( - '/ws/rest/v1/encounterrole?v=custom:(uuid,display,name)', + `${restBaseUrl}/encounterrole?v=custom:(uuid,display,name)`, openmrsFetch, ); const clinicalEncounterRole = data?.data.results.find((encounterRole) => encounterRole.name === 'Clinician'); diff --git a/src/utils/common-utils.ts b/src/utils/common-utils.ts index 9237a8d79..fde8d643e 100644 --- a/src/utils/common-utils.ts +++ b/src/utils/common-utils.ts @@ -1,4 +1,4 @@ -import { formatDate } from '@openmrs/esm-framework'; +import { formatDate, restBaseUrl } from '@openmrs/esm-framework'; import { Attachment, AttachmentResponse, FormField, OpenmrsObs, RenderType } from '../types'; export function flattenObsList(obsList: OpenmrsObs[]): OpenmrsObs[] { @@ -25,7 +25,7 @@ export function hasRendering(field: FormField, rendering: RenderType) { } export function createAttachment(data: AttachmentResponse): Attachment { - const attachmentUrl = '/ws/rest/v1/attachment'; + const attachmentUrl = `${restBaseUrl}/attachment`; return { id: data.uuid, src: `${window.openmrsBase}${attachmentUrl}/${data.uuid}/bytes`, diff --git a/src/zscore-tests/bmi-age.test.tsx b/src/zscore-tests/bmi-age.test.tsx index efc4a6ccb..bb4c9243c 100644 --- a/src/zscore-tests/bmi-age.test.tsx +++ b/src/zscore-tests/bmi-age.test.tsx @@ -10,16 +10,16 @@ import demoHtsForm from '../../__mocks__/forms/rfe-forms/demo_hts-form.json'; import { findNumberInput } from '../utils/test-utils'; import { mockVisit } from '../../__mocks__/visit.mock'; +import { restBaseUrl } from '@openmrs/esm-framework'; ////////////////////////////////////////// ////// Base setup ////////////////////////////////////////// -const mockUrl = `/ws/rest/v1/encounter?v=full`; const patientUUID = 'e13a8696-dc58-4b8c-ae40-2a1e7dd843e7'; const visit = mockVisit; const mockOpenmrsFetch = jest.fn(); -const formsResourcePath = when((url: string) => url.includes('/ws/rest/v1/form/')); -const clobdataResourcePath = when((url: string) => url.includes('/ws/rest/v1/clobdata/')); +const formsResourcePath = when((url: string) => url.includes(`${restBaseUrl}/form/`)); +const clobdataResourcePath = when((url: string) => url.includes(`${restBaseUrl}/clobdata/`)); global.ResizeObserver = require('resize-observer-polyfill'); when(mockOpenmrsFetch).calledWith(formsResourcePath).mockReturnValue({ data: demoHtsOpenmrsForm }); when(mockOpenmrsFetch).calledWith(clobdataResourcePath).mockReturnValue({ data: demoHtsForm }); diff --git a/src/zscore-tests/height-age.test.tsx b/src/zscore-tests/height-age.test.tsx index 1be3fec66..6c3ab0806 100644 --- a/src/zscore-tests/height-age.test.tsx +++ b/src/zscore-tests/height-age.test.tsx @@ -10,16 +10,16 @@ import demoHtsForm from '../../__mocks__/forms/rfe-forms/demo_hts-form.json'; import { findNumberInput } from '../utils/test-utils'; import { mockVisit } from '../../__mocks__/visit.mock'; +import { restBaseUrl } from '@openmrs/esm-framework'; ////////////////////////////////////////// ////// Base setup ////////////////////////////////////////// -const mockUrl = `/ws/rest/v1/encounter?v=full`; const patientUUID = 'e13a8696-dc58-4b8c-ae40-2a1e7dd843e7'; const visit = mockVisit; const mockOpenmrsFetch = jest.fn(); -const formsResourcePath = when((url: string) => url.includes('/ws/rest/v1/form/')); -const clobdataResourcePath = when((url: string) => url.includes('/ws/rest/v1/clobdata/')); +const formsResourcePath = when((url: string) => url.includes(`${restBaseUrl}/form/`)); +const clobdataResourcePath = when((url: string) => url.includes(`${restBaseUrl}/clobdata/`)); global.ResizeObserver = require('resize-observer-polyfill'); when(mockOpenmrsFetch).calledWith(formsResourcePath).mockReturnValue({ data: demoHtsOpenmrsForm }); when(mockOpenmrsFetch).calledWith(clobdataResourcePath).mockReturnValue({ data: demoHtsForm }); diff --git a/src/zscore-tests/weight-height.test.tsx b/src/zscore-tests/weight-height.test.tsx index fa7bb925f..842c882ca 100644 --- a/src/zscore-tests/weight-height.test.tsx +++ b/src/zscore-tests/weight-height.test.tsx @@ -10,16 +10,16 @@ import demoHtsForm from '../../__mocks__/forms/rfe-forms/demo_hts-form.json'; import { findNumberInput, findTextOrDateInput } from '../utils/test-utils'; import { mockVisit } from '../../__mocks__/visit.mock'; +import { restBaseUrl } from '@openmrs/esm-framework'; ////////////////////////////////////////// ////// Base setup ////////////////////////////////////////// -const mockUrl = `/ws/rest/v1/encounter?v=full`; const patientUUID = '8673ee4f-e2ab-4077-ba55-4980f408773e'; const visit = mockVisit; const mockOpenmrsFetch = jest.fn(); -const formsResourcePath = when((url: string) => url.includes('/ws/rest/v1/form/')); -const clobdataResourcePath = when((url: string) => url.includes('/ws/rest/v1/clobdata/')); +const formsResourcePath = when((url: string) => url.includes(`${restBaseUrl}/form/`)); +const clobdataResourcePath = when((url: string) => url.includes(`${restBaseUrl}/clobdata/`)); global.ResizeObserver = require('resize-observer-polyfill'); when(mockOpenmrsFetch).calledWith(formsResourcePath).mockReturnValue({ data: demoHtsOpenmrsForm }); when(mockOpenmrsFetch).calledWith(clobdataResourcePath).mockReturnValue({ data: demoHtsForm });