]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/holdings.service.ts
LP2045292 Color contrast for AngularJS patron bills
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / holdings.service.ts
1 /* eslint-disable no-magic-numbers */
2 /**
3  * Common code for mananging holdings
4  */
5 import {Injectable} from '@angular/core';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {tap} from 'rxjs/operators';
8 import {NetService} from '@eg/core/net.service';
9 import {AnonCacheService} from '@eg/share/util/anon-cache.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {EventService} from '@eg/core/event.service';
13
14 export interface CallNumData {
15     owner?: number;
16     label?: string;
17     fast_add?: boolean;
18     barcode?: string;
19     callnumber?: number;
20 }
21
22 @Injectable()
23 export class HoldingsService {
24
25     copyStatuses: {[id: number]: IdlObject};
26
27     constructor(
28         private net: NetService,
29         private auth: AuthService,
30         private pcrud: PcrudService,
31         private evt: EventService,
32         private anonCache: AnonCacheService
33     ) {}
34
35     // Open the holdings editor UI in a new browser window/tab.
36     spawnAddHoldingsUi(
37         recordId: number,                  // Bib record ID
38         editExistingCallNums?: number[],   // Add copies to / modify existing CNs
39         newCallNumData?: CallNumData[],    // Creating new call numbers
40         editCopyIds?: number[],            // Edit existing items
41         hideCopies?: boolean,              // Hide the copy edit pane
42         hideVols?: boolean) {
43
44         const raw: any[] = [];
45
46         if (editExistingCallNums) {
47             editExistingCallNums.forEach(
48                 callNumId => raw.push({callnumber: callNumId}));
49         } else if (newCallNumData) {
50             newCallNumData.forEach(data => raw.push(data));
51         }
52
53         this.anonCache.setItem(null, 'edit-these-copies', {
54             record_id: recordId,
55             raw: raw,
56             copies: editCopyIds,
57             hide_vols : hideVols === true,
58             hide_copies : hideCopies === true
59         }).then(key => {
60             if (!key) {
61                 console.error('Could not create holds cache key!');
62                 return;
63             }
64             setTimeout(() => {
65                 const tab = hideVols ? 'attrs' : 'holdings';
66                 const url = `/eg2/staff/cat/volcopy/${tab}/session/${key}`;
67                 window.open(url, '_blank');
68             });
69         });
70     }
71
72     // Using open-ils.actor.get_barcodes
73     getItemIdFromBarcode(barcode: string): Promise<number> {
74         return this.net.request(
75             'open-ils.actor',
76             'open-ils.actor.get_barcodes',
77             this.auth.token(), this.auth.user().ws_ou(), 'asset', barcode
78         ).toPromise().then(resp => {
79             if (this.evt.parse(resp)) {
80                 return Promise.reject(resp);
81             } else if (resp.length === 0) {
82                 return null;
83             } else {
84                 return resp[0].id;
85             }
86         });
87     }
88
89     /* TODO: make these more configurable per lp1616170 */
90     getMagicCopyStatuses(): Promise<number[]> {
91         return Promise.resolve([
92             1,  // Checked out
93             3,  // Lost
94             6,  // In transit
95             8,  // On holds shelf
96             16, // Long overdue
97             18  // Canceled Transit
98         ]);
99     }
100
101     getCopyStatuses(): Promise<{[id: number]: IdlObject}> {
102         if (this.copyStatuses) {
103             return Promise.resolve(this.copyStatuses);
104         }
105
106         this.copyStatuses = {};
107         return this.pcrud.retrieveAll('ccs', {order_by: {ccs: 'name'}})
108             .pipe(tap(stat => this.copyStatuses[stat.id()] = stat))
109             .toPromise().then(_ => this.copyStatuses);
110     }
111 }
112