]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/patron/patron.service.ts
9678c4d8c1521132828c9aa4c8b7990c037be466
[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     // Note pcrudOps should be constructed from the perspective
28     // of a user ('au') retrieval, not a barcode ('ac') retrieval.
29     getByBarcode(barcode: string, pcrudOps?: any): Promise<IdlObject> {
30         return this.bcSearch(barcode).toPromise()
31         .then(barcodes => {
32
33             // Use the first successful barcode response.
34             // TODO: What happens when there are multiple responses?
35             // Use for-loop for early exit since we have async
36             // action within the loop.
37             for (let i = 0; i < barcodes.length; i++) {
38                 const bc = barcodes[i];
39                 if (!this.evt.parse(bc)) {
40                     return this.getById(bc.id, pcrudOps);
41                 }
42             }
43
44             return null;
45         });
46     }
47
48     getById(id: number, pcrudOps?: any): Promise<IdlObject> {
49         return this.pcrud.retrieve('au', id, pcrudOps).toPromise();
50     }
51
52 }
53