1 /* eslint-disable no-unused-expressions */
2 import {Component, AfterViewInit, ViewChild} from '@angular/core';
3 import {Observable} from 'rxjs';
4 import {map, filter} from 'rxjs/operators';
5 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
6 import {Pager} from '@eg/share/util/pager';
7 import {IdlObject} from '@eg/core/idl.service';
8 import {EventService} from '@eg/core/event.service';
9 import {NetService} from '@eg/core/net.service';
10 import {AuthService} from '@eg/core/auth.service';
11 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
12 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
15 import {VandelayService, VandelayImportSelection} from './vandelay.service';
18 templateUrl: 'queue.component.html'
20 export class QueueComponent implements AfterViewInit {
23 queueType: string; // bib / authority
24 queueSource: GridDataSource;
25 queuedRecClass: string;
34 // keep a local copy for convenience
35 attrDefs: IdlObject[];
37 @ViewChild('queueGrid', { static: true }) queueGrid: GridComponent;
38 @ViewChild('confirmDelDlg', { static: false }) confirmDelDlg: ConfirmDialogComponent;
39 @ViewChild('progressDlg', { static: true }) progressDlg: ProgressDialogComponent;
41 cellTextGenerator: GridCellTextGenerator;
44 private router: Router,
45 private route: ActivatedRoute,
46 private evt: EventService,
47 private net: NetService,
48 private auth: AuthService,
49 private vandelay: VandelayService) {
51 this.route.paramMap.subscribe((params: ParamMap) => {
52 this.queueType = params.get('qtype');
53 this.queueId = +params.get('id');
56 this.queueSource = new GridDataSource();
57 this.queueSource.getRows = (pager: Pager) => {
58 this.vandelay.queuePageOffset = pager.offset;
59 return this.loadQueueRecords(pager);
62 this.cellTextGenerator = {
63 '+matches': row => row.matches.length + '',
64 // eslint-disable-next-line eqeqeq
65 'import_error': row => (row.import_error == null) ? '' : row.import_error,
66 'imported_as': row => row.imported_as + ''
70 limitToMatches(checked: boolean) {
71 this.filters.matches = checked;
72 this.queueGrid.reload();
75 limitToNonImported(checked: boolean) {
76 this.filters.nonImported = checked;
77 this.queueGrid.reload();
80 limitToImportErrors(checked: boolean) {
81 this.filters.withErrors = checked;
82 this.queueGrid.reload();
85 queuePageOffset(): number {
86 return this.vandelay.queuePageOffset;
91 this.applyQueueType();
93 this.loadQueueSummary();
98 openRecord(row: any) {
99 if (this.queueType === 'auth') {
100 this.queueType = 'authority';
103 `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
104 this.router.navigate([url]);
108 this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
109 this.vandelay.getAttrDefs(this.queueType).then(
111 this.attrDefs = attrs;
112 // Add grid columns for record attributes
113 attrs.forEach(attr => {
114 const col = new GridColumn();
115 col.name = attr.code(),
116 col.label = attr.description(),
117 col.datatype = 'string';
118 this.queueGrid.context.columnSet.add(col);
121 // Reapply the grid configuration now that we've
122 // dynamically added columns.
123 this.queueGrid.context.applyGridConfig();
128 qtypeShort(): string {
129 return this.queueType === 'bib' ? 'bib' : 'auth';
132 loadQueueSummary(): Promise<any> {
134 `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
136 return this.net.request(
137 'open-ils.vandelay', method, this.auth.token(), this.queueId)
138 .toPromise().then(sum => this.queueSummary = sum);
141 loadQueueRecords(pager: Pager): Observable<any> {
145 offset: pager.offset,
147 flesh_import_items: true,
148 non_imported: this.filters.nonImported,
149 with_import_error: this.filters.withErrors
152 return this.vandelay.getQueuedRecords(
153 this.queueId, this.queueType, options, this.filters.matches).pipe(
155 // avoid sending mishapen data to the grid
156 // this happens (among other reasons) when the grid
158 const e = this.evt.parse(rec);
159 if (e) { console.error(e); return false; }
163 const recHash: any = {
165 import_error: rec.import_error(),
166 error_detail: rec.error_detail(),
167 import_time: rec.import_time(),
168 imported_as: rec.imported_as(),
171 matches: rec.matches()
174 if (this.queueType === 'bib') {
175 recHash.import_items = rec.import_items();
176 recHash.error_items = rec.import_items().filter(i => i.import_error());
179 // Link the record attribute values to the root record
180 // object so the grid can find them.
181 rec.attributes().forEach(attr => {
183 this.attrDefs.filter(d => d.id() === attr.field())[0];
184 recHash[def.code()] = attr.attr_value();
191 findOrCreateImportSelection() {
192 let selection = this.vandelay.importSelection;
194 selection = new VandelayImportSelection();
195 this.vandelay.importSelection = selection;
197 selection.queue = this.queueSummary.queue;
201 hasOverlayTarget(rid: number): boolean {
202 return this.vandelay.importSelection &&
203 Boolean(this.vandelay.importSelection.overlayMap[rid]);
207 const rows = this.queueGrid.context.getSelectedRows();
209 const selection = this.findOrCreateImportSelection();
210 selection.recordIds = rows.map(row => row.id);
211 console.log('importing: ', this.vandelay.importSelection);
212 this.router.navigate(['/staff/cat/vandelay/import']);
217 const selection = this.findOrCreateImportSelection();
218 selection.importQueue = true;
219 this.router.navigate(['/staff/cat/vandelay/import']);
224 this.confirmDelDlg.open().subscribe(confirmed => {
225 if (!confirmed) { return; }
227 this.progressDlg.open();
230 `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
231 this.auth.token(), this.queueId
234 const e = this.evt.parse(resp);
235 if (e) { return new Error(e.toString()); }
237 // Jump back to the main queue page.
238 this.router.navigate(['/staff/cat/vandelay/queue']);
240 err => console.error('queue deletion failed!', err)
241 ).finally(() => this.progressDlg.close());
245 exportNonImported() {
246 this.vandelay.exportQueue(this.queueSummary.queue, true);