]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/barcodes/barcode-select.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / barcodes / barcode-select.component.ts
1 import {Component, Input, Output, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, mergeMap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {EventService, EgEvent} from '@eg/core/event.service';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
12
13 /* Support barcode completion for barcoded asset/actor data.
14  *
15  * When multiple barcodes match, the user is presented with a selection
16  * dialog to chose the desired barcode.
17  *
18  * <eg-barcode-select #barcodeSelect></eg-barcode-select>
19  *
20  * @ViewChild('barcodeSelect') private barcodeSelect: BarcodeSelectComponent;
21  *
22  * this.barcodeSelect.getBarcode(value)
23  *   .then(barcode => console.log('found barcode', barcode));
24  */
25
26 export interface BarcodeSelectResult {
27
28     // Will be the originally requested barcode when no match is found.
29     barcode: string;
30
31     // Will be null when no match is found.
32     id: number;
33 }
34
35 @Component({
36   selector: 'eg-barcode-select',
37   templateUrl: './barcode-select.component.html',
38 })
39
40 export class BarcodeSelectComponent extends DialogComponent {
41
42     matches: BarcodeSelectResult[];
43     selected: BarcodeSelectResult;
44     inputs: {[id: number]: boolean};
45
46     constructor(
47         private modal: NgbModal,
48         private evt: EventService,
49         private org: OrgService,
50         private net: NetService,
51         private pcrud: PcrudService,
52         private auth: AuthService
53     ) { super(modal); }
54
55     selectionChanged() {
56         const id = Object.keys(this.inputs).map(i => Number(i))
57             .filter(i => this.inputs[i] === true)[0];
58
59         if (id) {
60             this.selected = this.matches.filter(match => match.id === id)[0];
61
62         } else {
63             this.selected = null;
64         }
65     }
66
67     // Returns promise of barcode
68     // When multiple barcodes match, the user is asked to select one.
69     // Returns promise of null if no match is found or the user cancels
70     // the selection process.
71     getBarcode(class_: 'asset' | 'actor',
72         barcode: string): Promise<BarcodeSelectResult> {
73
74         this.matches = [];
75         this.inputs = {};
76
77         const result: BarcodeSelectResult = {
78             barcode: barcode,
79             id: null
80         };
81
82        let promise = this.net.request(
83             'open-ils.actor',
84             'open-ils.actor.get_barcodes',
85             this.auth.token(), this.auth.user().ws_ou(),
86             class_, barcode.trim()
87         ).toPromise();
88
89         promise = promise.then(results => {
90
91             if (!results) { return result; }
92
93             results.forEach(res => {
94                 if (!this.evt.parse(res)) {
95                     this.matches.push(res);
96                 }
97             });
98
99             if (this.matches.length === 0) {
100                 return result;
101
102             } else if (this.matches.length === 1) {
103                 return this.matches[0];
104
105             } else {
106                 return this.open().toPromise();
107             }
108         });
109
110         return promise;
111     }
112 }
113