]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/mark-missing-dialog.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / mark-missing-dialog.component.ts
1 import {Component, Input, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {NetService} from '@eg/core/net.service';
4 import {EventService} from '@eg/core/event.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {DialogComponent} from '@eg/share/dialog/dialog.component';
8 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
9 import {StringComponent} from '@eg/share/string/string.component';
10
11
12 /**
13  * Dialog for marking items missing.
14  */
15
16 @Component({
17     selector: 'eg-mark-missing-dialog',
18     templateUrl: 'mark-missing-dialog.component.html'
19 })
20
21 export class MarkMissingDialogComponent
22     extends DialogComponent {
23
24     @Input() copyIds: number[];
25
26     numSucceeded: number;
27     numFailed: number;
28
29     @ViewChild('successMsg', { static: true })
30     private successMsg: StringComponent;
31
32     @ViewChild('errorMsg', { static: true })
33     private errorMsg: StringComponent;
34
35     constructor(
36         private modal: NgbModal, // required for passing to parent
37         private toast: ToastService,
38         private net: NetService,
39         private evt: EventService,
40         private auth: AuthService) {
41         super(modal); // required for subclassing
42     }
43
44     open(args: NgbModalOptions): Observable<boolean> {
45         this.numSucceeded = 0;
46         this.numFailed = 0;
47         return super.open(args);
48     }
49
50     async markOneItemMissing(ids: number[]): Promise<any> {
51         if (ids.length === 0) {
52             return Promise.resolve();
53         }
54
55         const id = ids.pop();
56
57         return this.net.request(
58             'open-ils.circ',
59             'open-ils.circ.mark_item_missing',
60             this.auth.token(), id
61         ).toPromise().then(async(result) => {
62             if (Number(result) === 1) {
63                 this.numSucceeded++;
64                 this.toast.success(await this.successMsg.current());
65             } else {
66                 this.numFailed++;
67                 console.error('Mark missing failed ', this.evt.parse(result));
68                 this.toast.warning(await this.errorMsg.current());
69             }
70             return this.markOneItemMissing(ids);
71         });
72     }
73
74     async markItemsMissing(): Promise<any> {
75         this.numSucceeded = 0;
76         this.numFailed = 0;
77         const ids = [].concat(this.copyIds);
78         await this.markOneItemMissing(ids);
79         this.close(this.numSucceeded > 0);
80     }
81 }
82
83
84