Skip to content

Commit

Permalink
Merge pull request #36 from jembi/TB-400-summary-endpoint-forward-params
Browse files Browse the repository at this point in the history
Forwarding non _mdm query Parameters to Hapi FHIR
  • Loading branch information
drizzentic authored Apr 15, 2024
2 parents 70b34ed + 03c3238 commit 375665d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/middlewares/mpi-mdm-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { fetchMpiPatientLinks } from '../utils/mpi';
import { buildOpenhimResponseObject } from '../utils/utils';
import { fetchAllPatientSummariesByRefs } from '../routes/handlers/fetchPatientSummaries';

const fetchAllLinkedPatientSummary = async (patientId: string) => {
const fetchAllLinkedPatientSummary = async (patientId: string, params: object) => {
try {
const patientRef = `Patient/${patientId}`;
const patientRefs: string[] = [];

await fetchMpiPatientLinks(patientRef, patientRefs);

const bundle = await fetchAllPatientSummariesByRefs(patientRefs);
const bundle = await fetchAllPatientSummariesByRefs(patientRefs, params);

logger.debug(`Fetched all patient summaries from the MPI: ${bundle}`);

Expand Down Expand Up @@ -40,7 +40,9 @@ export const mpiMdmSummaryMiddleware: RequestHandler = async (req, res, next) =>
return next();
}

const { status, body } = await fetchAllLinkedPatientSummary(req.params.patientId);
delete req.query._mdm;

const { status, body } = await fetchAllLinkedPatientSummary(req.params.patientId, req.query);

res.set('Content-Type', 'application/json+openhim');
res.status(status).send(body);
Expand Down
19 changes: 15 additions & 4 deletions src/routes/handlers/fetchPatientSummaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ const {
} = getConfig();

export const fetchAllPatientSummariesByRefs = async (
patientRefs: string[]
patientRefs: string[],
queryParams?: object
): Promise<Bundle> => {
const patientExternalRefs = patientRefs.map((ref) => {
const path = `/fhir/${ref}/$summary`;
const params = Object.entries(queryParams ?? {});
let combinedParams = null;

if (params.length > 0) {
combinedParams = params
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join('&');
}

const path = `/fhir/${ref}/$summary${combinedParams ? `?${combinedParams}` : ''}`;

return getData(protocol, host, port, path, {
'Content-Type': 'application/fhir+json',
Expand All @@ -43,10 +53,11 @@ export const fetchAllPatientSummariesByRefs = async (
};

export const fetchPatientSummaryByRef = async (
ref: string
ref: string,
queryParams: object
): Promise<MpiMediatorResponseObject> => {
try {
const bundle = await fetchAllPatientSummariesByRefs([ref]);
const bundle = await fetchAllPatientSummariesByRefs([ref], queryParams);
const responseBody = buildOpenhimResponseObject('Successful', 200, bundle);

logger.info(`Successfully fetched patient summary with id ${ref}`);
Expand Down
5 changes: 4 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ routes.get(
'/fhir/Patient/:patientId/\\$summary',
mpiMdmSummaryMiddleware,
asyncHandler(async (req, res) => {
const { status, body } = await fetchPatientSummaryByRef(`Patient/${req.params.patientId}`);
const { status, body } = await fetchPatientSummaryByRef(
`Patient/${req.params.patientId}`,
req.query
);

res.set('Content-Type', 'application/json+openhim');
res.status(status).send(body);
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/fetchPatientSummaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,13 @@ describe('FetchPatientSummaries handler', (): void => {
describe('fetchPatientSummariesByRefs', async () => {
it('should return an empty bundle', async () => {
nock(fhirDatastoreUrl).get(`/fhir/${emptyPatientRef1}/$summary`).reply(200, {});

const result = await fetchPatientSummaryByRef(emptyPatientRef1);
const result = await fetchPatientSummaryByRef(emptyPatientRef1, {});
expect(JSON.parse(result.body.response.body)).to.deep.equal(emptyBundle);
});
it('should return a bundle with 2 entries for 1 given patient', async () => {
nock(fhirDatastoreUrl).get(`/fhir/${patientRef3}/$summary`).reply(200, patientSummary3);

const result = await fetchPatientSummaryByRef(patientRef3);
const result = await fetchPatientSummaryByRef(patientRef3, {});
expect(JSON.parse(result.body.response.body)).to.deep.equal(combinedBundle2);
});
});
Expand Down

0 comments on commit 375665d

Please sign in to comment.