LP1865898 Scan Item as Missing Pieces Angular Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / holdings.service.ts
1 /**
2  * Common code for mananging holdings
3  */
4 import {Injectable, EventEmitter} from '@angular/core';
5 import {NetService} from '@eg/core/net.service';
6 import {AnonCacheService} from '@eg/share/util/anon-cache.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {IdlObject} from '@eg/core/idl.service';
10 import {EventService} from '@eg/core/event.service';
11
12 interface NewCallNumData {
13     owner?: number;
14     label?: string;
15     fast_add?: boolean;
16     barcode?: string;
17 }
18
19 @Injectable()
20 export class HoldingsService {
21
22     constructor(
23         private net: NetService,
24         private auth: AuthService,
25         private pcrud: PcrudService,
26         private evt: EventService,
27         private anonCache: AnonCacheService
28     ) {}
29
30     // Open the holdings editor UI in a new browser window/tab.
31     spawnAddHoldingsUi(
32         recordId: number,                  // Bib record ID
33         editExistingCallNums?: number[],   // Add copies to / modify existing CNs
34         newCallNumData?: NewCallNumData[], // Creating new call numbers
35         editCopyIds?: number[],            // Edit existing items
36         hideCopies?: boolean,              // Hide the copy edit pane
37         hideVols?: boolean) {
38
39         const raw: any[] = [];
40
41         if (editExistingCallNums) {
42             editExistingCallNums.forEach(
43                 callNumId => raw.push({callnumber: callNumId}));
44         } else if (newCallNumData) {
45             newCallNumData.forEach(data => raw.push(data));
46         }
47
48         this.anonCache.setItem(null, 'edit-these-copies', {
49             record_id: recordId,
50             raw: raw,
51             copies: editCopyIds,
52             hide_vols : hideVols === true,
53             hide_copies : hideCopies === true
54         }).then(key => {
55             if (!key) {
56                 console.error('Could not create holds cache key!');
57                 return;
58             }
59             setTimeout(() => {
60                 const url = `/eg/staff/cat/volcopy/${key}`;
61                 window.open(url, '_blank');
62             });
63         });
64     }
65
66     // Using open-ils.actor.get_barcodes
67     getItemIdFromBarcode(barcode: string): Promise<number> {
68         return this.net.request(
69             'open-ils.actor',
70             'open-ils.actor.get_barcodes',
71             this.auth.token(), this.auth.user().ws_ou(), 'asset', barcode
72         ).toPromise().then(resp => {
73             if (this.evt.parse(resp)) {
74                 return Promise.reject(resp);
75             } else if (resp.length === 0) {
76                 return null;
77             } else {
78                 return resp[0].id;
79             }
80         });
81     }
82 }
83