From 595541f3a0cd96ff8a99fd18e50b2894d15f52a5 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Mon, 2 Oct 2023 16:07:25 -0700 Subject: [PATCH] correctly handle query for token with system but no code. --- backend/pkg/database/sqlite_repository_query.go | 17 ++++++++--------- .../database/sqlite_repository_query_test.go | 2 ++ .../pages/report-labs/report-labs.component.ts | 6 ++++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/backend/pkg/database/sqlite_repository_query.go b/backend/pkg/database/sqlite_repository_query.go index 3740c8500..e3a6e2349 100644 --- a/backend/pkg/database/sqlite_repository_query.go +++ b/backend/pkg/database/sqlite_repository_query.go @@ -417,12 +417,8 @@ func ProcessSearchParameterValue(searchParameter SearchParameter, searchValueWit } } else if len(searchParameterValueParts) == 2 { //if theres 2 parts, first is always system, second is always the code. Either one may be emty. If both are emty this is invalid. - if len(searchParameterValueParts[0]) > 0 { - searchParameterValue.SecondaryValues[searchParameter.Name+"System"] = searchParameterValueParts[0] - } - if len(searchParameterValueParts[1]) > 0 { - searchParameterValue.Value = searchParameterValueParts[1] - } + searchParameterValue.SecondaryValues[searchParameter.Name+"System"] = searchParameterValueParts[0] + searchParameterValue.Value = searchParameterValueParts[1] if len(searchParameterValueParts[0]) == 0 && len(searchParameterValueParts[1]) == 0 { return searchParameterValue, fmt.Errorf("invalid search parameter value: (%s=%s)", searchParameter.Name, searchParameterValue.Value) } @@ -571,7 +567,10 @@ func SearchCodeToWhereClause(searchParam SearchParameter, searchParamValue Searc //TODO: support ":text" modifier //setup the clause - clause := fmt.Sprintf("%sJson.value ->> '$.code' = @%s", searchParam.Name, NamedParameterWithSuffix(searchParam.Name, namedParameterSuffix)) + clause := []string{} + if searchParamValue.Value.(string) != "" { + clause = append(clause, fmt.Sprintf("%sJson.value ->> '$.code' = @%s", searchParam.Name, NamedParameterWithSuffix(searchParam.Name, namedParameterSuffix))) + } //append the code and/or system clauses (if required) //this looks like unnecessary code, however its required to ensure consistent tests @@ -580,10 +579,10 @@ func SearchCodeToWhereClause(searchParam SearchParameter, searchParamValue Searc for _, k := range allowedSecondaryKeys { namedParameterKey := fmt.Sprintf("%s%s", searchParam.Name, strings.Title(k)) if _, ok := searchParamValue.SecondaryValues[namedParameterKey]; ok { - clause += fmt.Sprintf(` AND %sJson.value ->> '$.%s' = @%s`, searchParam.Name, k, NamedParameterWithSuffix(namedParameterKey, namedParameterSuffix)) + clause = append(clause, fmt.Sprintf(`%sJson.value ->> '$.%s' = @%s`, searchParam.Name, k, NamedParameterWithSuffix(namedParameterKey, namedParameterSuffix))) } } - return fmt.Sprintf("(%s)", clause), searchClauseNamedParams, nil + return fmt.Sprintf("(%s)", strings.Join(clause, " AND ")), searchClauseNamedParams, nil case SearchParameterTypeKeyword: //setup the clause diff --git a/backend/pkg/database/sqlite_repository_query_test.go b/backend/pkg/database/sqlite_repository_query_test.go index add11cb5e..b6a9168c8 100644 --- a/backend/pkg/database/sqlite_repository_query_test.go +++ b/backend/pkg/database/sqlite_repository_query_test.go @@ -103,6 +103,8 @@ func TestProcessSearchParameterValue(t *testing.T) { {SearchParameter{Type: "token", Name: "identifier", Modifier: "otype"}, "http://terminology.hl7.org/CodeSystem/v2-0203|MR|446053", SearchParameterValue{Value: "MR|446053", Prefix: "", SecondaryValues: map[string]interface{}{"identifierSystem": "http://terminology.hl7.org/CodeSystem/v2-0203"}}, false}, {SearchParameter{Type: "token", Name: "code", Modifier: ""}, "|", SearchParameterValue{}, true}, //empty value should throw an error {SearchParameter{Type: "token", Name: "code", Modifier: ""}, "", SearchParameterValue{}, true}, //empty value should throw an error + {SearchParameter{Type: "token", Name: "code", Modifier: ""}, "http://acme.org/conditions/codes|", SearchParameterValue{Value: "", Prefix: "", SecondaryValues: map[string]interface{}{"codeSystem": "http://acme.org/conditions/codes"}}, false}, + {SearchParameter{Type: "token", Name: "code", Modifier: ""}, "|807-1", SearchParameterValue{Value: "807-1", Prefix: "", SecondaryValues: map[string]interface{}{"codeSystem": ""}}, false}, {SearchParameter{Type: "quantity", Name: "valueQuantity", Modifier: ""}, "5.4|http://unitsofmeasure.org|mg", SearchParameterValue{Value: float64(5.4), Prefix: "", SecondaryValues: map[string]interface{}{"valueQuantitySystem": "http://unitsofmeasure.org", "valueQuantityCode": "mg"}}, false}, {SearchParameter{Type: "quantity", Name: "valueQuantity", Modifier: ""}, "5.40e-3|http://unitsofmeasure.org|g", SearchParameterValue{Value: float64(0.0054), Prefix: "", SecondaryValues: map[string]interface{}{"valueQuantitySystem": "http://unitsofmeasure.org", "valueQuantityCode": "g"}}, false}, diff --git a/frontend/src/app/pages/report-labs/report-labs.component.ts b/frontend/src/app/pages/report-labs/report-labs.component.ts index 29252173a..25bc1d045 100644 --- a/frontend/src/app/pages/report-labs/report-labs.component.ts +++ b/frontend/src/app/pages/report-labs/report-labs.component.ts @@ -39,9 +39,11 @@ export class ReportLabsComponent implements OnInit { }) this.fastenApi.queryResources({ - select: ["*"], + select: [], from: "Observation", - where: {}, + where: { + "code": "http://loinc.org|,urn:oid:2.16.840.1.113883.6.1|", + }, aggregations: { order_by: { field: "sort_date",