diff --git a/src/smartHandler.test.ts b/src/smartHandler.test.ts index 80c4d27..b68c09b 100644 --- a/src/smartHandler.test.ts +++ b/src/smartHandler.test.ts @@ -734,6 +734,7 @@ describe('authorizeAndFilterReadResponse', () => { lastUpdated: '2020-11-20T11:10:48.034+00:00', }, type: 'searchset', + total: 0, link: [ { relation: 'self', @@ -749,6 +750,7 @@ describe('authorizeAndFilterReadResponse', () => { const searchAllEntitiesMatch = { ...emptySearchResult, entry: [createEntry(validPatient), createEntry(validPatientObservation), createEntry(validPatientEncounter)], + total: 3, }; const searchSomeEntitiesMatch = { @@ -760,6 +762,7 @@ describe('authorizeAndFilterReadResponse', () => { createEntry({ ...validPatientObservation, subject: 'not-you' }), createEntry(validPatientEncounter), ], + total: 5, }; const searchNoEntitiesMatch = { ...emptySearchResult, @@ -767,6 +770,7 @@ describe('authorizeAndFilterReadResponse', () => { createEntry({ ...validPatient, id: 'not-yours' }), createEntry({ ...validPatientObservation, subject: 'not-you' }), ], + total: 2, }; const cases: (string | ReadResponseAuthorizedRequest | boolean | any)[][] = [ [ @@ -998,7 +1002,7 @@ describe('authorizeAndFilterReadResponse', () => { readResponse: searchAllEntitiesMatch, }, true, - { ...emptySearchResult, entry: [createEntry(validPatientEncounter)] }, + { ...emptySearchResult, entry: [createEntry(validPatientEncounter)], total: 1 }, ], [ 'SEARCH: user scope; Practitioner able to search and get ALL results', diff --git a/src/smartHandler.ts b/src/smartHandler.ts index 4502bba..75787d9 100644 --- a/src/smartHandler.ts +++ b/src/smartHandler.ts @@ -318,7 +318,7 @@ export class SMARTHandler implements Authorization { const { operation, readResponse } = request; // If request is a search treat the readResponse as a bundle if (SEARCH_OPERATIONS.includes(operation)) { - const entries = (readResponse.entry ?? []).filter((entry: { resource: any }) => + const entries: any[] = (readResponse.entry ?? []).filter((entry: { resource: any }) => hasAccessToResource( fhirUserObject, patientLaunchContext, @@ -329,7 +329,13 @@ export class SMARTHandler implements Authorization { this.fhirVersion, ), ); - return { ...readResponse, entry: entries }; + let numTotal: number = readResponse.total; + if (!numTotal) { + numTotal = entries.length; + } else { + numTotal -= readResponse.entry.length - entries.length; + } + return { ...readResponse, entry: entries, total: numTotal }; } // If request is != search treat the readResponse as just a resource if (