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 {ServerStoreService} from '@eg/core/server-store.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {NetService} from '@eg/core/net.service';
9 import {EventService} from '@eg/core/event.service';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {PcrudService} from '@eg/core/pcrud.service';
12 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
13 import {DialogComponent} from '@eg/share/dialog/dialog.component';
14 import {StringComponent} from '@eg/share/string/string.component';
18 * Dialog container for patron penalty/message application
20 * <eg-patron-penalty-dialog [patronId]="myPatronId">
21 * </eg-patron-penalty-dialog>
25 selector: 'eg-patron-penalty-dialog',
26 templateUrl: 'penalty-dialog.component.html'
29 export class PatronPenaltyDialogComponent
30 extends DialogComponent implements OnInit {
32 @Input() patronId: number;
33 @Input() penaltyNote = '';
39 penalty: IdlObject; // modifying an existing penalty
40 penaltyTypes: IdlObject[];
41 penaltyTypeFromSelect = '';
42 penaltyTypeFromButton: number;
45 requireInitials = false;
50 @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
51 @ViewChild('errorMsg', {static: false}) errorMsg: StringComponent;
54 private modal: NgbModal,
55 private idl: IdlService,
56 private org: OrgService,
57 private net: NetService,
58 private store: ServerStoreService,
59 private evt: EventService,
60 private toast: ToastService,
61 private auth: AuthService,
62 private pcrud: PcrudService) {
67 this.onOpen$.subscribe(_ =>
68 this.init().subscribe(__ => this.dataLoaded = true));
71 init(): Observable<any> {
72 this.dataLoaded = false;
74 this.penaltyTypeFromButton = 0;
75 this.penaltyTypeFromSelect = '';
79 if (this.penalty) { // Modifying an existing penalty
80 const pen = this.penalty;
81 const sp = pen.standing_penalty().id();
82 if (sp === this.ALERT_NOTE ||
83 sp === this.SILENT_NOTE || sp === this.STAFF_CHR) {
84 this.penaltyTypeFromButton = sp;
86 this.penaltyTypeFromSelect = sp;
89 this.noteText = pen.note();
92 this.penaltyTypeFromButton = this.SILENT_NOTE;
95 this.store.getItem('ui.staff.require_initials.patron_standing_penalty')
96 .then(require => this.requireInitials = require);
98 const obs1 = this.pcrud.retrieve('au', this.patronId)
99 .pipe(tap(usr => this.patron = usr));
101 if (this.penaltyTypes) { return obs1; }
103 return obs1.pipe(switchMap(_ => {
104 return this.pcrud.search('csp', {id: {'>': 100}}, {}, {atomic: true})
106 .pipe(tap(ptypes => {
108 ptypes.sort((a, b) => a.label() < b.label() ? -1 : 1);
114 this.penalty.note(this.initials ?
115 `${this.noteText} [${this.initials}]` : this.noteText);
117 this.penalty.standing_penalty(
118 this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
120 this.pcrud.update(this.penalty).toPromise()
123 this.errorMsg.current().then(msg => this.toast.danger(msg));
124 this.error('Update failed', true);
126 this.successMsg.current().then(msg => this.toast.success(msg));
136 this.modifyPenalty();
140 const pen = this.idl.create('ausp');
143 message: this.noteText ? this.noteText : ''
145 pen.usr(this.patronId);
146 pen.org_unit(this.auth.user().ws_ou());
148 pen.staff(this.auth.user().id());
151 msg.message = `${this.noteText} [${this.initials}]`;
154 pen.standing_penalty(
155 this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
159 'open-ils.actor.user.penalty.apply',
160 this.auth.token(), pen, msg
161 ).subscribe(resp => {
162 const e = this.evt.parse(resp);
164 this.errorMsg.current().then(m => this.toast.danger(m));
167 // resp == penalty ID on success
168 this.successMsg.current().then(m => this.toast.success(m));
174 buttonClass(pType: number): string {
175 return this.penaltyTypeFromButton === pType ?
176 'btn-primary' : 'btn-light';