LP1865898 Scan Item as Missing Pieces Angular Port
[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     noteText = '';
47
48     @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
49     @ViewChild('errorMsg', {static: false}) errorMsg: StringComponent;
50
51     constructor(
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) {
60         super(modal);
61     }
62
63     ngOnInit() {
64         this.onOpen$.subscribe(_ =>
65             this.init().subscribe(__ => this.dataLoaded = true));
66     }
67
68     init(): Observable<any> {
69         this.dataLoaded = false;
70
71         this.penaltyTypeFromButton = this.SILENT_NOTE;
72
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']);
76
77         const obs1 = this.pcrud.retrieve('au', this.patronId)
78             .pipe(tap(usr => this.patron = usr));
79
80         if (this.penaltyTypes) { return obs1; }
81
82         return obs1.pipe(switchMap(_ => {
83             return this.pcrud.search('csp', {id: {'>': 100}}, {}, {atomic: true})
84
85             .pipe(tap(ptypes => {
86                 this.penaltyTypes =
87                     ptypes.sort((a, b) => a.label() < b.label() ? -1 : 1);
88             }));
89         }));
90     }
91
92     apply() {
93
94         const pen = this.idl.create('ausp');
95         pen.usr(this.patronId);
96         pen.org_unit(this.auth.user().ws_ou());
97         pen.set_date('now');
98         pen.staff(this.auth.user().id());
99
100         pen.note(this.initials ?
101             `${this.noteText} [${this.initials}]` : this.noteText);
102
103         pen.standing_penalty(
104             this.penaltyTypeFromSelect || this.penaltyTypeFromButton);
105
106         this.net.request(
107             'open-ils.actor',
108             'open-ils.actor.user.penalty.apply',
109             this.auth.token(), pen
110         ).subscribe(resp => {
111             const e = this.evt.parse(resp);
112             if (e) {
113                 this.errorMsg.current().then(msg => this.toast.danger(msg));
114                 this.error(e, true);
115             } else {
116                 // resp == penalty ID on success
117                 this.successMsg.current().then(msg => this.toast.success(msg));
118                 this.close(resp);
119             }
120         });
121     }
122
123     buttonClass(pType: number): string {
124         return this.penaltyTypeFromButton === pType ?
125             'btn-primary' : 'btn-light';
126     }
127 }
128
129
130