From cb95194d941b86ffeb20b1b106f9a836b034b1c3 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 15 Nov 2019 18:13:17 -0500 Subject: [PATCH] handle case where grid filter supplies multiple conditions for a field This can be the case for dates. TODO: deal with au term rewriting; for conditions coming from grid filters, it's always going to be an exact match on a single user ID. Signed-off-by: Galen Charlton --- .../src/app/staff/acq/search/acq-search.service.ts | 59 ++++++++++++---------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts b/Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts index 3f142c661f..4b7adc3024 100644 --- a/Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts +++ b/Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts @@ -83,36 +83,43 @@ export class AcqSearchService { generateAcqSearch(searchType, filters): any { const baseSearch = JSON.parse(JSON.stringify(defaultSearch[searchType])); // deep copy + const coreRecType = Object.keys(defaultSearch[searchType])[0]; + + // handle grid filters + // note that date filters coming from the grid do not need + // to worry about __castdate because the grid filter supplies + // both the start and end times Object.keys(filters).forEach(filterField => { - const searchTerm: Object = {}; - const coreRecType = Object.keys(defaultSearch[searchType])[0]; - let filterOp = "="; - let filterVal = ""; - if (Object.keys(filters[filterField][0]).some(x => x === "-not")) { - filterOp = Object.keys(filters[filterField][0]["-not"][filterField])[0]; - filterVal = filters[filterField][0]["-not"][filterField][filterOp]; - searchTerm["__not"] = true; - } else { - filterOp = Object.keys(filters[filterField][0][filterField])[0]; - filterVal = filters[filterField][0][filterField][filterOp]; - if (filterOp === "like" && filterVal.length > 1) { - if (filterVal[0] === "%" && filterVal[filterVal.length - 1] === "%") { - filterVal = filterVal.slice(1, filterVal.length - 1); - } else if (filterVal[filterVal.length - 1] === "%") { - filterVal = filterVal.slice(0, filterVal.length - 1); - filterOp = "startswith"; - } else if (filterVal[0] === "%") { - filterVal = filterVal.slice(1); - filterOp = "endswith"; + filters[filterField].forEach(condition => { + const searchTerm: Object = {}; + let filterOp = "="; + let filterVal = ""; + if (Object.keys(condition).some(x => x === "-not")) { + filterOp = Object.keys(condition["-not"][filterField])[0]; + filterVal = condition["-not"][filterField][filterOp]; + searchTerm["__not"] = true; + } else { + filterOp = Object.keys(condition[filterField])[0]; + filterVal = condition[filterField][filterOp]; + if (filterOp === "like" && filterVal.length > 1) { + if (filterVal[0] === "%" && filterVal[filterVal.length - 1] === "%") { + filterVal = filterVal.slice(1, filterVal.length - 1); + } else if (filterVal[filterVal.length - 1] === "%") { + filterVal = filterVal.slice(0, filterVal.length - 1); + filterOp = "startswith"; + } else if (filterVal[0] === "%") { + filterVal = filterVal.slice(1); + filterOp = "endswith"; + } } } - } - searchTerm[filterField] = filterVal; - if (filterOp in operatorMap) { - searchTerm[operatorMap[filterOp]] = true; - } - baseSearch[coreRecType].push(searchTerm); + searchTerm[filterField] = filterVal; + if (filterOp in operatorMap) { + searchTerm[operatorMap[filterOp]] = true; + } + baseSearch[coreRecType].push(searchTerm); + }); }); console.debug(baseSearch); return baseSearch; -- 2.11.0