import {Pager} from '@eg/share/util/pager';
import {IdlObject} from '@eg/core/idl.service';
import {EventService} from '@eg/core/event.service';
+import {AttrDefsService} from './attr-defs.service';
const baseIdlClass = {
lineitem: 'jub',
_terms: AcqSearchTerm[] = [];
_conjunction = 'all';
- attrDefs: {[code: string]: IdlObject};
firstRun = true;
constructor(
private net: NetService,
private evt: EventService,
private auth: AuthService,
- private pcrud: PcrudService
+ private pcrud: PcrudService,
+ private attrDefs: AttrDefsService
) {
- this.attrDefs = {};
this.firstRun = true;
}
- fetchAttrDefs(): Promise<void> {
- if (Object.keys(this.attrDefs).length) {
- return Promise.resolve();
- }
- return new Promise((resolve, reject) => {
- this.pcrud.retrieveAll('acqliad', {},
- {atomic: true}
- ).subscribe(list => {
- list.forEach(acqliad => {
- this.attrDefs[acqliad.code()] = acqliad;
- });
- resolve();
- });
- });
- }
-
setSearch(search: AcqSearch) {
this._terms = search.terms;
this._conjunction = search.conjunction;
searchTerm[operatorMap[filterOp]] = true;
}
if ((['title', 'author'].indexOf(filterField) > -1) &&
- (filterField in this.attrDefs)) {
+ (filterField in this.attrDefs.attrDefs)) {
if (!('acqlia' in andTerms)) {
andTerms['acqlia'] = [];
}
- searchTerm[this.attrDefs[filterField].id()] = filterVal;
+ searchTerm[this.attrDefs.attrDefs[filterField].id()] = filterVal;
andTerms['acqlia'].push(searchTerm);
} else {
searchTerm[filterField] = filterVal;
getAcqSearchDataSource(searchType: string): GridDataSource {
const gridSource = new GridDataSource();
- this.fetchAttrDefs().then(() => {
- gridSource.getRows = (pager: Pager, sort: any[]) => {
+ 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();
- }
+ // 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 currentSearch = this.generateAcqSearch(searchType, gridSource.filters);
+ const currentSearch = this.generateAcqSearch(searchType, gridSource.filters);
- const opts = { ...searchOptions[searchType] };
- opts['offset'] = pager.offset;
- opts['limit'] = pager.limit;
- opts['au_by_id'] = true;
+ const opts = { ...searchOptions[searchType] };
+ opts['offset'] = pager.offset;
+ opts['limit'] = pager.limit;
+ opts['au_by_id'] = true;
- if (sort.length > 0) {
- opts['order_by'] = [];
- sort.forEach(sort_clause => {
- if (searchType === 'lineitem' &&
- ['title', 'author'].indexOf(sort_clause.name) > -1) {
- opts['order_by'].push({
- class: 'acqlia',
- field: 'attr_value',
- direction: sort_clause.dir
- });
- opts['order_by_attr'] = sort_clause.name;
- } else {
- opts['order_by'].push({
- class: baseIdlClass[searchType],
- field: sort_clause.name,
- direction: sort_clause.dir
- });
- }
- });
- }
+ if (sort.length > 0) {
+ opts['order_by'] = [];
+ sort.forEach(sort_clause => {
+ if (searchType === 'lineitem' &&
+ ['title', 'author'].indexOf(sort_clause.name) > -1) {
+ opts['order_by'].push({
+ class: 'acqlia',
+ field: 'attr_value',
+ direction: sort_clause.dir
+ });
+ opts['order_by_attr'] = sort_clause.name;
+ } else {
+ opts['order_by'].push({
+ class: baseIdlClass[searchType],
+ field: sort_clause.name,
+ direction: sort_clause.dir
+ });
+ }
+ });
+ }
- return this.net.request(
- 'open-ils.acq',
- 'open-ils.acq.' + searchType + '.unified_search',
- this.auth.token(),
- currentSearch.andTerms,
- currentSearch.orTerms,
- null,
- opts
- ).pipe(
- map(res => {
- if (this.evt.parse(res)) {
- throw throwError(res);
- } else {
- return res;
- }
- }),
- );
- };
- });
+ return this.net.request(
+ 'open-ils.acq',
+ 'open-ils.acq.' + searchType + '.unified_search',
+ this.auth.token(),
+ currentSearch.andTerms,
+ currentSearch.orTerms,
+ null,
+ opts
+ ).pipe(
+ map(res => {
+ if (this.evt.parse(res)) {
+ throw throwError(res);
+ } else {
+ return res;
+ }
+ }),
+ );
+ };
return gridSource;
}
}