]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/queue.component.ts
LP1779158 Angular7 and ng-lint updates
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / queue.component.ts
1 import {Component, OnInit, AfterViewInit, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, filter} from 'rxjs/operators';
4 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
5 import {Pager} from '@eg/share/util/pager';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {EventService} from '@eg/core/event.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
11 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
12 import {GridComponent} from '@eg/share/grid/grid.component';
13 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
14 import {VandelayService, VandelayImportSelection,
15     VANDELAY_EXPORT_PATH} from './vandelay.service';
16
17 @Component({
18   templateUrl: 'queue.component.html'
19 })
20 export class QueueComponent implements OnInit, AfterViewInit {
21
22     queueId: number;
23     queueType: string; // bib / authority
24     queueSource: GridDataSource;
25     queuedRecClass: string;
26     queueSummary: any;
27
28     filters = {
29         matches: false,
30         nonImported: false,
31         withErrors: false
32     };
33
34     limitToMatches: (checked: boolean) => void;
35     limitToNonImported: (checked: boolean) => void;
36     limitToImportErrors: (checked: boolean) => void;
37
38     // keep a local copy for convenience
39     attrDefs: IdlObject[];
40
41     @ViewChild('queueGrid') queueGrid: GridComponent;
42     @ViewChild('confirmDelDlg') confirmDelDlg: ConfirmDialogComponent;
43     @ViewChild('progressDlg') progressDlg: ProgressDialogComponent;
44
45     constructor(
46         private router: Router,
47         private route: ActivatedRoute,
48         private evt: EventService,
49         private net: NetService,
50         private auth: AuthService,
51         private vandelay: VandelayService) {
52
53         this.route.paramMap.subscribe((params: ParamMap) => {
54             this.queueType = params.get('qtype');
55             this.queueId = +params.get('id');
56         });
57
58         this.queueSource = new GridDataSource();
59         this.queueSource.getRows = (pager: Pager) => {
60             this.vandelay.queuePageOffset = pager.offset;
61             return this.loadQueueRecords(pager);
62         };
63
64         this.limitToMatches = (checked: boolean) => {
65             this.filters.matches = checked;
66             this.queueGrid.reload();
67         };
68
69         this.limitToNonImported = (checked: boolean) => {
70             this.filters.nonImported = checked;
71             this.queueGrid.reload();
72         };
73
74         this.limitToImportErrors = (checked: boolean) => {
75             this.filters.withErrors = checked;
76             this.queueGrid.reload();
77         };
78     }
79
80     ngOnInit() {
81     }
82
83     queuePageOffset(): number {
84         return this.vandelay.queuePageOffset;
85     }
86
87     ngAfterViewInit() {
88         if (this.queueType) {
89             this.applyQueueType();
90             if (this.queueId) {
91                 this.loadQueueSummary();
92             }
93         }
94     }
95
96     openRecord(row: any) {
97         if (this.queueType === 'auth') {
98             this.queueType = 'authority';
99         }
100         const url =
101           `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
102         this.router.navigate([url]);
103     }
104
105     applyQueueType() {
106         this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
107         this.vandelay.getAttrDefs(this.queueType).then(
108             attrs => {
109                 this.attrDefs = attrs;
110                 // Add grid columns for record attributes
111                 attrs.forEach(attr => {
112                     const col = new GridColumn();
113                     col.name = attr.code(),
114                     col.label = attr.description(),
115                     col.datatype = 'string';
116                     this.queueGrid.context.columnSet.add(col);
117                 });
118
119                 // Reapply the grid configuration now that we've
120                 // dynamically added columns.
121                 this.queueGrid.context.applyGridConfig();
122             }
123         );
124     }
125
126     qtypeShort(): string {
127         return this.queueType === 'bib' ? 'bib' : 'auth';
128     }
129
130     loadQueueSummary(): Promise<any> {
131         const method =
132             `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
133
134         return this.net.request(
135             'open-ils.vandelay', method, this.auth.token(), this.queueId)
136         .toPromise().then(sum => this.queueSummary = sum);
137     }
138
139     loadQueueRecords(pager: Pager): Observable<any> {
140
141         const options = {
142             clear_marc: true,
143             offset: pager.offset,
144             limit: pager.limit,
145             flesh_import_items: true,
146             non_imported: this.filters.nonImported,
147             with_import_error: this.filters.withErrors
148         };
149
150         return this.vandelay.getQueuedRecords(
151             this.queueId, this.queueType, options, this.filters.matches).pipe(
152         filter(rec => {
153             // avoid sending mishapen data to the grid
154             // this happens (among other reasons) when the grid
155             // no longer exists
156             const e = this.evt.parse(rec);
157             if (e) { console.error(e); return false; }
158             return true;
159         }),
160         map(rec => {
161             const recHash: any = {
162                 id: rec.id(),
163                 import_error: rec.import_error(),
164                 error_detail: rec.error_detail(),
165                 import_time: rec.import_time(),
166                 imported_as: rec.imported_as(),
167                 import_items: [],
168                 error_items: [],
169                 matches: rec.matches()
170             };
171
172             if (this.queueType === 'bib') {
173                 recHash.import_items = rec.import_items();
174                 recHash.error_items = rec.import_items().filter(i => i.import_error());
175             }
176
177             // Link the record attribute values to the root record
178             // object so the grid can find them.
179             rec.attributes().forEach(attr => {
180                 const def =
181                     this.attrDefs.filter(d => d.id() === attr.field())[0];
182                 recHash[def.code()] = attr.attr_value();
183             });
184
185             return recHash;
186         }));
187     }
188
189     findOrCreateImportSelection() {
190         let selection = this.vandelay.importSelection;
191         if (!selection) {
192             selection = new VandelayImportSelection();
193             this.vandelay.importSelection = selection;
194         }
195         selection.queue = this.queueSummary.queue;
196         return selection;
197     }
198
199     hasOverlayTarget(rid: number): boolean {
200         return this.vandelay.importSelection &&
201             Boolean(this.vandelay.importSelection.overlayMap[rid]);
202     }
203
204     importSelected() {
205         const rows = this.queueGrid.context.getSelectedRows();
206         if (rows.length) {
207             const selection = this.findOrCreateImportSelection();
208             selection.recordIds = rows.map(row => row.id);
209             console.log('importing: ', this.vandelay.importSelection);
210             this.router.navigate(['/staff/cat/vandelay/import']);
211         }
212     }
213
214     importAll() {
215         const selection = this.findOrCreateImportSelection();
216         selection.importQueue = true;
217         this.router.navigate(['/staff/cat/vandelay/import']);
218     }
219
220     deleteQueue() {
221         this.confirmDelDlg.open().then(
222             yes => {
223                 this.progressDlg.open();
224                 return this.net.request(
225                     'open-ils.vandelay',
226                     `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
227                     this.auth.token(), this.queueId
228                 ).toPromise();
229             },
230             no => {
231                 this.progressDlg.close();
232                 return Promise.reject('delete failed');
233             }
234         ).then(
235             resp => {
236                 this.progressDlg.close();
237                 const e = this.evt.parse(resp);
238                 if (e) {
239                     console.error(e);
240                     alert(e);
241                 } else {
242                     // Jump back to the main queue page.
243                     this.router.navigate(['/staff/cat/vandelay/queue']);
244                 }
245             },
246             err => {
247                 this.progressDlg.close();
248             }
249         );
250     }
251
252     exportNonImported() {
253         this.vandelay.exportQueue(this.queueSummary.queue, true);
254     }
255 }
256