]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/transfer-items.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / transfer-items.component.ts
1 import {Component, 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 {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
8 import {AlertDialogComponent} from '@eg/share/dialog/alert.component';
9 import {StringComponent} from '@eg/share/string/string.component';
10
11 /* Transfer items to a call number. */
12
13 @Component({
14   selector: 'eg-transfer-items',
15   templateUrl: 'transfer-items.component.html'
16 })
17
18 export class TransferItemsComponent {
19
20     @ViewChild('successMsg', {static: false})
21         private successMsg: StringComponent;
22
23     @ViewChild('errorMsg', {static: false})
24         private errorMsg: StringComponent;
25
26     @ViewChild('noTargetMsg', {static: false})
27         private noTargetMsg: StringComponent;
28
29     @ViewChild('confirmDialog', {static: false})
30         private confirmDialog: ConfirmDialogComponent;
31
32     @ViewChild('alertDialog', {static: false})
33         private alertDialog: AlertDialogComponent;
34
35     eventDesc: string;
36
37     constructor(
38         private toast: ToastService,
39         private net: NetService,
40         private auth: AuthService,
41         private evt: EventService) {}
42
43     // Transfers a set of items/copies (by ID) to the selected call
44     // number (by ID).
45     // Resolves with true if transfer completed, false otherwise.
46     transferItems(itemIds: number[],
47         cnId: number, override?: boolean): Promise<boolean> {
48
49         this.eventDesc = '';
50
51         let method = 'open-ils.cat.transfer_copies_to_volume';
52         if (override) { method += '.override'; }
53
54         return this.net.request('open-ils.cat',
55             method, this.auth.token(), cnId, itemIds)
56         .toPromise().then(resp => {
57
58             const evt = this.evt.parse(resp);
59
60             if (evt) {
61
62                 if (override) {
63                     // Override failed, no looping please.
64                     this.toast.warning(this.errorMsg.text);
65                     return false;
66                 }
67
68                 this.eventDesc = evt.desc;
69
70                 return this.confirmDialog.open().toPromise().then(ok =>
71                     ok ? this.transferItems(itemIds, cnId, true) : false);
72
73             } else { // success
74
75                 this.toast.success(this.successMsg.text);
76                 return true;
77             }
78         });
79     }
80
81     // Transfers a set of items/copies (by object with fleshed call numbers)
82     // to the selected record and org unit ID, creating new call numbers
83     // where needed.
84     // Resolves with true if transfer completed, false otherwise.
85     autoTransferItems(items: IdlObject[], // acp with fleshed call_number's
86         recId: number, orgId: number): Promise<Boolean> {
87
88         this.eventDesc = '';
89
90         const cnTransfers: any = {};
91         const itemTransfers: any = {};
92
93         items.forEach(item => {
94             const cn = item.call_number();
95
96             if (cn.owning_lib() !== orgId || cn.record() !== recId) {
97                 cn.owning_lib(orgId);
98                 cn.record(recId);
99
100                 if (cnTransfers[cn.id()]) {
101                     itemTransfers[cn.id()].push(item.id());
102
103                 } else {
104                     cnTransfers[cn.id()] = cn;
105                     itemTransfers[cn.id()] = [item.id()];
106                 }
107             }
108         });
109
110         return this.transferCallNumbers(cnTransfers, itemTransfers);
111     }
112
113     transferCallNumbers(cnTransfers, itemTransfers): Promise<boolean> {
114
115         const cnId = Object.keys(cnTransfers)[0];
116         const cn = cnTransfers[cnId];
117         delete cnTransfers[cnId];
118
119         return this.net.request('open-ils.cat',
120             'open-ils.cat.call_number.find_or_create',
121             this.auth.token(),
122             cn.label(),
123             cn.record(),     // may be new
124             cn.owning_lib(), // may be new
125             (typeof cn.prefix() === 'object' ? cn.prefix().id() : cn.prefix()),
126             (typeof cn.suffix() === 'object' ? cn.suffix().id() : cn.suffix()),
127             cn.label_class()
128
129         ).toPromise().then(resp => {
130
131             const evt = this.evt.parse(resp);
132
133             if (evt) {
134                 // Problem.  Stop processing.
135                 this.toast.warning(this.errorMsg.text);
136                 this.eventDesc = evt.desc;
137                 return this.alertDialog.open().toPromise().then(_ => false);
138             }
139
140             return this.transferItems(itemTransfers[cn.id()], resp.acn_id)
141             .then(ok => {
142
143                 if (ok && Object.keys(cnTransfers).length > 0) {
144                     // More call numbers to transfer.
145                     return this.transferCallNumbers(cnTransfers, itemTransfers);
146                 }
147
148                 return ok;
149             });
150         });
151     }
152 }
153
154
155