Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/datasources/location-data-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { vi, it, describe, beforeEach, expect } from 'vitest';
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { LocationDataSource } from './location-data-source';

const mockOpenmrsFetch = vi.mocked(openmrsFetch);

const locations = (from: number, count: number) =>
Array.from({ length: count }, (_, i) => ({ uuid: `uuid-${from + i}`, display: `Clinic ${from + i}` }));

const backendNextUri = 'https://backend:8443/openmrs/ws/rest/v1/location?tag=Visit+Location&startIndex=50';
const proxiedNextUri = `${window.location.protocol}//${window.location.host}/openmrs/ws/rest/v1/location?tag=Visit+Location&startIndex=50`;

describe('LocationDataSource', () => {
let ds: LocationDataSource;

beforeEach(() => {
mockOpenmrsFetch.mockReset();
ds = new LocationDataSource();
});

describe('fetchData', () => {
it('returns every tagged location when the server pages the results', async () => {
mockOpenmrsFetch
.mockResolvedValueOnce({
data: { results: locations(0, 50), links: [{ rel: 'next', uri: backendNextUri }] },
} as any)
.mockResolvedValueOnce({ data: { results: locations(50, 7), links: [{ rel: 'prev', uri: '' }] } } as any);

const result = await ds.fetchData(null, { tag: 'Visit Location' });

expect(result).toEqual(locations(0, 57));
expect(mockOpenmrsFetch).toHaveBeenCalledTimes(2);
expect(mockOpenmrsFetch).toHaveBeenNthCalledWith(
1,
`${restBaseUrl}/location?tag=Visit Location&v=custom:(uuid,display)`,
);
expect(mockOpenmrsFetch).toHaveBeenNthCalledWith(2, proxiedNextUri);
});

it('returns a single page as it is when there is no next link', async () => {
mockOpenmrsFetch.mockResolvedValueOnce({ data: { results: locations(0, 3) } } as any);

const result = await ds.fetchData('Clinic');

expect(result).toEqual(locations(0, 3));
expect(mockOpenmrsFetch).toHaveBeenCalledTimes(1);
expect(mockOpenmrsFetch).toHaveBeenCalledWith(`${restBaseUrl}/location?v=custom:(uuid,display)&q=Clinic`);
});
});
});
36 changes: 30 additions & 6 deletions src/datasources/location-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,37 @@ export class LocationDataSource extends BaseOpenMRSDataSource {
//overwrite url if there's a uuid value, meaning we are in edit mode
if (uuid) {
apiUrl = `${urlParts[0]}/${uuid}?${urlParts[1]}`;
return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
if (data.results) {
return data.results;
}
return data;
});
}

return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
if (data.results) {
return data.results;
}
return data;
});
return fetchAllPages(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl);
}
}

async function fetchAllPages(url: string): Promise<Array<any>> {
const results = [];
let nextUrl: string | null = url;
while (nextUrl) {
const { data } = await openmrsFetch(nextUrl);
results.push(...(data.results ?? []));
nextUrl = getNextUrl(data);
}
return results;
}

// A copy of the framework's unexported getNextUrl: keeps next links on this origin so proxies work.
function getNextUrl(response): string | null {
const uri = response?.links?.find((link) => link.rel === 'next')?.uri;
if (!uri) {
return null;
}
const url = new URL(uri);
url.host = window.location.host;
url.protocol = window.location.protocol;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Next links keep backend ports. Assigning url.host does not clear the old port when window.location.host has no port. For a page at https://clinic.example, the next link used in the test becomes https://clinic.example:8443/..., not the browser's origin. The second request bypasses the intended proxy and can fail, leaving the dropdown empty. Explicitly copy window.location.port, including its empty value, after changing the protocol.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/datasources/location-data-source.ts
Line: 48-49

Comment:
**Next links keep backend ports.** Assigning `url.host` does not clear the old port when `window.location.host` has no port. For a page at `https://clinic.example`, the next link used in the test becomes `https://clinic.example:8443/...`, not the browser's origin. The second request bypasses the intended proxy and can fail, leaving the dropdown empty. Explicitly copy `window.location.port`, including its empty value, after changing the protocol.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in 63c2189: the next link's path and query are now resolved against window.location.origin, so the backend's host, port and scheme are all dropped, and a new test on a port-less https://clinic.example page covers it.

return url.toString();
}
Loading