-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from mcode/parse-query-parameters
Parse query parameters in the library
- Loading branch information
Showing
5 changed files
with
238 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { Bundle, BundleEntry, Parameters } from 'fhir/r4'; | ||
import { parseQueryParameters } from '../src/query-parameters'; | ||
|
||
describe('parseQueryParameters', () => { | ||
it('extracts passed properties', () => { | ||
expect( | ||
parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'Parameters', | ||
parameter: [ | ||
{ | ||
name: 'zipCode', | ||
valueString: '01730' | ||
}, | ||
{ | ||
name: 'travelRadius', | ||
valueString: '25' | ||
}, | ||
{ | ||
name: 'phase', | ||
valueString: 'phase-1' | ||
}, | ||
{ | ||
name: 'recruitmentStatus', | ||
valueString: 'approved' | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}) | ||
).toEqual({ | ||
zipCode: '01730', | ||
travelRadius: 25, | ||
phase: 'phase-1', | ||
recruitmentStatus: 'approved' | ||
}); | ||
}); | ||
it('extracts travel radius from valueDecimal', () => { | ||
expect( | ||
parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'Parameters', | ||
parameter: [ | ||
{ | ||
name: 'travelRadius', | ||
valueDecimal: 25 | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}) | ||
).toEqual({ | ||
travelRadius: 25 | ||
}); | ||
}); | ||
it('ignores travel radius of an invalid type', () => { | ||
expect( | ||
parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'Parameters', | ||
parameter: [ | ||
{ | ||
name: 'travelRadius', | ||
valueDecimal: 25 | ||
}, | ||
{ | ||
name: 'travelRadius', | ||
valueBoolean: true | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}) | ||
).toEqual({ | ||
travelRadius: 25 | ||
}); | ||
}); | ||
|
||
it('ignores unknown parameters', () => { | ||
const parameters = parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'Parameters', | ||
parameter: [ | ||
{ | ||
name: 'unknown', | ||
valueString: 'invalid' | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}); | ||
expect(parameters).toEqual({}); | ||
}); | ||
|
||
it('ignores invalid entries', () => { | ||
const bundle: Bundle = { | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: undefined | ||
} | ||
] | ||
}; | ||
// Force an invalid entry in | ||
bundle.entry?.push({ invalid: true } as unknown as BundleEntry, { resource: 'invalid' } as unknown as BundleEntry); | ||
// Passing in this case is not throwing an exception and returning nothing | ||
expect(parseQueryParameters(bundle)).toEqual({}); | ||
}); | ||
|
||
it('ignores non-parameter resources', () => { | ||
expect( | ||
parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'Observation', | ||
code: {}, | ||
status: 'final' | ||
} | ||
} | ||
] | ||
}) | ||
).toEqual({}); | ||
}); | ||
|
||
it('ignores invalid parameters', () => { | ||
// Invalid parameters object | ||
const invalidParameters = { | ||
resourceType: 'Parameters', | ||
parameter: 'invalid' | ||
} as unknown as Parameters; | ||
// Passing in this case is not throwing an exception and returning nothing | ||
expect( | ||
parseQueryParameters({ | ||
resourceType: 'Bundle', | ||
type: 'collection', | ||
entry: [ | ||
{ | ||
resource: invalidParameters | ||
} | ||
] | ||
}) | ||
).toEqual({}); | ||
}); | ||
|
||
it('ignores invalid bundles', () => { | ||
// Passing in this case is not throwing an exception and returning nothing | ||
expect( | ||
parseQueryParameters({ | ||
entry: 'invalid' | ||
} as unknown as Bundle) | ||
).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Bundle } from 'fhir/r4'; | ||
|
||
/** | ||
* The following parameters are defined by the PCT IG. | ||
*/ | ||
export interface QueryParameters { | ||
zipCode?: string; | ||
travelRadius?: number; | ||
phase?: string; | ||
recruitmentStatus?: string; | ||
} | ||
|
||
/** | ||
* Parses out query parameters from a patient bundle. | ||
* @param patientBundle the patient bundle containing parameters | ||
*/ | ||
export function parseQueryParameters(patientBundle: Bundle): QueryParameters { | ||
// Resulting parameters | ||
const parameters: QueryParameters = {}; | ||
if (Array.isArray(patientBundle.entry)) { | ||
for (const entry of patientBundle.entry) { | ||
if (!('resource' in entry)) { | ||
// Skip bad entries | ||
continue; | ||
} | ||
const resource = entry.resource; | ||
// Pull out search parameters | ||
if (resource?.resourceType === 'Parameters') { | ||
if (Array.isArray(resource.parameter)) { | ||
for (const parameter of resource.parameter) { | ||
if (parameter.name === 'zipCode') { | ||
parameters.zipCode = parameter.valueString; | ||
} else if (parameter.name === 'travelRadius') { | ||
if (typeof parameter.valueString === 'string') { | ||
parameters.travelRadius = parseFloat(parameter.valueString); | ||
} else if (typeof parameter.valueDecimal === 'number') { | ||
parameters.travelRadius = parameter.valueDecimal; | ||
} | ||
} else if (parameter.name === 'phase') { | ||
parameters.phase = parameter.valueString; | ||
} else if (parameter.name === 'recruitmentStatus') { | ||
parameters.recruitmentStatus = parameter.valueString; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return parameters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters