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