Skip to content

Commit

Permalink
feat: update chain search logic
Browse files Browse the repository at this point in the history
1. When search coming, check parameter is chain or not
> chain rule
> Have dot
> the parameter must be reference type
2. If is chain, combine the mongodb query
3. support colon chain parameter
  • Loading branch information
Chinlinlee committed Nov 8, 2022
1 parent 3cb4c6f commit 687978b
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 171 deletions.
64 changes: 0 additions & 64 deletions api/FHIR/Patient/PatientParametersHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,69 +484,5 @@ paramsSearch["organization"] = (query) => {
};
//#endregion

// #region chain search
let patientRefItem = resourceInclude["Patient"];
for (let refItem of patientRefItem) {
let refSearch = Object.keys(paramsSearchFields).map(v => {
if (paramsSearchFields[v].find(field => field.includes(refItem.path))) {
return v;
}
});

refSearch = _.compact(refSearch);
if (refSearch.length > 0) {
let refSearchParam = refSearch[0];
let refSearchParamField = paramsSearchFields[refSearchParam][0];
let refResources = refItem.resourceList;
for (let refResource of refResources) {
if (refResource === "Resource") continue;
if (refResource === path.basename(__dirname)) {
parameterHandler = {
paramsSearch: paramsSearch,
paramsSearchFields: paramsSearchFields
};
} else {
parameterHandler = require(`../${refResource}/${refResource}ParametersHandler`);
}
if (_.isEmpty(parameterHandler)) continue;

let refParamSearch = parameterHandler.paramsSearch;
Object.keys(refParamSearch).forEach(v => {
paramsSearch[`${refSearchParam}.${v}`] = function(query) {
let targetSearchParamsFields = parameterHandler.paramsSearchFields[v];
query["isChain"] = true;
let queryForChain = {
$and: [],
[v]: query[`${refSearchParam}.${v}`]
};
refParamSearch[v](queryForChain);
for (let targetField of targetSearchParamsFields) {
let originalPath = jp.nodes(queryForChain, `$..${targetField}`);
let cleanPath = originalPath[0].path.slice(1);
cleanPath.pop();
let replacePath = [...cleanPath, `ref${refResource}.${targetField}`];
let searchValue = originalPath[0].value;
_.set(queryForChain, replacePath, searchValue);
queryForChain = _.omit(queryForChain, [`${cleanPath.join(".")}.${targetField}`]);
}

let chainAggregate = chainSearch(refResource, refSearchParamField);
chainAggregate.push({
"$match": {
...queryForChain
}
});

if (!_.get(query, "chain")) query["chain"] = [];
query["chain"] = [...query["chain"], chainAggregate];
delete query[`${refSearchParam}.${v}`]
}
});
}
}
}
//#endregion


module.exports.paramsSearch = paramsSearch;
module.exports.paramsSearchFields = paramsSearchFields;
Loading

0 comments on commit 687978b

Please sign in to comment.