]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/op-change/op-change.component.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / op-change / op-change.component.ts
1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {ToastService} from '@eg/share/toast/toast.service';
3 import {AuthService} from '@eg/core/auth.service';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
6 import {NetRequest, NetService} from '@eg/core/net.service';
7
8 @Component({
9     selector: 'eg-op-change',
10     templateUrl: 'op-change.component.html'
11 })
12
13 export class OpChangeComponent
14     extends DialogComponent implements OnInit {
15
16     @Input() username: string;
17     @Input() password: string;
18     @Input() loginType = 'temp';
19
20     @Input() successMessage: string;
21     @Input() failMessage: string;
22
23     requestToEscalate: NetRequest;
24
25     constructor(
26         private modal: NgbModal, // required for passing to parent
27         private renderer: Renderer2,
28         private toast: ToastService,
29         private net: NetService,
30         private auth: AuthService) {
31         super(modal);
32     }
33
34     ngOnInit() {
35         // Focus the username any time the dialog is opened.
36         this.onOpen$.subscribe(
37             val => this.renderer.selectRootElement('#username').focus()
38         );
39     }
40
41     my_close() {
42         if (this.requestToEscalate) {
43             this.requestToEscalate.observer.error('Operation canceled');
44             delete this.requestToEscalate;
45         }
46         this.close(); // dialog close
47     }
48
49     login(): Promise<any> {
50         if (!(this.username && this.password)) {
51             return Promise.reject('Missing Params');
52         }
53
54         return this.auth.login(
55             {   username    : this.username,
56                 password    : this.password,
57                 workstation : this.auth.workstation(),
58                 type        : this.loginType
59             },  true        // isOpChange
60         ).then(
61             ok => {
62                 this.password = '';
63                 this.username = '';
64
65                 // Fetch the user object
66                 this.auth.testAuthToken().then(
67                     ok2 => {
68                         this.close();
69                         this.toast.success(this.successMessage);
70                         if (this.requestToEscalate) {
71                             // Allow a breath for the dialog to clean up.
72                             setTimeout(() => this.sendEscalatedRequest());
73                         }
74                     }
75                 );
76             },
77             notOk => {
78                 this.password = '';
79                 this.toast.danger(this.failMessage);
80             }
81         );
82     }
83
84     restore(): Promise<any> {
85         return this.auth.undoOpChange().then(
86             ok => this.toast.success(this.successMessage),
87             err => this.toast.danger(this.failMessage)
88         );
89     }
90
91     escalateRequest(req: NetRequest) {
92         this.requestToEscalate = req;
93         this.open({});
94     }
95
96     // Resend a net request using the credentials just created
97     // via operator change.
98     sendEscalatedRequest() {
99         const sourceReq = this.requestToEscalate;
100         delete this.requestToEscalate;
101
102         console.debug('Op-Change escalating request', sourceReq);
103
104         // Clone the source request, modifying the params to
105         // use the op-change'd authtoken
106         const req = new NetRequest(
107             sourceReq.service,
108             sourceReq.method,
109             [this.auth.token()].concat(sourceReq.params.splice(1))
110         );
111
112         // Relay responses received for our escalated request to
113         // the caller via the original request observer.
114         this.net.requestCompiled(req)
115             .subscribe(
116                 res => sourceReq.observer.next(res),
117                 (err: unknown) => sourceReq.observer.error(err),
118                 ()  => sourceReq.observer.complete()
119             ).add(() => this.auth.undoOpChange());
120     }
121 }
122
123