From 1efa8a4f720430ccf42d6c886b60f16d86af6dbf Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 6 Mar 2020 10:41:52 -0500 Subject: [PATCH] start work on AcqProviderSearch Signed-off-by: Galen Charlton --- .../acq/provider/acq-provider-search.service.ts | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Open-ILS/src/eg2/src/app/staff/acq/provider/acq-provider-search.service.ts 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 index 0000000000..0850a536a3 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/acq/provider/acq-provider-search.service.ts @@ -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; + } +} -- 2.11.0