Skip to content

Commit

Permalink
feat: chain search of bundle special params
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Sep 23, 2023
1 parent e2d56c8 commit 000540f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions api/FHIRApiService/search/chain-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require("lodash");
const resourceIncludeRef = require("../../../api_generator/resource-reference/resourceInclude.json");
const { findParamType, isResourceType } = require("../../../utils/fhir-param");
const uuid = require("uuid");
const { flatten } = require("flat");

/**
*
Expand Down Expand Up @@ -304,6 +305,8 @@ function getChainParentJoinQuery(chainParent, value) {
}
}

processBundleSpecialChain(chainParent, lastParentFieldList, value);

pipeline.push({
$match: {
$or: lastParentFieldList
Expand All @@ -315,5 +318,49 @@ function getChainParentJoinQuery(chainParent, value) {
}
}

function processBundleSpecialChain(chainParent, lastParentFieldList, value) {
if (chainParent[0][0].field.includes("entry.0.resource")) {
let lastChain = chainParent[chainParent.length - 1][0];
let originalQuery = {
$and: [],
[lastChain.param]: value
};
lastChain["searchFunc"](originalQuery);
let bundleSpecialQuery = getBundleSpecialQuery(lastChain, originalQuery);

lastParentFieldList.push({
$and: bundleSpecialQuery.$and
});
}
}

function getBundleSpecialQuery(lastChain, query) {
let flattenOriginalQuery = flatten(query, {
object: true
});
let unflattenOriginalQuery = {};
Object.keys(flattenOriginalQuery).map(key => {
let keySplit = key.split(".");
let startPath = keySplit.slice(0, keySplit.lastIndexOf(lastChain.field));
let entryPath = "entry.resource";
let paramPath = keySplit.slice(keySplit.lastIndexOf(lastChain.field));
let combinePath = [entryPath, ...paramPath].join(".");
if (combinePath.includes("$regex")) {
_.set(unflattenOriginalQuery, startPath, {
[combinePath.slice(0, combinePath.indexOf("$regex")-1)]: {
$regex: flattenOriginalQuery[key]
}
});
} else {
_.set(unflattenOriginalQuery, startPath, {
[combinePath]: flattenOriginalQuery[key]
});
}
delete flattenOriginalQuery[key];
});

return unflattenOriginalQuery;
}

module.exports.checkIsChainAndGetChainParent = checkIsChainAndGetChainParent;
module.exports.getChainParentJoinQuery = getChainParentJoinQuery;

0 comments on commit 000540f

Please sign in to comment.