1 import {Component, OnInit, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {PermService} from '@eg/core/perm.service';
8 import {IdlObject} from '@eg/core/idl.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
11 import {CatalogService} from '@eg/share/catalog/catalog.service';
12 import {StaffCatalogService} from '../catalog.service';
13 import {HoldsService, HoldRequest,
14 HoldRequestTarget} from '@eg/staff/share/holds/holds.service';
15 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
16 import {PatronService} from '@eg/staff/share/patron/patron.service';
17 import {PatronSearchDialogComponent
18 } from '@eg/staff/share/patron/search-dialog.component';
21 holdMeta: HoldRequestTarget;
23 lastRequest: HoldRequest;
24 canOverride?: boolean;
28 constructor(target: number) {
29 this.holdTarget = target;
30 this.processing = false;
31 this.selectedFormats = {
32 // code => selected-boolean
40 templateUrl: 'hold.component.html'
42 export class HoldComponent implements OnInit {
45 holdTargets: number[];
60 holdContexts: HoldContext[];
61 recordSummaries: BibRecordSummary[];
63 currentUserBarcode: string;
64 smsCarriers: ComboboxEntry[];
67 placeHoldsClicked: boolean;
69 @ViewChild('patronSearch', {static: false})
70 patronSearch: PatronSearchDialogComponent;
73 private router: Router,
74 private route: ActivatedRoute,
75 private renderer: Renderer2,
76 private evt: EventService,
77 private net: NetService,
78 private org: OrgService,
79 private auth: AuthService,
80 private pcrud: PcrudService,
81 private bib: BibRecordService,
82 private cat: CatalogService,
83 private staffCat: StaffCatalogService,
84 private holds: HoldsService,
85 private patron: PatronService,
86 private perm: PermService
88 this.holdContexts = [];
89 this.smsCarriers = [];
94 // Respond to changes in hold type. This currently assumes hold
95 // types only toggle post-init between copy-level types (C,R,F)
96 // and no other params (e.g. target) change with it. If other
97 // types require tracking, additional data collection may be needed.
98 this.route.paramMap.subscribe(
99 (params: ParamMap) => this.holdType = params.get('type'));
101 this.holdType = this.route.snapshot.params['type'];
102 this.holdTargets = this.route.snapshot.queryParams['target'];
103 this.holdFor = this.route.snapshot.queryParams['holdFor'] || 'patron';
105 if (this.staffCat.holdForBarcode) {
106 this.holdFor = 'patron';
107 this.userBarcode = this.staffCat.holdForBarcode;
110 if (!Array.isArray(this.holdTargets)) {
111 this.holdTargets = [this.holdTargets];
114 this.holdTargets = this.holdTargets.map(t => Number(t));
116 this.requestor = this.auth.user();
117 this.pickupLib = this.auth.user().ws_ou();
119 this.holdContexts = this.holdTargets.map(target => {
120 const ctx = new HoldContext(target);
124 if (this.holdFor === 'staff' || this.userBarcode) {
125 this.holdForChanged();
128 this.getTargetMeta();
130 this.org.settings('sms.enable').then(sets => {
131 this.smsEnabled = sets['sms.enable'];
132 if (!this.smsEnabled) { return; }
134 this.pcrud.search('csc', {active: 't'}, {order_by: {csc: 'name'}})
135 .subscribe(carrier => {
136 this.smsCarriers.push({
138 label: carrier.name()
143 setTimeout(() => // Focus barcode input
144 this.renderer.selectRootElement('#patron-barcode').focus());
147 // Load the bib, call number, copy, etc. data associated with each target.
149 this.holds.getHoldTargetMeta(this.holdType, this.holdTargets)
151 this.holdContexts.filter(ctx => ctx.holdTarget === meta.target)
154 this.mrFiltersToSelectors(ctx);
159 // By default, all metarecord filters options are enabled.
160 mrFiltersToSelectors(ctx: HoldContext) {
161 if (this.holdType !== 'M') { return; }
163 const meta = ctx.holdMeta;
164 if (meta.metarecord_filters) {
165 if (meta.metarecord_filters.formats) {
166 meta.metarecord_filters.formats.forEach(
167 ccvm => ctx.selectedFormats.formats[ccvm.code()] = true);
169 if (meta.metarecord_filters.langs) {
170 meta.metarecord_filters.langs.forEach(
171 ccvm => ctx.selectedFormats.langs[ccvm.code()] = true);
176 // Map the selected metarecord filters optoins to a JSON-encoded
177 // list of attr filters as required by the API.
178 // Compiles a blob of
179 // {target: JSON({"0": [{_attr: ctype, _val: code}, ...], "1": [...]})}
180 // TODO: this should live in the hold service, not in the UI code.
181 mrSelectorsToFilters(ctx: HoldContext): {[target: number]: string} {
183 const meta = ctx.holdMeta;
184 const slf = ctx.selectedFormats;
185 const result: any = {};
187 const formats = Object.keys(slf.formats)
188 .filter(code => Boolean(slf.formats[code])); // user-selected
190 const langs = Object.keys(slf.langs)
191 .filter(code => Boolean(slf.langs[code])); // user-selected
193 const compiled: any = {};
195 if (formats.length > 0) {
197 formats.forEach(code => {
198 const ccvm = meta.metarecord_filters.formats.filter(
199 format => format.code() === code)[0];
207 if (langs.length > 0) {
209 langs.forEach(code => {
210 const ccvm = meta.metarecord_filters.langs.filter(
211 format => format.code() === code)[0];
219 if (Object.keys(compiled).length > 0) {
221 res[ctx.holdTarget] = JSON.stringify(compiled);
231 if (this.holdFor === 'patron') {
232 if (this.userBarcode) {
233 this.userBarcodeChanged();
236 // To bypass the dupe check.
237 this.currentUserBarcode = '_' + this.requestor.id();
238 this.getUser(this.requestor.id());
242 activeDateSelected(dateStr: string) {
243 this.activeDate = dateStr;
246 userBarcodeChanged() {
248 // Avoid simultaneous or duplicate lookups
249 if (this.userBarcode === this.currentUserBarcode) {
255 if (!this.userBarcode) {
261 this.currentUserBarcode = this.userBarcode;
265 getUser(id?: number) {
266 const flesh = {flesh: 1, flesh_fields: {au: ['settings']}};
268 const promise = id ? this.patron.getById(id, flesh) :
269 this.patron.getByBarcode(this.userBarcode, flesh);
271 promise.then(user => {
273 this.applyUserSettings();
278 this.notifyEmail = true;
279 this.notifyPhone = true;
280 this.phoneValue = '';
281 this.pickupLib = this.requestor.ws_ou();
284 applyUserSettings() {
285 if (!this.user || !this.user.settings()) { return; }
287 // Start with defaults.
288 this.phoneValue = this.user.day_phone() || this.user.evening_phone();
290 // Default to work org if placing holds for staff.
291 if (this.user.id() !== this.requestor.id()) {
292 this.pickupLib = this.user.home_ou();
295 this.user.settings().forEach(setting => {
296 const name = setting.name();
297 let value = setting.value();
299 if (value === '' || value === null) { return; }
301 // When fleshing 'settings' on the actor.usr object,
302 // we're grabbing the raw JSON values.
303 value = JSON.parse(value);
306 case 'opac.hold_notify':
307 this.notifyPhone = Boolean(value.match(/phone/));
308 this.notifyEmail = Boolean(value.match(/email/));
309 this.notifySms = Boolean(value.match(/sms/));
312 case 'opac.default_pickup_location':
313 this.pickupLib = Number(value);
318 if (!this.user.email()) {
319 this.notifyEmail = false;
322 if (!this.phoneValue) {
323 this.notifyPhone = false;
327 // Attempt hold placement on all targets
328 placeHolds(idx?: number) {
329 if (!idx) { idx = 0; }
330 if (!this.holdTargets[idx]) {
331 this.placeHoldsClicked = false;
334 this.placeHoldsClicked = true;
336 const target = this.holdTargets[idx];
337 const ctx = this.holdContexts.filter(
338 c => c.holdTarget === target)[0];
340 this.placeOneHold(ctx).then(() => this.placeHolds(idx + 1));
343 placeOneHold(ctx: HoldContext, override?: boolean): Promise<any> {
345 ctx.processing = true;
346 const selectedFormats = this.mrSelectorsToFilters(ctx);
348 let hType = this.holdType;
349 let hTarget = ctx.holdTarget;
350 if (hType === 'T' && ctx.holdMeta.part) {
351 // A Title hold morphs into a Part hold at hold placement time
352 // if a part is selected. This can happen on a per-hold basis
353 // when placing T-level holds.
355 hTarget = ctx.holdMeta.part.id();
358 console.debug(`Placing ${hType}-type hold on ${hTarget}`);
360 return this.holds.placeHold({
363 recipient: this.user.id(),
364 requestor: this.requestor.id(),
365 pickupLib: this.pickupLib,
367 notifyEmail: this.notifyEmail, // bool
368 notifyPhone: this.notifyPhone ? this.phoneValue : null,
369 notifySms: this.notifySms ? this.smsValue : null,
370 smsCarrier: this.notifySms ? this.smsCarrier : null,
371 thawDate: this.suspend ? this.activeDate : null,
372 frozen: this.suspend,
373 holdableFormats: selectedFormats
377 ctx.lastRequest = request;
378 ctx.processing = false;
380 if (!request.result.success) {
381 console.debug('hold failed with: ', request);
383 // If this request failed and was not already an override,
384 // see of this user has permission to override.
385 if (!request.override && request.result.evt) {
387 const txtcode = request.result.evt.textcode;
388 const perm = txtcode + '.override';
390 return this.perm.hasWorkPermHere(perm).then(
391 permResult => ctx.canOverride = permResult[perm]);
396 ctx.processing = false;
397 console.error(error);
402 override(ctx: HoldContext) {
403 this.placeOneHold(ctx, true);
406 canOverride(ctx: HoldContext): boolean {
407 return ctx.lastRequest &&
408 !ctx.lastRequest.result.success && ctx.canOverride;
411 iconFormatLabel(code: string): string {
412 return this.cat.iconFormatLabel(code);
415 // TODO: for now, only show meta filters for meta holds.
416 // Add an "advanced holds" option to display these for T hold.
417 hasMetaFilters(ctx: HoldContext): boolean {
419 this.holdType === 'M' && // TODO
420 ctx.holdMeta.metarecord_filters && (
421 ctx.holdMeta.metarecord_filters.langs.length > 1 ||
422 ctx.holdMeta.metarecord_filters.formats.length > 1
428 this.patronSearch.open({size: 'xl'}).toPromise().then(
430 if (!patrons || patrons.length === 0) { return; }
432 const user = patrons[0];
436 this.currentUserBarcode = user.card().barcode();
437 user.home_ou(this.org.get(user.home_ou()).id()); // de-flesh
438 this.applyUserSettings();
443 isItemHold(): boolean {
444 return this.holdType === 'C'
445 || this.holdType === 'R'
446 || this.holdType === 'F';
449 setPart(ctx: HoldContext, $event) {
450 const partId = $event.target.value;
453 ctx.holdMeta.parts.filter(p => +p.id() === +partId)[0];
455 ctx.holdMeta.part = null;