LP1823367 Move away from "volume" moniker internally
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / delete-volcopy-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {EventService} from '@eg/core/event.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
10 import {DialogComponent} from '@eg/share/dialog/dialog.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12
13
14 /**
15  * Dialog that confirms, then deletes items and call numbers
16  */
17
18 @Component({
19   selector: 'eg-delete-holding-dialog',
20   templateUrl: 'delete-volcopy-dialog.component.html'
21 })
22
23 export class DeleteHoldingDialogComponent
24     extends DialogComponent implements OnInit {
25
26     // List of "acn" objects which may contain copies.
27     // Objects of either type marked "isdeleted" will be deleted.
28     @Input() callNums: IdlObject[];
29
30     // If true, just ask the server to delete all attached copies
31     // for any deleted call numbers.
32     // Note if this is true and a call number is provided that does not
33     // contain its fleshed copies, the number of copies to delete will not be
34     // reported correctly.
35     @Input() forceDeleteCopies: boolean;
36
37     numCallNums: number;
38     numCopies: number;
39     numSucceeded: number;
40     numFailed: number;
41
42     @ViewChild('successMsg')
43         private successMsg: StringComponent;
44
45     @ViewChild('errorMsg')
46         private errorMsg: StringComponent;
47
48     constructor(
49         private modal: NgbModal, // required for passing to parent
50         private toast: ToastService,
51         private net: NetService,
52         private pcrud: PcrudService,
53         private evt: EventService,
54         private renderer: Renderer2,
55         private auth: AuthService) {
56         super(modal); // required for subclassing
57     }
58
59     ngOnInit() {}
60
61     open(args: NgbModalOptions): Observable<boolean> {
62         this.numCallNums = 0;
63         this.numCopies = 0;
64         this.numSucceeded = 0;
65         this.numFailed = 0;
66
67         this.callNums.forEach(callNum => {
68             if (callNum.isdeleted()) {
69                 this.numCallNums++;
70             }
71             if (Array.isArray(callNum.copies())) {
72                 callNum.copies().forEach(c => {
73                     if (c.isdeleted() || this.forceDeleteCopies) {
74                         // Marking copies deleted in forceDeleteCopies mode
75                         // is not required, but we do it here so we can
76                         // report the number of copies to be deleted.
77                         c.isdeleted(true);
78                         this.numCopies++;
79                     }
80                 });
81             }
82         });
83
84         if (this.numCallNums === 0 && this.numCopies === 0) {
85             console.debug('Holdings delete called with no usable data');
86             return throwError(false);
87         }
88
89         return super.open(args);
90     }
91
92     deleteHoldings() {
93
94         const flags = {
95             force_delete_copies: this.forceDeleteCopies
96         };
97
98         this.net.request(
99             'open-ils.cat',
100             'open-ils.cat.asset.volume.fleshed.batch.update.override',
101             this.auth.token(), this.callNums, 1, flags
102         ).toPromise().then(
103             result => {
104                 const evt = this.evt.parse(result);
105                 if (evt) {
106                     console.warn(evt);
107                     this.errorMsg.current().then(msg => this.toast.warning(msg));
108                     this.numFailed++;
109                 } else {
110                     this.numSucceeded++;
111                     this.close(this.numSucceeded > 0);
112                 }
113             },
114             err => {
115                 console.warn(err);
116                 this.errorMsg.current().then(msg => this.toast.warning(msg));
117                 this.numFailed++;
118             }
119         );
120     }
121 }
122
123
124