Skip to content

Commit

Permalink
(fix) Updates sort params and switches to restBaseUrl (#218)
Browse files Browse the repository at this point in the history
* (fix) Updates sort params and switches to restBaseUrl

* fixes typo
  • Loading branch information
pirupius authored Apr 25, 2024
1 parent bc125ee commit 348be86
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 43 deletions.
38 changes: 18 additions & 20 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
}
Expand All @@ -98,8 +96,8 @@ export async function fetchOpenMRSForm(nameOrUUID: string): Promise<OpenmrsForm
}

const { url, isUUID } = isUuid(nameOrUUID)
? { url: `/ws/rest/v1/form/${nameOrUUID}?v=full`, isUUID: true }
: { url: `/ws/rest/v1/form?q=${nameOrUUID}&v=full`, isUUID: false };
? { url: `${restBaseUrl}/form/${nameOrUUID}?v=full`, isUUID: true }
: { url: `${restBaseUrl}/form?q=${nameOrUUID}&v=full`, isUUID: false };

const { data: openmrsFormResponse } = await openmrsFetch(url);
if (isUUID) {
Expand All @@ -125,7 +123,7 @@ export async function fetchClobdata(form: OpenmrsForm): Promise<any | null> {
return null;
}

const clobDataUrl = `/ws/rest/v1/clobdata/${jsonSchemaResource.valueReference}`;
const clobDataUrl = `${restBaseUrl}/clobdata/${jsonSchemaResource.valueReference}`;
const { data: clobDataResponse } = await openmrsFetch(clobDataUrl);

return clobDataResponse;
Expand All @@ -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;
Expand All @@ -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',
Expand All @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions src/form-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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 });
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useClobData.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
);

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useConcepts.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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)))';

export function useConcepts(references: Set<string>) {
// TODO: handle paging (ie when number of concepts greater than default limit per page)
const { data, error, isLoading } = useSWRImmutable<{ data: { results: Array<OpenmrsResource> } }, 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 };
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useEncounter.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useEncounterRole.tsx
Original file line number Diff line number Diff line change
@@ -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<OpenmrsResource> } }, 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');
Expand Down
4 changes: 2 additions & 2 deletions src/utils/common-utils.ts
Original file line number Diff line number Diff line change
@@ -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[] {
Expand All @@ -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`,
Expand Down
6 changes: 3 additions & 3 deletions src/zscore-tests/bmi-age.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 3 additions & 3 deletions src/zscore-tests/height-age.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 3 additions & 3 deletions src/zscore-tests/weight-height.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 348be86

Please sign in to comment.