LP1869898 Angular staff cat place hold from patron
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / patron / patron.service.ts
1 import {Injectable} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {EventService} from '@eg/core/event.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {Observable} from 'rxjs';
8
9
10 @Injectable()
11 export class PatronService {
12     constructor(
13         private net: NetService,
14         private evt: EventService,
15         private pcrud: PcrudService,
16         private auth: AuthService
17     ) {}
18
19     bcSearch(barcode: string): Observable<any> {
20         return this.net.request(
21             'open-ils.actor',
22             'open-ils.actor.get_barcodes',
23             this.auth.token(), this.auth.user().ws_ou(),
24            'actor', barcode.trim());
25     }
26
27     getByBarcode(barcode: string, pcrudOps?: any): Promise<IdlObject> {
28         return this.bcSearch(barcode).toPromise()
29         .then(barcodes => {
30
31             // Use the first successful barcode response.
32             // TODO: What happens when there are multiple responses?
33             // Use for-loop for early exit since we have async
34             // action within the loop.
35             for (let i = 0; i < barcodes.length; i++) {
36                 const bc = barcodes[i];
37                 if (!this.evt.parse(bc)) {
38                     return this.getById(bc.id);
39                 }
40             }
41
42             return null;
43         });
44     }
45
46     getById(id: number, pcrudOps?: any): Promise<IdlObject> {
47         return this.pcrud.retrieve('au', id, pcrudOps).toPromise();
48     }
49
50 }
51