start work on AcqProviderSearch
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 6 Mar 2020 15:41:52 +0000 (10:41 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 6 Mar 2020 15:42:04 +0000 (10:42 -0500)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/provider/acq-provider-search.service.ts [new file with mode: 0644]

diff --git a/Open-ILS/src/eg2/src/app/staff/acq/provider/acq-provider-search.service.ts b/Open-ILS/src/eg2/src/app/staff/acq/provider/acq-provider-search.service.ts
new file mode 100644 (file)
index 0000000..0850a53
--- /dev/null
@@ -0,0 +1,115 @@
+import {Injectable} from '@angular/core';
+import {empty, throwError} from 'rxjs';
+import {map} from 'rxjs/operators';
+import {AuthService} from '@eg/core/auth.service';
+import {GridDataSource} from '@eg/share/grid/grid';
+import {PcrudService} from '@eg/core/pcrud.service';
+import {Pager} from '@eg/share/util/pager';
+import {EventService} from '@eg/core/event.service';
+
+export interface AcqProviderSearchTerm {
+    field: string;
+    value: string;
+}
+
+export interface AcqProviderSearch {
+    terms: ProviderTerm[];
+}
+
+@Injectable()
+export class AcqProviderSearchService {
+
+    _terms: AcqProviderSearchTerm[] = [];
+    firstRun = true;
+
+    constructor(
+        private evt: EventService,
+        private auth: AuthService,
+        private pcrud: PcrudService
+    ) {
+        this.firstRun = true;
+    }
+
+    setSearch(search: AcqProviderSearch) {
+        this._terms = search.terms;
+        this.firstRun = false;
+    }
+
+    generateSearch(filters): any {
+        // base query to grab all providers
+        const base = { id: { '!=': null } };
+        const query: any = new Array();
+        query.push(base);
+
+        // handle supplied search terms
+        this._terms.forEach(term => {
+            if (term.value === '') {
+                return;
+            }
+
+            const query_part = new Object();
+            query_part[term.field] = { '=' : term.value };
+            query.push(query_part);
+        });
+
+        // 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
+        const observables = [];
+        Object.keys(filters).forEach(filterField => {
+            filters[filterField].forEach(condition => {
+                query.push(condition);
+            });
+        });
+        return query;
+    }
+
+    getDataSource(): GridDataSource {
+        const gridSource = new GridDataSource();
+
+        gridSource.getRows = (pager: Pager, sort: any[]) => {
+
+            // don't do a search the very first time we
+            // get invoked, which is during initialization; we'll
+            // let components higher up the change decide whether
+            // to submit a search
+            if (this.firstRun) {
+                this.firstRun = false;
+                return empty();
+            }
+
+            const query = this.generateSearch(gridSource.filters);
+
+            const opts = {};
+            opts['offset'] = pager.offset;
+            opts['limit'] = pager.limit;
+            opts['au_by_id'] = true;
+
+            if (sort.length > 0) {
+                opts['order_by'] = [];
+                sort.forEach(sort_clause => {
+                    opts['order_by'].push({
+                        class: 'acqpro',
+                        field: sort_clause.name,
+                        direction: sort_clause.dir
+                    });
+                });
+            }
+
+            return this.pcrud.search('acqpro',
+                query,
+                opts
+            ).pipe(
+                map(res => {
+                    if (this.evt.parse(res)) {
+                        throw throwError(res);
+                    } else {
+                        return res;
+                    }
+                }),
+            );
+        };
+        return gridSource;
+    }
+}