Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenMRS Add pagination #741

Merged
merged 13 commits into from
Sep 11, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/wicked-carrots-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/language-openmrs': minor
---

- Add pagination support on request helper function
4 changes: 0 additions & 4 deletions packages/openmrs/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"docs": {
"description": "Gets patient matching a uuid",
"tags": [
{
"title": "example",
"description": "getPatient(\"123\")"
},
{
"title": "function",
"description": null,
Expand Down
13 changes: 6 additions & 7 deletions packages/openmrs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "2.0.1"
"@openfn/language-common": "workspace:*"
},
"devDependencies": {
"@openfn/simple-ast": "^0.4.1",
"assertion-error": "^1.0.1",
"chai": "^3.4.0",
"assertion-error": "^1.1.0",
"chai": "^5.1.1",
"deep-eql": "^0.1.3",
"esno": "^0.16.3",
"mocha": "^10.1.0",
"mocha": "^10.7.3",
"nock": "^12.0.3",
"rimraf": "^3.0.2",
"sinon": "^1.17.2"
"rimraf": "^3.0.2"
},
"type": "module",
"exports": {
Expand All @@ -48,4 +47,4 @@
"./package.json": "./package.json"
},
"types": "types/index.d.ts"
}
}
8 changes: 2 additions & 6 deletions packages/openmrs/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export function execute(...operations) {

/**
* Gets patient matching a uuid
* @example
* getPatient("123")
* @function
* @public
* @param {string} uuid - A uuid for the patient
Expand Down Expand Up @@ -175,7 +173,7 @@ export function post(path, data, callback = s => s) {
*/
export function searchPatient(query, callback = s => s) {
return async state => {
const [resolvedQuery = {}] = expandReferences(state, query);
const [resolvedQuery] = expandReferences(state, query);

console.log('Searching for patient with query:', resolvedQuery);

Expand All @@ -187,8 +185,6 @@ export function searchPatient(query, callback = s => s) {
resolvedQuery
);

console.log(`Found ${response.body.results.length} patients`);

return prepareNextState(state, response, callback);
};
}
Expand Down Expand Up @@ -324,8 +320,8 @@ export function getEncounters(query, callback = s => s) {
{},
resolvedQuery
);
console.log(`Found ${response.body.results.length} results`);

console.log(`Found ${response.body.results.length}} results`);
return prepareNextState(state, response, callback);
};
}
Expand Down
43 changes: 36 additions & 7 deletions packages/openmrs/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { composeNextState } from '@openfn/language-common';
import {
request as commonRequest,
makeBasicAuthHeader,
logResponse
logResponse,
} from '@openfn/language-common/util';

export const prepareNextState = (state, response, callback) => {
const { body, ...responseWithoutBody } = response
const { body, ...responseWithoutBody } = response;
const nextState = {
...composeNextState(state, response.body),
response: responseWithoutBody,
Expand All @@ -15,24 +15,53 @@ export const prepareNextState = (state, response, callback) => {
return callback(nextState);
};

export function request(state, method, path, data, params) {
export async function request(state, method, path, data, params) {
const { instanceUrl, username, password } = state.configuration;
const headers = makeBasicAuthHeader(username, password);

const options = {
body: data,

headers: {
...headers,
'content-type': 'application/json',
},

query: params,

parseAs: 'json',
};

const url = `${instanceUrl}${path}`;

return commonRequest(method, url, options).then(response => logResponse(response));
let allResponses;
let query = options?.query;
let allowPagination = isNaN(query?.startIndex);

do {
const requestOptions = query ? { ...options, query } : options;
const response = await commonRequest(method, url, requestOptions);
logResponse(response);

if (allResponses) {
allResponses.body.results.push(...response.body.results);
} else {
allResponses = response;
}
const nextUrl = response?.body?.links?.find(
link => link.rel === 'next'
)?.uri;

if (nextUrl) {
console.log(`Fetched ${response.body.results.length} results`);
console.log(`Fetching next page from ${nextUrl}`);
const urlObj = new URL(nextUrl);
const params = new URLSearchParams(urlObj.search);
const startIndex = params.get('startIndex');

query = { ...query, startIndex };
} else {
delete allResponses.body.links;
break;
}
} while (allowPagination);

return allResponses;
}
Loading
Loading