]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/booking/make-bookable-dialog.component.ts
LP1821382 Make items bookable (part 2)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / booking / make-bookable-dialog.component.ts
1 import {Component, OnInit, OnDestroy, Input, ViewChild} from '@angular/core';
2 import {Subscription} 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} 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  * Dialog for making items bookable
15  */
16
17 @Component({
18   selector: 'eg-make-bookable-dialog',
19   templateUrl: 'make-bookable-dialog.component.html'
20 })
21 export class MakeBookableDialogComponent
22     extends DialogComponent implements OnInit, OnDestroy {
23
24     // Note copyIds must refer to copies that belong to a single
25     // bib record.
26     @Input() copyIds: number[];
27     copies: IdlObject[];
28
29     numSucceeded: number;
30     numFailed: number;
31     updateComplete: boolean;
32     newResourceType: number;
33     newResourceOrg: number;
34
35     onOpenSub: Subscription;
36
37     @ViewChild('successMsg') private successMsg: StringComponent;
38     @ViewChild('errorMsg') private errorMsg: StringComponent;
39
40     constructor(
41         private modal: NgbModal, // required for passing to parent
42         private toast: ToastService,
43         private net: NetService,
44         private pcrud: PcrudService,
45         private evt: EventService,
46         private auth: AuthService) {
47         super(modal); // required for subclassing
48     }
49
50     ngOnInit() {
51         this.onOpenSub = this.onOpen$.subscribe(async () => {
52             this.numSucceeded = 0;
53             this.numFailed = 0;
54             this.updateComplete = false;
55         });
56     }
57
58     ngOnDestroy() {
59         this.onOpenSub.unsubscribe();
60     }
61
62     manageUrlParams(): any {
63         if (this.newResourceOrg) {
64             return {
65                 gridFilters: JSON.stringify({type: this.newResourceType}),
66                 contextOrg: this.newResourceOrg
67             };
68         }
69     }
70
71     makeBookable() {
72         this.newResourceType = null;
73
74         this.net.request(
75             'open-ils.booking',
76             'open-ils.booking.resources.create_from_copies',
77             this.auth.token(), this.copyIds
78         ).toPromise().then(
79             resp => {
80                 // resp.brsrc = [[brsrc.id, acp.id, existed], ...]
81                 // resp.brt = [[brt.id, brt.peer_record, existed], ...]
82                 const evt = this.evt.parse(resp);
83                 if (evt) { return Promise.reject(evt); }
84                 this.numSucceeded = resp.brsrc.length;
85                 this.newResourceType = resp.brt[0][0]; // new resource ID
86                 this.updateComplete = true;
87                 this.successMsg.current().then(msg => this.toast.success(msg));
88             },
89             err => Promise.reject(err)
90         ).then(
91             ok => {
92                 // Once resource creation is complete, grab the call number
93                 // for the first copy to get the owning library
94                 this.pcrud.retrieve('acp', this.copyIds[0],
95                     {flesh: 1, flesh_fields: {acp: ['call_number']}})
96                 .toPromise().then(copy => {
97                     this.newResourceOrg = copy.call_number().owning_lib();
98                     this.updateComplete = true;
99                 });
100             },
101             err => {
102                 console.error(err);
103                 this.numFailed++;
104                 this.errorMsg.current().then(msg => this.toast.danger(msg));
105                 this.updateComplete = true;
106             }
107         );
108     }
109 }
110
111
112