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 {EgEvent, 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 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
16 * Dialog that confirms, then deletes items and call numbers
20 selector: 'eg-delete-holding-dialog',
21 templateUrl: 'delete-volcopy-dialog.component.html'
24 export class DeleteHoldingDialogComponent
25 extends DialogComponent implements OnInit {
27 // List of "acn" objects which may contain copies.
28 // Objects of either type marked "isdeleted" will be deleted.
29 @Input() callNums: IdlObject[];
31 // If true, just ask the server to delete all attached copies
32 // for any deleted call numbers.
33 // Note if this is true and a call number is provided that does not
34 // contain its fleshed copies, the number of copies to delete will not be
35 // reported correctly.
36 @Input() forceDeleteCopies: boolean;
42 deleteEventDesc: string;
44 @ViewChild('successMsg', { static: true })
45 private successMsg: StringComponent;
47 @ViewChild('errorMsg', { static: true })
48 private errorMsg: StringComponent;
50 @ViewChild('confirmOverride', {static: false})
51 private confirmOverride: ConfirmDialogComponent;
54 private modal: NgbModal, // required for passing to parent
55 private toast: ToastService,
56 private net: NetService,
57 private pcrud: PcrudService,
58 private evt: EventService,
59 private renderer: Renderer2,
60 private auth: AuthService) {
61 super(modal); // required for subclassing
66 open(args: NgbModalOptions): Observable<boolean> {
69 this.numSucceeded = 0;
72 this.callNums.forEach(callNum => {
73 if (callNum.isdeleted()) {
76 if (Array.isArray(callNum.copies())) {
77 callNum.copies().forEach(c => {
78 if (c.isdeleted() || this.forceDeleteCopies) {
79 // Marking copies deleted in forceDeleteCopies mode
80 // is not required, but we do it here so we can
81 // report the number of copies to be deleted.
89 if (this.numCallNums === 0 && this.numCopies === 0) {
90 console.debug('Holdings delete called with no usable data');
91 return throwError(false);
94 return super.open(args);
97 deleteHoldings(override?: boolean) {
99 this.deleteEventDesc = '';
102 force_delete_copies: this.forceDeleteCopies
105 let method = 'open-ils.cat.asset.volume.fleshed.batch.update';
107 method = `${method}.override`;
108 flags.events = ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'];
112 'open-ils.cat', method,
113 this.auth.token(), this.callNums, 1, flags
116 const evt = this.evt.parse(result);
118 this.handleDeleteEvent(evt, override);
121 this.close(this.numSucceeded > 0);
126 this.errorMsg.current().then(msg => this.toast.warning(msg));
132 handleDeleteEvent(evt: EgEvent, override?: boolean): Promise<any> {
134 if (override) { // override failed
137 return this.errorMsg.current().then(msg => this.toast.warning(msg));
140 this.deleteEventDesc = evt.desc;
142 return this.confirmOverride.open().toPromise().then(confirmed => {
144 return this.deleteHoldings(true);
147 // User canceled the delete confirmation dialog
149 this.errorMsg.current().then(msg => this.toast.warning(msg));
150 this.close(this.numSucceeded > 0);