]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
LP#1996818 Issues Placing Holds from the Patron Record
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / catalog.service.ts
1 import {Injectable, EventEmitter, NgZone} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
7 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
8 import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {PatronService} from '@eg/staff/share/patron/patron.service';
10 import {StoreService} from '@eg/core/store.service';
11 import {BroadcastService} from '@eg/share/util/broadcast.service';
12 import {Observable} from 'rxjs';
13 import {tap} from 'rxjs/operators';
14
15 const HOLD_FOR_PATRON_KEY = 'eg.circ.patron_hold_target';
16
17 /**
18  * Shared bits needed by the staff version of the catalog.
19  */
20
21 @Injectable()
22 export class StaffCatalogService {
23
24     searchContext: CatalogSearchContext;
25     routeIndex = 0;
26     defaultSearchOrg: IdlObject;
27     defaultSearchLimit: number;
28     // Track the current template through route changes.
29     selectedTemplate: string;
30
31     // Display the Exclude Electronic checkbox
32     showExcludeElectronic = false;
33
34     // Advanced search filters to display
35     searchFilters: string[];
36
37     // TODO: does unapi support pref-lib for result-page copy counts?
38     prefOrg: IdlObject;
39
40     // Default search tab
41     defaultTab: string;
42
43     // Patron barcode we hope to place a hold for.
44     holdForBarcode: string;
45     // User object for above barcode.
46     holdForUser: IdlObject;
47
48     // Emit that the value has changed so components can detect
49     // the change even when the component is not itself digesting
50     // new values.
51     holdForChange: EventEmitter<void> = new EventEmitter<void>();
52
53     // Cache the currently selected detail record (i.g. catalog/record/123)
54     // summary so the record detail component can avoid duplicate fetches
55     // during record tab navigation.
56     currentDetailRecordSummary: any;
57
58     // Add digital bookplate to search options.
59     enableBookplates = false;
60
61     // Cache of browse results so the browse pager is not forced to
62     // re-run the browse search on each navigation.
63     browsePagerData: any[];
64
65     // whether to redirect to record page upon a single search
66     // result
67     jumpOnSingleHit = false;
68
69     constructor(
70         private router: Router,
71         private route: ActivatedRoute,
72         private store: StoreService,
73         private org: OrgService,
74         private cat: CatalogService,
75         private patron: PatronService,
76         private catUrl: CatalogUrlService,
77         private broadcaster: BroadcastService,
78         private zone: NgZone
79     ) { }
80
81     createContext(): void {
82         // Initialize the search context from the load-time URL params.
83         // Do this here so the search form and other context data are
84         // applied on every page, not just the search results page.  The
85         // search results pages will handle running the actual search.
86         this.searchContext =
87             this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
88
89         this.holdForBarcode = this.store.getLoginSessionItem(HOLD_FOR_PATRON_KEY);
90
91         if (this.holdForBarcode) {
92             this.patron.getByBarcode(this.holdForBarcode)
93             .then(user => {
94                 this.holdForUser = user;
95                 this.holdForChange.emit();
96             });
97         } else {
98             // In case the session item was cleared from another component.
99             this.clearHoldPatron();
100         }
101
102         this.searchContext.org = this.org; // service, not searchOrg
103         this.searchContext.isStaff = true;
104         this.applySearchDefaults();
105     }
106
107     clearHoldPatron(broadcast: boolean = true) {
108         const removedTarget = this.holdForBarcode;
109
110         this.holdForUser = null;
111         this.holdForBarcode = null;
112         this.store.removeLoginSessionItem(HOLD_FOR_PATRON_KEY);
113         this.holdForChange.emit();
114         if (!broadcast) return;
115
116         // clear hold patron on other tabs
117         this.broadcaster.broadcast(
118             HOLD_FOR_PATRON_KEY, { removedTarget }
119         );
120     }
121
122     onBeforeUnload(): void {
123         const closedTarget = this.holdForBarcode;
124         if (closedTarget) {
125             this.clearHoldPatron(false);
126             this.broadcaster.broadcast(HOLD_FOR_PATRON_KEY,
127                 { closedTarget }
128             );
129         }
130     }
131
132     onChangeHoldPatron(): Observable<any> {
133         return this.broadcaster.listen(HOLD_FOR_PATRON_KEY).pipe(
134             tap(({ removedTarget, closedTarget }) => {
135                 if (removedTarget && this.holdForBarcode) {
136                     // broadcaster doesn't trigger change detection,
137                     // so trigger it manually
138                     this.zone.run(() => this.clearHoldPatron(false));
139
140                 } else if (closedTarget) {
141                     // if hold target was unset by another tab,
142                     // restore the hold target
143                     if (closedTarget === this.holdForBarcode) {
144                         this.store.setLoginSessionItem(
145                             HOLD_FOR_PATRON_KEY, closedTarget
146                         );
147                     }
148                 }
149             })
150         );
151     }
152
153     cloneContext(context: CatalogSearchContext): CatalogSearchContext {
154         const params: any = this.catUrl.toUrlParams(context);
155         const ctx = this.catUrl.fromUrlHash(params);
156         ctx.isStaff = true; // not carried in the URL
157         return ctx;
158     }
159
160     applySearchDefaults(): void {
161         if (!this.searchContext.searchOrg) {
162             this.searchContext.searchOrg =
163                 this.defaultSearchOrg || this.org.root();
164         }
165
166         if (!this.searchContext.pager.limit) {
167             this.searchContext.pager.limit = this.defaultSearchLimit || 10;
168         }
169     }
170
171     /**
172      * Redirect to the search results page while propagating the current
173      * search paramters into the URL.  Let the search results component
174      * execute the actual search.
175      */
176     search(): void {
177         if (!this.searchContext.isSearchable()) { return; }
178
179         // Clear cached detail summary for new searches.
180         this.currentDetailRecordSummary = null;
181
182         const params = this.catUrl.toUrlParams(this.searchContext);
183
184         // Force a new search every time this method is called, even if
185         // it's the same as the active search.  Since router navigation
186         // exits early when the route + params is identical, add a
187         // random token to the route params to force a full navigation.
188         // This also resolves a problem where only removing secondary+
189         // versions of a query param fail to cause a route navigation.
190         // (E.g. going from two query= params to one).  Investigation
191         // pending.
192         params.ridx = '' + this.routeIndex++;
193
194         this.router.navigate(
195           ['/staff/catalog/search'], {queryParams: params});
196     }
197
198     /**
199      * Redirect to the browse results page while propagating the current
200      * browse paramters into the URL.  Let the browse results component
201      * execute the actual browse.
202      */
203     browse(): void {
204         if (!this.searchContext.browseSearch.isSearchable()) { return; }
205         const params = this.catUrl.toUrlParams(this.searchContext);
206
207         // Force a new browse every time this method is called, even if
208         // it's the same as the active browse.  Since router navigation
209         // exits early when the route + params is identical, add a
210         // random token to the route params to force a full navigation.
211         // This also resolves a problem where only removing secondary+
212         // versions of a query param fail to cause a route navigation.
213         // (E.g. going from two query= params to one).
214         params.ridx = '' + this.routeIndex++;
215
216         this.router.navigate(
217             ['/staff/catalog/browse'], {queryParams: params});
218     }
219
220     // Call number browse.
221     // Redirect to cn browse page and let its component perform the search
222     cnBrowse(): void {
223         if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
224         const params = this.catUrl.toUrlParams(this.searchContext);
225         params.ridx = '' + this.routeIndex++; // see comments above
226         this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
227     }
228
229     // Params to genreate a new author search based on a reset
230     // clone of the current page params.
231     getAuthorSearchParams(summary: BibRecordSummary): any {
232         const tmpContext = this.cloneContext(this.searchContext);
233         tmpContext.reset();
234         tmpContext.termSearch.fieldClass = ['author'];
235         tmpContext.termSearch.query = [summary.display.author];
236         return this.catUrl.toUrlParams(tmpContext);
237     }
238 }
239
240