]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-flat-data.service.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-flat-data.service.ts
1 import {Injectable, EventEmitter, TemplateRef} from '@angular/core';
2 import {Observable, empty, throwError} from 'rxjs';
3 import {tap} from 'rxjs/operators';
4 import {StoreService} from '@eg/core/store.service';
5 import {LocaleService} from '@eg/core/locale.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {NetService} from '@eg/core/net.service';
8 import {GridContext, GridColumnSort} from './grid';
9 import {Pager} from '@eg/share/util/pager';
10
11 interface FlatQueryFields {
12     [name: string]: string;
13 }
14
15
16 @Injectable()
17 export class GridFlatDataService {
18
19     constructor(
20         private net: NetService,
21         private auth: AuthService
22     ) {}
23
24
25     getRows(gridContext: GridContext,
26         query: any, pager: Pager, sort: GridColumnSort[]): Observable<any> {
27
28         if (!gridContext.idlClass) {
29             return throwError('GridFlatDataService requires an idlClass');
30         }
31
32         const fields = this.compileFields(gridContext);
33         const flatSort = sort.map(s => {
34             const obj: any = {};
35             obj[s.name] = s.dir;
36             return obj;
37         });
38
39         return this.net.request(
40             'open-ils.fielder',
41             'open-ils.fielder.flattened_search',
42             this.auth.token(), gridContext.idlClass,
43             fields, query, {
44                 sort: flatSort,
45                 limit: pager.limit,
46                 offset: pager.offset
47             }
48         );
49     }
50
51     compileFields(gridContext: GridContext): FlatQueryFields {
52         const fields: FlatQueryFields = {};
53
54         gridContext.columnSet.requiredColumns().forEach(col => {
55             // Verify the column describes a proper IDL field
56             const path = col.path || col.name;
57             const info = gridContext.columnSet.idlInfoFromDotpath(path);
58             if (info) { fields[col.name] = path; }
59         });
60
61         return fields;
62     }
63 }
64