Skip to content

Commit

Permalink
#1531 - do not run query when in filter function returns empty string…
Browse files Browse the repository at this point in the history
… or when range type is used but no range is present.
  • Loading branch information
petmongrels committed Oct 4, 2024
1 parent 073abf1 commit 02cdac6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/openchs-android/src/service/CustomFilterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function getDateFilterFunction(filterValue, widget, queryColumn) {
}
}

function noQueryResultFunction() {
return "";
}

@Service("customFilterService")
class CustomFilterService extends BaseService {
constructor(db, context) {
Expand Down Expand Up @@ -148,7 +152,8 @@ class CustomFilterService extends BaseService {
filterForNonRangeWidgetType(latestEncounters, schemaName, selectedAnswerFiltersFunction, indFunc, inMemoryFilter) {
//cannot append next filtered to this query because sorting happens at the end of query and we will not get expected result.
//so we get the most recent encounters from above query and pass it down to the next query.
if (_.isEmpty(latestEncounters)) return [];
if (_.isEmpty(latestEncounters) || _.isEmpty(selectedAnswerFiltersFunction())) return [];

let encountersBasedOnSelectedAnswers = latestEncounters.filtered(` ${selectedAnswerFiltersFunction()} `);
if (!_.isNil(inMemoryFilter)) {
General.logDebug("CustomFilterService", "Running in memory filter");
Expand Down Expand Up @@ -217,28 +222,28 @@ class CustomFilterService extends BaseService {
case (Concept.dataType.Notes) :
case (Concept.dataType.Id) :
const textFilterQuery = _.map(selectedOptions, c => ` (concept.uuid == '${concept.uuid}' AND ${this.tokenizedNameQuery(c.name)}) `).join(" OR ");
return () => this.getObsSubQueryForQuery(textFilterQuery);
return selectedOptions.length === 0 ? noQueryResultFunction : () => this.getObsSubQueryForQuery(textFilterQuery);
case (Concept.dataType.Numeric) :
if (widget === CustomFilter.widget.Range) {
return (obs) => obs.concept.uuid === concept.uuid && obs.getValue() >= selectedOption.minValue && obs.getValue() <= selectedOption.maxValue;
} else {
const numericFilterQuery = _.map(selectedOptions, c => ` (concept.uuid == '${concept.uuid}' AND valueJSON CONTAINS[c] '"answer":${c.minValue}}') `).join(" OR ");
return () => this.getObsSubQueryForQuery(numericFilterQuery);
return selectedOptions.length === 0 ? noQueryResultFunction : () => this.getObsSubQueryForQuery(numericFilterQuery);
}
case (Concept.dataType.Date) :
case (Concept.dataType.DateTime):
if (widget === CustomFilter.widget.Range) {
return (obs) => obs.concept.uuid === concept.uuid && moment(obs.getValue()).isBetween(selectedOption.minValue, selectedOption.maxValue, null, []);
} else {
const dateFilterQuery = _.map(selectedOptions, c => ` (concept.uuid == '${concept.uuid}' AND valueJSON CONTAINS[c] '"answer":"${c.minValue.replace('Z', '')}') `).join(" OR ");
return () => this.getObsSubQueryForQuery(dateFilterQuery);
return selectedOptions.length === 0 ? noQueryResultFunction : () => this.getObsSubQueryForQuery(dateFilterQuery);
}
case (Concept.dataType.Time):
if (widget === CustomFilter.widget.Range) {
return (obs) => obs.concept.uuid === concept.uuid && moment(obs.getValue(), 'H:mma').isBetween(moment(selectedOption.minValue, 'h:mma'), moment(selectedOption.maxValue, 'h:mma'), null, []);
} else {
const timeFilterQuery = _.map(selectedOptions, c => ` (concept.uuid == '${concept.uuid}' AND valueJSON CONTAINS[c] '${c.minValue}') `).join(" OR ");
return () => this.getObsSubQueryForQuery(timeFilterQuery);
return selectedOptions.length === 0 ? noQueryResultFunction : () => this.getObsSubQueryForQuery(timeFilterQuery);
}
default:
return () => 'uuid != null';
Expand Down Expand Up @@ -363,7 +368,6 @@ class CustomFilterService extends BaseService {
}

applyCustomFilters(customFilters, filterName, includeVoided = false) {
General.logDebugTemp("CustomFilterService", "Applying custom filters");
let uniqueSubjectUUIDs = [];
_.forEach(this.getSettings()[filterName], filter => {
const selectedOptions = customFilters[filter.titleKey];
Expand Down

0 comments on commit 02cdac6

Please sign in to comment.