LP1821382 Make items bookable (part 2)
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / share / booking / make-bookable-dialog.component.ts
index 84d7941..9df3f9f 100644 (file)
@@ -1,5 +1,4 @@
-import {Component, OnInit, OnDestroy, Input, ViewChild,
-        Renderer2} from '@angular/core';
+import {Component, OnInit, OnDestroy, Input, ViewChild} from '@angular/core';
 import {Subscription} from 'rxjs';
 import {IdlObject} from '@eg/core/idl.service';
 import {NetService} from '@eg/core/net.service';
@@ -7,7 +6,7 @@ import {EventService} from '@eg/core/event.service';
 import {PcrudService} from '@eg/core/pcrud.service';
 import {ToastService} from '@eg/share/toast/toast.service';
 import {AuthService} from '@eg/core/auth.service';
-import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
+import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
 import {DialogComponent} from '@eg/share/dialog/dialog.component';
 import {StringComponent} from '@eg/share/string/string.component';
 
@@ -30,6 +29,8 @@ export class MakeBookableDialogComponent
     numSucceeded: number;
     numFailed: number;
     updateComplete: boolean;
+    newResourceType: number;
+    newResourceOrg: number;
 
     onOpenSub: Subscription;
 
@@ -42,7 +43,6 @@ export class MakeBookableDialogComponent
         private net: NetService,
         private pcrud: PcrudService,
         private evt: EventService,
-        private renderer: Renderer2,
         private auth: AuthService) {
         super(modal); // required for subclassing
     }
@@ -58,6 +58,54 @@ export class MakeBookableDialogComponent
     ngOnDestroy() {
         this.onOpenSub.unsubscribe();
     }
+
+    manageUrlParams(): any {
+        if (this.newResourceOrg) {
+            return {
+                gridFilters: JSON.stringify({type: this.newResourceType}),
+                contextOrg: this.newResourceOrg
+            };
+        }
+    }
+
+    makeBookable() {
+        this.newResourceType = null;
+
+        this.net.request(
+            'open-ils.booking',
+            'open-ils.booking.resources.create_from_copies',
+            this.auth.token(), this.copyIds
+        ).toPromise().then(
+            resp => {
+                // resp.brsrc = [[brsrc.id, acp.id, existed], ...]
+                // resp.brt = [[brt.id, brt.peer_record, existed], ...]
+                const evt = this.evt.parse(resp);
+                if (evt) { return Promise.reject(evt); }
+                this.numSucceeded = resp.brsrc.length;
+                this.newResourceType = resp.brt[0][0]; // new resource ID
+                this.updateComplete = true;
+                this.successMsg.current().then(msg => this.toast.success(msg));
+            },
+            err => Promise.reject(err)
+        ).then(
+            ok => {
+                // Once resource creation is complete, grab the call number
+                // for the first copy to get the owning library
+                this.pcrud.retrieve('acp', this.copyIds[0],
+                    {flesh: 1, flesh_fields: {acp: ['call_number']}})
+                .toPromise().then(copy => {
+                    this.newResourceOrg = copy.call_number().owning_lib();
+                    this.updateComplete = true;
+                });
+            },
+            err => {
+                console.error(err);
+                this.numFailed++;
+                this.errorMsg.current().then(msg => this.toast.danger(msg));
+                this.updateComplete = true;
+            }
+        );
+    }
 }