diff --git a/api_generator/parameterHandler.js b/api_generator/parameterHandler.js index 0e419d12..31dfbdfe 100644 --- a/api_generator/parameterHandler.js +++ b/api_generator/parameterHandler.js @@ -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() { @@ -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 { @@ -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; diff --git a/models/FHIR/queryBuild.js b/models/FHIR/queryBuild.js index 2e25f5d6..cc0f29d1 100644 --- a/models/FHIR/queryBuild.js +++ b/models/FHIR/queryBuild.js @@ -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("/"); @@ -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) { diff --git a/models/FHIR/searchParameterQueryHandler.js b/models/FHIR/searchParameterQueryHandler.js index 1d79b3c0..b671765b 100644 --- a/models/FHIR/searchParameterQueryHandler.js +++ b/models/FHIR/searchParameterQueryHandler.js @@ -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]]; } @@ -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({