Skip to content

Commit

Permalink
Merge branch '10.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Oct 7, 2024
2 parents 19aa214 + 3b85469 commit 5e814a1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/openchs-android/src/service/CustomFilterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import _ from "lodash";
import ConceptService from "./ConceptService";
import moment from "moment";
import RealmQueryService from "./query/RealmQueryService";
import {JSONStringify} from "../utility/JsonStringify";

function getDateFilterFunction(filterValue, widget, queryColumn) {
if (widget === CustomFilter.widget.Range) {
Expand All @@ -26,6 +27,10 @@ function getDateFilterFunction(filterValue, widget, queryColumn) {
}
}

function noQueryResultFunction() {
return "";
}

@Service("customFilterService")
class CustomFilterService extends BaseService {
constructor(db, context) {
Expand Down Expand Up @@ -148,7 +153,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 +223,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,18 +369,18 @@ 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];
const {scopeParameters, scope, conceptUUID, type, widget} = filter;
const selectedAnswerFilterQueryFunction = this.getFilterQueryByTypeFunction(filter, selectedOptions);
const subjects = this.getSubjects(conceptUUID, selectedOptions, type, scope, scopeParameters, widget, selectedAnswerFilterQueryFunction, includeVoided);
const filterHasValue = !_.isEmpty(selectedOptions) && !_.isNil(selectedOptions)
if (!filterHasValue || _.isEmpty(uniqueSubjectUUIDs)) {
uniqueSubjectUUIDs = subjects;
} else {
uniqueSubjectUUIDs = _.intersection(uniqueSubjectUUIDs, subjects);
if (filterHasValue) {
const selectedAnswerFilterQueryFunction = this.getFilterQueryByTypeFunction(filter, selectedOptions);
const subjects = this.getSubjects(conceptUUID, selectedOptions, type, scope, scopeParameters, widget, selectedAnswerFilterQueryFunction, includeVoided);
if (_.isEmpty(uniqueSubjectUUIDs))
uniqueSubjectUUIDs = subjects
else
uniqueSubjectUUIDs = _.intersection(uniqueSubjectUUIDs, subjects);
}
});
return uniqueSubjectUUIDs;
Expand Down

0 comments on commit 5e814a1

Please sign in to comment.