]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/transfer-holdings.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / transfer-holdings.component.ts
1 import {Component, Input, ViewChild} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {EventService} from '@eg/core/event.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {AlertDialogComponent} from '@eg/share/dialog/alert.component';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
10
11 /* Transfer holdings (AKA asset.call_number) to a target bib record. */
12
13 @Component({
14   selector: 'eg-transfer-holdings',
15   templateUrl: 'transfer-holdings.component.html'
16 })
17
18 export class TransferHoldingsComponent {
19
20     // Array of 'acn' objects.
21     // Assumes all acn's are children of the same bib record.
22     @Input() callNums: IdlObject[];
23
24     // Required field.
25     // All call numbers will be transferred to this record ID.
26     @Input() targetRecId: number;
27
28     // Optional.  If set, all call numbers will transfer to this org
29     // unit (owning lib) in addition to transfering to the select bib
30     // record.
31     @Input() targetOrgId: number;
32
33     @ViewChild('successMsg', {static: false})
34         private successMsg: StringComponent;
35
36     @ViewChild('noTargetMsg', {static: false})
37         private noTargetMsg: StringComponent;
38
39     @ViewChild('alertDialog', {static: false})
40         private alertDialog: AlertDialogComponent;
41
42     @ViewChild('progressDialog', {static: false})
43         private progressDialog: ProgressDialogComponent;
44
45     eventDesc: string;
46
47     constructor(
48         private toast: ToastService,
49         private net: NetService,
50         private auth: AuthService,
51         private evt: EventService) {}
52
53     // Resolves with true if transfer completed, false otherwise.
54     // Assumes all volumes are transferred to the same bib record.
55     transferHoldings(): Promise<Boolean> {
56         if (!this.callNums || this.callNums.length === 0) {
57             return Promise.resolve(false);
58         }
59
60         if (!this.targetRecId) {
61             this.toast.warning(this.noTargetMsg.text);
62             return Promise.resolve(false);
63         }
64
65         this.eventDesc = '';
66
67         // Group the transfers by owning library.
68         const transferVols: {[orgId: number]: number[]} = {};
69
70         if (this.targetOrgId) {
71
72             // Transfering all call numbers to the same bib record
73             // and owning library.
74             transferVols[+this.targetOrgId] = this.callNums.map(cn => cn.id());
75
76         } else {
77
78             // Transfering all call numbers to the same bib record
79             // while retaining existing owning library.
80             this.callNums.forEach(cn => {
81                 const orgId = Number(cn.owning_lib());
82                 if (!transferVols[orgId]) { transferVols[orgId] = []; }
83                 transferVols[orgId].push(cn.id());
84             });
85         }
86
87         this.progressDialog.update({
88             value: 0,
89             max: Object.keys(transferVols).length
90         });
91         this.progressDialog.open();
92
93         return this.performTransfers(transferVols)
94         .then(res => {
95             this.progressDialog.close();
96             return res;
97         });
98     }
99
100     performTransfers(transferVols: any): Promise<Boolean> {
101         const orgId = Object.keys(transferVols)[0];
102         const volIds = transferVols[orgId];
103
104         // Avoid re-processing
105         delete transferVols[orgId];
106
107         // Note the AngJS client also assumes .override.
108         const method = 'open-ils.cat.asset.volume.batch.transfer.override';
109
110         return this.net.request('open-ils.cat', method, this.auth.token(), {
111             docid: this.targetRecId,
112             lib: orgId,
113             volumes: volIds
114         }).toPromise().then(resp => {
115             const evt = this.evt.parse(resp);
116
117             if (evt || Number(resp) !== 1) {
118                 console.warn(resp);
119                 this.eventDesc = evt ? evt.desc : '';
120
121                 // Failure -- stop short there to avoid alert storm.
122                 return this.alertDialog.open().toPromise()
123                 .then(_ => { this.eventDesc = ''; return false; });
124             }
125
126             this.progressDialog.increment();
127
128             if (Object.keys(transferVols).length > 0) {
129                 return this.performTransfers(transferVols);
130             }
131
132             return true; // All done
133         });
134     }
135 }
136
137
138