implement LI grid filtering for acqlia title and author
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 24 Jan 2020 11:18:51 +0000 (06:18 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 24 Jan 2020 11:18:51 +0000 (06:18 -0500)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.service.ts
Open-ILS/src/eg2/src/app/staff/acq/search/lineitem-results.component.html

index 55f30d7..c3847e2 100644 (file)
@@ -2,7 +2,9 @@ import {Injectable} from '@angular/core';
 import {NetService} from '@eg/core/net.service';
 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 {IdlObject} from '@eg/core/idl.service';
 
 const defaultSearch = {
     lineitem: {
@@ -88,11 +90,30 @@ export class AcqSearchService {
 
     _terms: AcqSearchTerm[] = [];
     _conjunction = 'all';
+    attrDefs: {[code: string]: IdlObject};
 
     constructor(
         private net: NetService,
-        private auth: AuthService
+        private auth: AuthService,
+        private pcrud: PcrudService
     ) {
+        this.attrDefs = {};
+    }
+
+    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) {
@@ -141,6 +162,7 @@ export class AcqSearchService {
         // 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 => {
                 const searchTerm: Object = {};
@@ -166,11 +188,20 @@ export class AcqSearchService {
                     }
                 }
 
-                searchTerm[filterField] = filterVal;
                 if (filterOp in operatorMap) {
                     searchTerm[operatorMap[filterOp]] = true;
                 }
-                andTerms[coreRecType].push(searchTerm);
+                if ((['title', 'author'].indexOf(filterField) > -1) &&
+                     (filterField in this.attrDefs)) {
+                        if (!('acqlia' in andTerms)) {
+                            andTerms['acqlia'] = [];
+                        }
+                        searchTerm[this.attrDefs[filterField].id()] = filterVal;
+                        andTerms['acqlia'].push(searchTerm);
+                } else {
+                    searchTerm[filterField] = filterVal;
+                    andTerms[coreRecType].push(searchTerm);
+                }
             });
         });
         console.debug(andTerms);
@@ -180,23 +211,25 @@ export class AcqSearchService {
     getAcqSearchDataSource(searchType: string): GridDataSource {
         const gridSource = new GridDataSource();
 
-        gridSource.getRows = (pager: Pager) => {
-
-            const currentSearch = this.generateAcqSearch(searchType, gridSource.filters);
-
-            const opts = { ...searchOptions[searchType] };
-            opts['offset'] = pager.offset;
-            opts['limit'] = pager.limit;
-            return this.net.request(
-                'open-ils.acq',
-                'open-ils.acq.' + searchType + '.unified_search',
-                    this.auth.token(),
-                    currentSearch.andTerms,
-                    currentSearch.orTerms,
-                    null,
-                    opts
-            );
-        };
+        this.fetchAttrDefs().then(() => {
+            gridSource.getRows = (pager: Pager) => {
+
+                const currentSearch = this.generateAcqSearch(searchType, gridSource.filters);
+
+                const opts = { ...searchOptions[searchType] };
+                opts['offset'] = pager.offset;
+                opts['limit'] = pager.limit;
+                return this.net.request(
+                    'open-ils.acq',
+                    'open-ils.acq.' + searchType + '.unified_search',
+                        this.auth.token(),
+                        currentSearch.andTerms,
+                        currentSearch.orTerms,
+                        null,
+                        opts
+                );
+            };
+        });
         return gridSource;
     }
 }
index 794fa40..18eee2c 100644 (file)
@@ -59,8 +59,8 @@
 
   <eg-grid-column path="id" [cellTemplate]="idTmpl" [disableTooltip]="true"></eg-grid-column>
   <!-- TODO: Title and Author filters will require special work as they're acqlia values -->
-  <eg-grid-column i18n-label label="Title" path="title" [cellTemplate]="liAttrTmpl" [filterable]="false"></eg-grid-column>
-  <eg-grid-column i18n-label label="Author" path="author" [cellTemplate]="liAttrTmpl" [filterable]="false"></eg-grid-column>
+  <eg-grid-column i18n-label label="Title" path="title" [cellTemplate]="liAttrTmpl"></eg-grid-column>
+  <eg-grid-column i18n-label label="Author" path="author" [cellTemplate]="liAttrTmpl"></eg-grid-column>
   <eg-grid-column path="provider" [cellTemplate]="providerTmpl"></eg-grid-column>
   <eg-grid-column i18n-label label="Links" path="_links" [cellTemplate]="liLinksTmpl" [disableTooltip]="true" [filterable]="false"></eg-grid-column>
   <eg-grid-column path="claim_policy.name"></eg-grid-column>