Skip to content

Commit 000540f

Browse files
committed
feat: chain search of bundle special params
1 parent e2d56c8 commit 000540f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

api/FHIRApiService/search/chain-params.js

+47
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const _ = require("lodash");
22
const resourceIncludeRef = require("../../../api_generator/resource-reference/resourceInclude.json");
33
const { findParamType, isResourceType } = require("../../../utils/fhir-param");
44
const uuid = require("uuid");
5+
const { flatten } = require("flat");
56

67
/**
78
*
@@ -304,6 +305,8 @@ function getChainParentJoinQuery(chainParent, value) {
304305
}
305306
}
306307

308+
processBundleSpecialChain(chainParent, lastParentFieldList, value);
309+
307310
pipeline.push({
308311
$match: {
309312
$or: lastParentFieldList
@@ -315,5 +318,49 @@ function getChainParentJoinQuery(chainParent, value) {
315318
}
316319
}
317320

321+
function processBundleSpecialChain(chainParent, lastParentFieldList, value) {
322+
if (chainParent[0][0].field.includes("entry.0.resource")) {
323+
let lastChain = chainParent[chainParent.length - 1][0];
324+
let originalQuery = {
325+
$and: [],
326+
[lastChain.param]: value
327+
};
328+
lastChain["searchFunc"](originalQuery);
329+
let bundleSpecialQuery = getBundleSpecialQuery(lastChain, originalQuery);
330+
331+
lastParentFieldList.push({
332+
$and: bundleSpecialQuery.$and
333+
});
334+
}
335+
}
336+
337+
function getBundleSpecialQuery(lastChain, query) {
338+
let flattenOriginalQuery = flatten(query, {
339+
object: true
340+
});
341+
let unflattenOriginalQuery = {};
342+
Object.keys(flattenOriginalQuery).map(key => {
343+
let keySplit = key.split(".");
344+
let startPath = keySplit.slice(0, keySplit.lastIndexOf(lastChain.field));
345+
let entryPath = "entry.resource";
346+
let paramPath = keySplit.slice(keySplit.lastIndexOf(lastChain.field));
347+
let combinePath = [entryPath, ...paramPath].join(".");
348+
if (combinePath.includes("$regex")) {
349+
_.set(unflattenOriginalQuery, startPath, {
350+
[combinePath.slice(0, combinePath.indexOf("$regex")-1)]: {
351+
$regex: flattenOriginalQuery[key]
352+
}
353+
});
354+
} else {
355+
_.set(unflattenOriginalQuery, startPath, {
356+
[combinePath]: flattenOriginalQuery[key]
357+
});
358+
}
359+
delete flattenOriginalQuery[key];
360+
});
361+
362+
return unflattenOriginalQuery;
363+
}
364+
318365
module.exports.checkIsChainAndGetChainParent = checkIsChainAndGetChainParent;
319366
module.exports.getChainParentJoinQuery = getChainParentJoinQuery;

0 commit comments

Comments
 (0)