Skip to content

Commit

Permalink
feat: handle expression where(type='${type}') in the reference type…
Browse files Browse the repository at this point in the history
… search parameter
  • Loading branch information
Chinlinlee committed Feb 7, 2022
1 parent 2b9751b commit 17fe9b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions api_generator/parameterHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ class ReferenceParameter {
constructor(param, field) {
this.Param = param;
this.Field = field;
this.FixedType=""; //using in `RelatedArtifact`, expression: relatedArtifact.where(type='composed-of').resource(Any)
}

getCodeString() {
Expand All @@ -443,7 +444,15 @@ class ReferenceParameter {
searchFields = searchFields.map(v=> {
if (v.includes("where")) {
let lastIndexFieldInField = v.lastIndexOf(".");
v = v.substring(0, lastIndexFieldInField) + ".reference";
if (!v.includes("type=")) {
v = v.substring(0, lastIndexFieldInField) + ".reference";
} else {
let firstDotIndex = v.indexOf(".");
let fieldName = v.substring(0, firstDotIndex);
let type = v.substring(v.indexOf("type=")+5, v.lastIndexOf("'")).replace("'", "");
this.FixedType = type;
v = fieldName + v.substring(lastIndexFieldInField);
}
} else if (v.includes(" as ")) {
v = v.substr(0, v.indexOf(" as "));
} else {
Expand All @@ -455,7 +464,7 @@ class ReferenceParameter {
codeStr += `
paramsSearch["${this.Param}"] = (query) => {
try {
queryHandler.getReferenceQuery(query, paramsSearchFields, "${this.Param}");
queryHandler.getReferenceQuery(query, paramsSearchFields, "${this.Param}"${(this.FixedType) ? `, "${this.FixedType}"`: ""});
} catch(e) {
console.error(e);
throw e;
Expand Down
16 changes: 15 additions & 1 deletion models/FHIR/queryBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function instantQuery(value, field) {
return queryBuilder;
}

function referenceQuery (query , field) {
function referenceQuery (query , field, type="") {
const urlRegex = /^(http|https):\/\/(.*)\/(\w+\/.+)$/;
const isUrl = query.match(urlRegex);
let typeAndId = query.split("/");
Expand All @@ -438,6 +438,20 @@ function referenceQuery (query , field) {
} else {
queryBuilder[field] = {$regex : new RegExp(query)};
}
if (type) {
let andQuery = {
$and: []
};
let typeField = field.substring(0, field.lastIndexOf(".")) + ".type";
queryBuilder[typeField] = type;
andQuery.$and.push({
[typeField]: queryBuilder[typeField]
});
andQuery.$and.push({
[field]: queryBuilder[field]
});
return andQuery;
}
return queryBuilder;
}
function arrayStringBuild (query , field , queryField) {
Expand Down
4 changes: 2 additions & 2 deletions models/FHIR/searchParameterQueryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function getPolyDateQuery(query, paramsSearchFields, queryFieldName, paramsSearc
delete query[queryFieldName];
}

function getReferenceQuery(query, paramsSearchFields, queryFieldName) {
function getReferenceQuery(query, paramsSearchFields, queryFieldName, type="") {
if (!_.isArray(query[queryFieldName])) {
query[queryFieldName] = [query[queryFieldName]];
}
Expand All @@ -346,7 +346,7 @@ function getReferenceQuery(query, paramsSearchFields, queryFieldName) {
$or : []
};
for (let field of paramsSearchFields[queryFieldName]) {
let buildResult =queryBuild.referenceQuery(item , field);
let buildResult =queryBuild.referenceQuery(item , field, type);
buildQs.$or.push(buildResult);
}
query.$and.push({
Expand Down

0 comments on commit 17fe9b2

Please sign in to comment.