Merge branch '16159-logout-request-with-token'
[arvados-workbench2.git] / src / services / api / filter-builder.ts
index 08746c81b9ee1d91736b4e9e09eb79b820bd916f..d1a4fd08b6aa5b32500c727cb1ea1acbf695fd61 100644 (file)
@@ -2,17 +2,19 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as _ from "lodash";
-
-export function joinFilters(filters0?: string, filters1?: string) {
-    return [filters0, filters1].filter(s => s).join(",");
+export function joinFilters(...filters: string[]) {
+    return filters.filter(s => s).join(",");
 }
 
 export class FilterBuilder {
     constructor(private filters = "") { }
 
-    public addEqual(field: string, value?: string | boolean, resourcePrefix?: string) {
-        return this.addCondition(field, "=", value, "", "", resourcePrefix );
+    public addEqual(field: string, value?: string | boolean | null, resourcePrefix?: string) {
+        return this.addCondition(field, "=", value, "", "", resourcePrefix);
+    }
+
+    public addDistinct(field: string, value?: string | boolean | null, resourcePrefix?: string) {
+        return this.addCondition(field, "!=", value, "", "", resourcePrefix);
     }
 
     public addLike(field: string, value?: string, resourcePrefix?: string) {
@@ -23,6 +25,10 @@ export class FilterBuilder {
         return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
     }
 
+    public addContains(field: string, value?: string, resourcePrefix?: string) {
+        return this.addCondition(field, "contains", value, "", "", resourcePrefix);
+    }
+
     public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
         return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
     }
@@ -54,18 +60,31 @@ export class FilterBuilder {
     public addExists(value?: string, resourcePrefix?: string) {
         return this.addCondition("properties", "exists", value, "", "", resourcePrefix);
     }
+    public addDoesNotExist(field: string, resourcePrefix?: string) {
+        return this.addCondition("properties." + field, "exists", false, "", "", resourcePrefix);
+    }
+
+    public addFullTextSearch(value: string) {
+        const terms = value.trim().split(/(\s+)/);
+        terms.forEach(term => {
+            if (term !== " ") {
+                this.addCondition("any", "ilike", term, "%", "%");
+            }
+        });
+        return this;
+    }
 
     public getFilters() {
         return this.filters;
     }
 
-    private addCondition(field: string, cond: string, value?: string | string[] | boolean, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
-        if (value) {
+    private addCondition(field: string, cond: string, value?: string | string[] | boolean | null, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
+        if (value !== undefined) {
             if (typeof value === "string") {
                 value = `"${prefix}${value}${postfix}"`;
             } else if (Array.isArray(value)) {
                 value = `["${value.join(`","`)}"]`;
-            } else {
+            } else if (value !== null) {
                 value = value ? "true" : "false";
             }
 
@@ -73,9 +92,7 @@ export class FilterBuilder {
                 ? resourcePrefix + "."
                 : "";
 
-            const fld = field.indexOf('properties.') < 0 ? _.snakeCase(field) : field;
-
-            this.filters += `${this.filters ? "," : ""}["${resPrefix}${fld}","${cond}",${value}]`;
+            this.filters += `${this.filters ? "," : ""}["${resPrefix}${field}","${cond}",${value}]`;
         }
         return this;
     }