1 import {Component, OnInit, Input, Output, ViewChild} from '@angular/core';
2 import {merge, from, Observable} from 'rxjs';
3 import {tap, take, switchMap} from 'rxjs/operators';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {NetService} from '@eg/core/net.service';
8 import {EventService} from '@eg/core/event.service';
9 import {ToastService} from '@eg/share/toast/toast.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
12 import {DialogComponent} from '@eg/share/dialog/dialog.component';
13 import {StringComponent} from '@eg/share/string/string.component';
17 * Dialog container for patron penalty/message application
19 * <eg-patron-penalty-dialog [patronId]="myPatronId">
20 * </eg-patron-penalty-dialog>
24 selector: 'eg-patron-penalty-dialog',
25 templateUrl: 'penalty-dialog.component.html'
28 export class PatronPenaltyDialogComponent
29 extends DialogComponent implements OnInit {
31 @Input() patronId: number;
32 @Input() penaltyNote = '';
38 staffInitials: string;
39 penaltyTypes: IdlObject[];
40 penaltyTypeFromSelect = '';
41 penaltyTypeFromButton;
44 requireInitials = false;
48 @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
49 @ViewChild('errorMsg', {static: false}) errorMsg: StringComponent;
52 private modal: NgbModal,
53 private idl: IdlService,
54 private org: OrgService,
55 private net: NetService,
56 private evt: EventService,
57 private toast: ToastService,
58 private auth: AuthService,
59 private pcrud: PcrudService) {
64 this.onOpen$.subscribe(_ =>
65 this.init().subscribe(__ => this.dataLoaded = true));
68 init(): Observable<any> {
69 this.dataLoaded = false;
71 this.penaltyTypeFromButton = this.SILENT_NOTE;
73 this.org.settings(['ui.staff.require_initials.patron_standing_penalty'])
74 .then(sets => this.requireInitials =
75 sets['ui.staff.require_initials.patron_standing_penalty']);
77 const obs1 = this.pcrud.retrieve('au', this.patronId)
78 .pipe(tap(usr => this.patron = usr));
80 if (this.penaltyTypes) { return obs1; }
82 return obs1.pipe(switchMap(_ => {
83 return this.pcrud.search('csp', {id: {'>': 100}}, {}, {atomic: true})
87 ptypes.sort((a, b) => a.label() < b.label() ? -1 : 1);
94 const pen = this.idl.create('ausp');
95 pen.usr(this.patronId);
96 pen.org_unit(this.auth.user().ws_ou());
98 pen.staff(this.auth.user().id());
100 pen.note(this.initials ?
101 `${this.noteText} [${this.initials}]` : this.noteText);
103 pen.standing_penalty(
104 this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
108 'open-ils.actor.user.penalty.apply',
109 this.auth.token(), pen
110 ).subscribe(resp => {
111 const e = this.evt.parse(resp);
113 this.errorMsg.current().then(msg => this.toast.danger(msg));
116 // resp == penalty ID on success
117 this.successMsg.current().then(msg => this.toast.success(msg));
123 buttonClass(pType: number): string {
124 return this.penaltyTypeFromButton === pType ?
125 'btn-primary' : 'btn-light';