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';
18 holdMeta: HoldRequestTarget;
20 lastRequest: HoldRequest;
21 canOverride?: boolean;
25 constructor(target: number) {
26 this.holdTarget = target;
27 this.processing = false;
28 this.selectedFormats = {
29 // code => selected-boolean
37 templateUrl: 'hold.component.html'
39 export class HoldComponent implements OnInit {
42 holdTargets: number[];
57 holdContexts: HoldContext[];
58 recordSummaries: BibRecordSummary[];
60 currentUserBarcode: string;
61 smsCarriers: ComboboxEntry[];
64 placeHoldsClicked: boolean;
67 private router: Router,
68 private route: ActivatedRoute,
69 private renderer: Renderer2,
70 private evt: EventService,
71 private net: NetService,
72 private org: OrgService,
73 private auth: AuthService,
74 private pcrud: PcrudService,
75 private bib: BibRecordService,
76 private cat: CatalogService,
77 private staffCat: StaffCatalogService,
78 private holds: HoldsService,
79 private perm: PermService
81 this.holdContexts = [];
82 this.smsCarriers = [];
87 this.holdType = this.route.snapshot.params['type'];
88 this.holdTargets = this.route.snapshot.queryParams['target'];
89 this.holdFor = this.route.snapshot.queryParams['holdFor'] || 'patron';
91 if (!Array.isArray(this.holdTargets)) {
92 this.holdTargets = [this.holdTargets];
95 this.holdTargets = this.holdTargets.map(t => Number(t));
97 this.requestor = this.auth.user();
98 this.pickupLib = this.auth.user().ws_ou();
100 this.holdContexts = this.holdTargets.map(target => {
101 const ctx = new HoldContext(target);
105 if (this.holdFor === 'staff') {
106 this.holdForChanged();
109 this.getTargetMeta();
111 this.org.settings('sms.enable').then(sets => {
112 this.smsEnabled = sets['sms.enable'];
113 if (!this.smsEnabled) { return; }
115 this.pcrud.search('csc', {active: 't'}, {order_by: {csc: 'name'}})
116 .subscribe(carrier => {
117 this.smsCarriers.push({
119 label: carrier.name()
124 setTimeout(() => // Focus barcode input
125 this.renderer.selectRootElement('#patron-barcode').focus());
128 // Load the bib, call number, copy, etc. data associated with each target.
130 this.holds.getHoldTargetMeta(this.holdType, this.holdTargets)
132 this.holdContexts.filter(ctx => ctx.holdTarget === meta.target)
135 this.mrFiltersToSelectors(ctx);
140 // By default, all metarecord filters options are enabled.
141 mrFiltersToSelectors(ctx: HoldContext) {
142 if (this.holdType !== 'M') { return; }
144 const meta = ctx.holdMeta;
145 if (meta.metarecord_filters) {
146 if (meta.metarecord_filters.formats) {
147 meta.metarecord_filters.formats.forEach(
148 ccvm => ctx.selectedFormats.formats[ccvm.code()] = true);
150 if (meta.metarecord_filters.langs) {
151 meta.metarecord_filters.langs.forEach(
152 ccvm => ctx.selectedFormats.langs[ccvm.code()] = true);
157 // Map the selected metarecord filters optoins to a JSON-encoded
158 // list of attr filters as required by the API.
159 // Compiles a blob of
160 // {target: JSON({"0": [{_attr: ctype, _val: code}, ...], "1": [...]})}
161 // TODO: this should live in the hold service, not in the UI code.
162 mrSelectorsToFilters(ctx: HoldContext): {[target: number]: string} {
164 const meta = ctx.holdMeta;
165 const slf = ctx.selectedFormats;
166 const result: any = {};
168 const formats = Object.keys(slf.formats)
169 .filter(code => Boolean(slf.formats[code])); // user-selected
171 const langs = Object.keys(slf.langs)
172 .filter(code => Boolean(slf.langs[code])); // user-selected
174 const compiled: any = {};
176 if (formats.length > 0) {
178 formats.forEach(code => {
179 const ccvm = meta.metarecord_filters.formats.filter(
180 format => format.code() === code)[0];
188 if (langs.length > 0) {
190 langs.forEach(code => {
191 const ccvm = meta.metarecord_filters.langs.filter(
192 format => format.code() === code)[0];
200 if (Object.keys(compiled).length > 0) {
202 res[ctx.holdTarget] = JSON.stringify(compiled);
212 if (this.holdFor === 'patron') {
213 if (this.userBarcode) {
214 this.userBarcodeChanged();
217 // To bypass the dupe check.
218 this.currentUserBarcode = '_' + this.requestor.id();
219 this.getUser(this.requestor.id());
223 activeDateSelected(dateStr: string) {
224 this.activeDate = dateStr;
227 userBarcodeChanged() {
229 // Avoid simultaneous or duplicate lookups
230 if (this.userBarcode === this.currentUserBarcode) {
236 if (!this.userBarcode) {
242 this.currentUserBarcode = this.userBarcode;
246 'open-ils.actor.get_barcodes',
247 this.auth.token(), this.auth.user().ws_ou(),
248 'actor', this.userBarcode
249 ).subscribe(barcodes => {
251 // Use the first successful barcode response.
252 // TODO: What happens when there are multiple responses?
253 // Use for-loop for early exit since we have async
254 // action within the loop.
255 for (let i = 0; i < barcodes.length; i++) {
256 const bc = barcodes[i];
257 if (!this.evt.parse(bc)) {
266 this.notifyEmail = true;
267 this.notifyPhone = true;
268 this.phoneValue = '';
269 this.pickupLib = this.requestor.ws_ou();
272 getUser(id: number) {
273 this.pcrud.retrieve('au', id, {flesh: 1, flesh_fields: {au: ['settings']}})
276 this.applyUserSettings();
280 applyUserSettings() {
281 if (!this.user || !this.user.settings()) { return; }
283 // Start with defaults.
284 this.phoneValue = this.user.day_phone() || this.user.evening_phone();
286 // Default to work org if placing holds for staff.
287 if (this.user.id() !== this.requestor.id()) {
288 this.pickupLib = this.user.home_ou();
291 this.user.settings().forEach(setting => {
292 const name = setting.name();
293 const value = setting.value();
295 if (value === '' || value === null) { return; }
298 case 'opac.hold_notify':
299 this.notifyPhone = Boolean(value.match(/phone/));
300 this.notifyEmail = Boolean(value.match(/email/));
301 this.notifySms = Boolean(value.match(/sms/));
304 case 'opac.default_pickup_location':
305 this.pickupLib = value;
310 if (!this.user.email()) {
311 this.notifyEmail = false;
314 if (!this.phoneValue) {
315 this.notifyPhone = false;
319 // Attempt hold placement on all targets
320 placeHolds(idx?: number) {
321 if (!idx) { idx = 0; }
322 if (!this.holdTargets[idx]) { return; }
323 this.placeHoldsClicked = true;
325 const target = this.holdTargets[idx];
326 const ctx = this.holdContexts.filter(
327 c => c.holdTarget === target)[0];
329 this.placeOneHold(ctx).then(() => this.placeHolds(idx + 1));
332 placeOneHold(ctx: HoldContext, override?: boolean): Promise<any> {
334 ctx.processing = true;
335 const selectedFormats = this.mrSelectorsToFilters(ctx);
337 return this.holds.placeHold({
338 holdTarget: ctx.holdTarget,
339 holdType: this.holdType,
340 recipient: this.user.id(),
341 requestor: this.requestor.id(),
342 pickupLib: this.pickupLib,
344 notifyEmail: this.notifyEmail, // bool
345 notifyPhone: this.notifyPhone ? this.phoneValue : null,
346 notifySms: this.notifySms ? this.smsValue : null,
347 smsCarrier: this.notifySms ? this.smsCarrier : null,
348 thawDate: this.suspend ? this.activeDate : null,
349 frozen: this.suspend,
350 holdableFormats: selectedFormats
354 console.log('hold returned: ', request);
355 ctx.lastRequest = request;
356 ctx.processing = false;
358 // If this request failed and was not already an override,
359 // see of this user has permission to override.
360 if (!request.override &&
361 !request.result.success && request.result.evt) {
363 const txtcode = request.result.evt.textcode;
364 const perm = txtcode + '.override';
366 return this.perm.hasWorkPermHere(perm).then(
367 permResult => ctx.canOverride = permResult[perm]);
371 ctx.processing = false;
372 console.error(error);
377 override(ctx: HoldContext) {
378 this.placeOneHold(ctx, true);
381 canOverride(ctx: HoldContext): boolean {
382 return ctx.lastRequest &&
383 !ctx.lastRequest.result.success && ctx.canOverride;
386 iconFormatLabel(code: string): string {
387 return this.cat.iconFormatLabel(code);
390 // TODO: for now, only show meta filters for meta holds.
391 // Add an "advanced holds" option to display these for T hold.
392 hasMetaFilters(ctx: HoldContext): boolean {
394 this.holdType === 'M' && // TODO
395 ctx.holdMeta.metarecord_filters && (
396 ctx.holdMeta.metarecord_filters.langs.length > 1 ||
397 ctx.holdMeta.metarecord_filters.formats.length > 1