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;
49 @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
50 @ViewChild('errorMsg', {static: false}) errorMsg: StringComponent;
53 private modal: NgbModal,
54 private idl: IdlService,
55 private org: OrgService,
56 private net: NetService,
57 private evt: EventService,
58 private toast: ToastService,
59 private auth: AuthService,
60 private pcrud: PcrudService) {
65 this.onOpen$.subscribe(_ =>
66 this.init().subscribe(__ => this.dataLoaded = true));
69 init(): Observable<any> {
70 this.dataLoaded = false;
72 this.penaltyTypeFromButton = this.SILENT_NOTE;
74 this.org.settings(['ui.staff.require_initials.patron_standing_penalty'])
75 .then(sets => this.requireInitials =
76 sets['ui.staff.require_initials.patron_standing_penalty']);
78 const obs1 = this.pcrud.retrieve('au', this.patronId)
79 .pipe(tap(usr => this.patron = usr));
81 if (this.penaltyTypes) { return obs1; }
83 return obs1.pipe(switchMap(_ => {
84 return this.pcrud.search('csp', {id: {'>': 100}}, {}, {atomic: true})
88 ptypes.sort((a, b) => a.label() < b.label() ? -1 : 1);
95 const pen = this.idl.create('ausp');
98 message: this.noteText ? this.noteText : ''
100 pen.usr(this.patronId);
101 pen.org_unit(this.auth.user().ws_ou());
103 pen.staff(this.auth.user().id());
106 msg.message = `${this.noteText} [${this.initials}]`;
109 pen.standing_penalty(
110 this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
114 'open-ils.actor.user.penalty.apply',
115 this.auth.token(), pen, msg
116 ).subscribe(resp => {
117 const e = this.evt.parse(resp);
119 this.errorMsg.current().then(m => this.toast.danger(m));
122 // resp == penalty ID on success
123 this.successMsg.current().then(m => this.toast.success(m));
129 buttonClass(pType: number): string {
130 return this.penaltyTypeFromButton === pType ?
131 'btn-primary' : 'btn-light';