]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/booking/make-bookable-dialog.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[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', { static: true }) private successMsg: StringComponent;
38     @ViewChild('errorMsg', { static: true }) 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         // eslint-disable-next-line rxjs/no-async-subscribe
52         this.onOpenSub = this.onOpen$.subscribe(async () => {
53             this.numSucceeded = 0;
54             this.numFailed = 0;
55             this.updateComplete = false;
56         });
57     }
58
59     ngOnDestroy() {
60         this.onOpenSub.unsubscribe();
61     }
62
63     manageUrlParams(): any {
64         if (this.newResourceOrg) {
65             return {
66                 gridFilters: JSON.stringify({type: this.newResourceType}),
67                 contextOrg: this.newResourceOrg
68             };
69         }
70     }
71
72     makeBookable() {
73         this.newResourceType = null;
74
75         this.net.request(
76             'open-ils.booking',
77             'open-ils.booking.resources.create_from_copies',
78             this.auth.token(), this.copyIds
79         ).toPromise().then(
80             resp => {
81                 // resp.brsrc = [[brsrc.id, acp.id, existed], ...]
82                 // resp.brt = [[brt.id, brt.peer_record, existed], ...]
83                 const evt = this.evt.parse(resp);
84                 if (evt) { return Promise.reject(evt); }
85                 this.numSucceeded = resp.brsrc.length;
86                 this.newResourceType = resp.brt[0][0]; // new resource ID
87                 this.updateComplete = true;
88                 this.successMsg.current().then(msg => this.toast.success(msg));
89             },
90             err => Promise.reject(err)
91         ).then(
92             ok => {
93                 // Once resource creation is complete, grab the call number
94                 // for the first copy to get the owning library
95                 this.pcrud.retrieve('acp', this.copyIds[0],
96                     {flesh: 1, flesh_fields: {acp: ['call_number']}})
97                     .toPromise().then(copy => {
98                         this.newResourceOrg = copy.call_number().owning_lib();
99                         this.updateComplete = true;
100                     });
101             },
102             err => {
103                 console.error(err);
104                 this.numFailed++;
105                 this.errorMsg.current().then(msg => this.toast.danger(msg));
106                 this.updateComplete = true;
107             }
108         );
109     }
110 }
111
112
113