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';
11 export class PatronService {
13 private net: NetService,
14 private evt: EventService,
15 private pcrud: PcrudService,
16 private auth: AuthService
19 bcSearch(barcode: string): Observable<any> {
20 return this.net.request(
22 'open-ils.actor.get_barcodes',
23 this.auth.token(), this.auth.user().ws_ou(),
24 'actor', barcode.trim());
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()
32 if (!barcodes) { return null; }
34 // Use the first successful barcode response.
35 // TODO: What happens when there are multiple responses?
36 // Use for-loop for early exit since we have async
37 // action within the loop.
38 for (let i = 0; i < barcodes.length; i++) {
39 const bc = barcodes[i];
40 if (!this.evt.parse(bc)) {
41 return this.getById(bc.id, pcrudOps);
49 getById(id: number, pcrudOps?: any): Promise<IdlObject> {
50 return this.pcrud.retrieve('au', id, pcrudOps).toPromise();