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