implement more filter operators
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 15 Nov 2019 22:54:38 +0000 (17:54 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Thu, 16 Jan 2020 21:38:28 +0000 (16:38 -0500)
- contains
- does not contain
- starts with
- ends with

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts

index dbad57f..3f142c6 100644 (file)
@@ -62,10 +62,14 @@ const searchOptions = {
 };
 
 const operatorMap = {
+    "!=": "__not",
     ">": "__gte",
     ">=": "__gte",
     "<=": "__lte",
     "<": "__lte",
+    "startswith": "__starts",
+    "endswith": "__ends",
+    "like": "__fuzzy",
 }
 
 @Injectable()
@@ -80,11 +84,30 @@ export class AcqSearchService {
     generateAcqSearch(searchType, filters): any {
         const baseSearch = JSON.parse(JSON.stringify(defaultSearch[searchType])); // deep copy
         Object.keys(filters).forEach(filterField => {
+            const searchTerm: Object = {};
             const coreRecType = Object.keys(defaultSearch[searchType])[0];
-            const filterOp = Object.keys(filters[filterField][0][filterField])[0];
-            const filterVal = filters[filterField][0][filterField][filterOp];
+            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";
+                    }
+                }
+            }
 
-            const searchTerm: Object = {};
             searchTerm[filterField] = filterVal;
             if (filterOp in operatorMap) {
                 searchTerm[operatorMap[filterOp]] = true;