--- /dev/null
+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;
+ }
+}