]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/booking/reservation-actions.service.ts
LP1851306: Port Capture Booking Resource to Angular
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / reservation-actions.service.ts
1 import {Injectable} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {Observable, of} from 'rxjs';
4 import {mergeMap, switchMap, tap} from 'rxjs/operators';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PrintService} from '@eg/share/print/print.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9
10 // Some grid actions that are shared across booking grids
11
12 export interface CaptureInformation {
13     captured: number;
14     reservation: IdlObject;
15     mvr?: IdlObject;
16     new_copy_status?: number;
17     transit?: IdlObject;
18     resource?: IdlObject;
19     type?: IdlObject;
20     staff?: IdlObject;
21     workstation?: string;
22 }
23
24 @Injectable({providedIn: 'root'})
25 export class ReservationActionsService {
26
27     constructor(
28         private auth: AuthService,
29         private pcrud: PcrudService,
30         private printer: PrintService,
31         private router: Router,
32     ) {
33     }
34
35     manageReservationsByResource = (barcode: string) => {
36         this.router.navigate(['/staff', 'booking', 'manage_reservations', 'by_resource', barcode]);
37     }
38
39     printCaptureSlip = (templateData: CaptureInformation) => {
40         templateData.staff = this.auth.user();
41         templateData.workstation = this.auth.workstation();
42         this.printer.print({
43             templateName: 'booking_capture',
44             contextData: templateData,
45             printContext: 'receipt'
46         });
47     }
48
49     reprintCaptureSlip = (ids: number[]): Observable<CaptureInformation> => {
50         return this.fetchDataForCaptureSlip$(ids)
51         .pipe(tap((data) => this.printCaptureSlip(data)));
52     }
53
54     viewItemStatus = (barcode: string) => {
55         this.pcrud.search('acp', { 'barcode': barcode }, { limit: 1 })
56         .subscribe((acp) => {
57             window.open('/eg/staff/cat/item/' + acp.id());
58         });
59     }
60
61     notOneUniqueSelected = (ids: number[]) => {
62         return (new Set(ids).size !== 1);
63     }
64
65     private fetchDataForCaptureSlip$ = (ids: number[]): Observable<CaptureInformation> => {
66         return this.pcrud.search('bresv', {'id': ids}, {
67             flesh: 2,
68             flesh_fields : {
69                 'bresv': ['usr', 'current_resource', 'type'],
70                 'au': ['card'],
71                 'brsrc': ['type']
72             }
73         }).pipe(mergeMap((reservation: IdlObject) => this.assembleDataForCaptureSlip$(reservation)));
74     }
75
76     private assembleDataForCaptureSlip$ = (reservation: IdlObject): Observable<CaptureInformation> => {
77         let observable$ = of({
78             reservation: reservation,
79             captured: 1
80         });
81         if (reservation.pickup_lib() === this.auth.user().ws_ou()) {
82             observable$ = this.pcrud.search('artc', {'reservation': reservation.id()}, {limit: 1})
83             .pipe(switchMap(transit => {
84                 return of({
85                     reservation: reservation,
86                     captured: 1,
87                     transit: transit
88                 });
89             }));
90         }
91         return observable$;
92     }
93
94 }
95