]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/patron/penalty-dialog.component.ts
LP#1846354: update Angular new penalty dialog
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / patron / penalty-dialog.component.ts
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';
14
15
16 /**
17  * Dialog container for patron penalty/message application
18  *
19  * <eg-patron-penalty-dialog [patronId]="myPatronId">
20  * </eg-patron-penalty-dialog>
21  */
22
23 @Component({
24   selector: 'eg-patron-penalty-dialog',
25   templateUrl: 'penalty-dialog.component.html'
26 })
27
28 export class PatronPenaltyDialogComponent
29     extends DialogComponent implements OnInit {
30
31     @Input() patronId: number;
32     @Input() penaltyNote = '';
33
34     ALERT_NOTE = 20;
35     SILENT_NOTE = 21;
36     STAFF_CHR = 25;
37
38     staffInitials: string;
39     penaltyTypes: IdlObject[];
40     penaltyTypeFromSelect = '';
41     penaltyTypeFromButton;
42     patron: IdlObject;
43     dataLoaded = false;
44     requireInitials = false;
45     initials: string;
46     title = '';
47     noteText = '';
48
49     @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
50     @ViewChild('errorMsg', {static: false}) errorMsg: StringComponent;
51
52     constructor(
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) {
61         super(modal);
62     }
63
64     ngOnInit() {
65         this.onOpen$.subscribe(_ =>
66             this.init().subscribe(__ => this.dataLoaded = true));
67     }
68
69     init(): Observable<any> {
70         this.dataLoaded = false;
71
72         this.penaltyTypeFromButton = this.SILENT_NOTE;
73
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']);
77
78         const obs1 = this.pcrud.retrieve('au', this.patronId)
79             .pipe(tap(usr => this.patron = usr));
80
81         if (this.penaltyTypes) { return obs1; }
82
83         return obs1.pipe(switchMap(_ => {
84             return this.pcrud.search('csp', {id: {'>': 100}}, {}, {atomic: true})
85
86             .pipe(tap(ptypes => {
87                 this.penaltyTypes =
88                     ptypes.sort((a, b) => a.label() < b.label() ? -1 : 1);
89             }));
90         }));
91     }
92
93     apply() {
94
95         const pen = this.idl.create('ausp');
96         const msg = {
97             title: this.title,
98             message: this.noteText ? this.noteText : ''
99         };
100         pen.usr(this.patronId);
101         pen.org_unit(this.auth.user().ws_ou());
102         pen.set_date('now');
103         pen.staff(this.auth.user().id());
104
105         if (this.initials) {
106             msg.message = `${this.noteText} [${this.initials}]`;
107         }
108
109         pen.standing_penalty(
110             this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
111
112         this.net.request(
113             'open-ils.actor',
114             'open-ils.actor.user.penalty.apply',
115             this.auth.token(), pen, msg
116         ).subscribe(resp => {
117             const e = this.evt.parse(resp);
118             if (e) {
119                 this.errorMsg.current().then(m => this.toast.danger(m));
120                 this.error(e, true);
121             } else {
122                 // resp == penalty ID on success
123                 this.successMsg.current().then(m => this.toast.success(m));
124                 this.close(resp);
125             }
126         });
127     }
128
129     buttonClass(pType: number): string {
130         return this.penaltyTypeFromButton === pType ?
131             'btn-primary' : 'btn-light';
132     }
133 }
134
135
136