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