handle case where grid filter supplies multiple conditions for a field
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 15 Nov 2019 23:13:17 +0000 (18:13 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Thu, 16 Jan 2020 21:38:28 +0000 (16:38 -0500)
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 <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts

index 3f142c6..4b7adc3 100644 (file)
@@ -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;