Project

General

Profile

1
import {Component, Input, OnDestroy, OnInit, ViewChild} from "@angular/core";
2
import {User} from "../login/utils/helper.class";
3
import {ActivatedRoute, Router} from "@angular/router";
4
import {UserManagementService} from "../services/user-management.service";
5
import {UserRegistryService} from "../services/user-registry.service";
6
import {LoginErrorCodes} from "../login/utils/guardHelper.class";
7
import {Subscriber} from "rxjs";
8
import {FormBuilder, FormControl, Validators} from "@angular/forms";
9
import {AlertModal} from "../utils/modal/alert";
10

    
11
@Component({
12
  selector: 'role-verification',
13
  template: `
14
    <modal-alert #managerModal>
15
      <div>
16
        You have been invited to join <span class="uk-text-bold">{{name}}</span> Monitor Dashboard as a manager.
17
        <span class="portal-color">Fill</span> in the <span class="portal-color">verification code</span>, sent to your
18
        email, to accept the invitation request.
19
      </div>
20
      <div *ngIf="!loading" class="uk-margin-medium-top uk-flex uk-flex-center">
21
        <div dashboard-input [formInput]="code" [extraLeft]="false" class="uk-width-medium" placeholder="Write verification code">
22
          <div *ngIf="error" class="uk-text-danger uk-margin-remove uk-width-1-1">{{error}}</div>
23
        </div>
24
      </div>
25
      <div *ngIf="loading" class="uk-margin-medium-top">
26
        <loading></loading>
27
      </div>
28
      <div class="uk-margin-medium-top uk-flex uk-flex-right">
29
        <button class="uk-button uk-button-default uk-margin-medium-right" [class.uk-disabled]="loading"
30
                (click)="cancel(managerModal)">Cancel
31
        </button>
32
        <button class="uk-button" [class.portal-button]="code.valid" [class.uk-disabled]="code.invalid || loading"
33
                (click)="verifyManager()">Accept
34
        </button>
35
      </div>
36
    </modal-alert>
37
    <modal-alert #memberModal>
38
      <div *ngIf="!isMember">
39
        <div>
40
          You have been invited to join <span class="uk-text-bold">{{name}}</span> Monitor Dashboard as a member.
41
          <span class="portal-color">Fill</span> in the <span class="portal-color">verification code</span>, sent to
42
          your
43
          email, to accept the invitation request.
44
        </div>
45
        <div *ngIf="!loading" class="uk-margin-medium-top uk-flex uk-flex-wrap uk-flex-center">
46
          <div dashboard-input [formInput]="code" [extraLeft]="false" class="uk-width-medium" placeholder="Write verification code">
47
            <div *ngIf="error" class="uk-text-danger uk-margin-remove uk-width-1-1">{{error}}</div>
48
          </div>
49
        </div>
50
        <div *ngIf="loading" class="uk-margin-medium-top">
51
          <loading></loading>
52
        </div>
53
        <div class="uk-margin-medium-top uk-flex uk-flex-right">
54
          <button class="uk-button uk-button-default uk-margin-medium-right" [class.uk-disabled]="loading"
55
                  (click)="cancel(memberModal)">Cancel
56
          </button>
57
          <button class="uk-button" [class.portal-button]="code.valid" [class.uk-disabled]="code.invalid || loading"
58
                  (click)="verifyMember()">Accept
59
          </button>
60
        </div>
61
      </div>
62
      <div *ngIf="isMember">
63
        <div>
64
          Welcome! You are now a member of the OpenAIRE Monitor Dashboard for the <span class="uk-text-bold">{{name}}</span>!
65
          From now on, you will have access to our restricted content.
66
        </div>
67
        <div class="uk-margin-medium-top uk-flex uk-flex-right">
68
          <button class="uk-button uk-button-default" [class.uk-disabled]="loading"
69
                  (click)="cancel(memberModal)">Close
70
          </button>
71
        </div>
72
      </div>
73
    </modal-alert>
74
    <modal-alert #errorModal (alertOutput)="cancel(errorModal)">
75
      <div>
76
        We are unable to process the request because the link is invalid, or it has expired.
77
      </div>
78
    </modal-alert>
79
  `
80
})
81
export class RoleVerificationComponent implements OnInit, OnDestroy {
82
  
83
  @Input()
84
  public id: string;
85
  @Input()
86
  public type: string;
87
  @Input()
88
  public name: string;
89
  public user: User;
90
  public verification: any;
91
  public code: FormControl;
92
  private subs: any[] = [];
93
  @ViewChild('managerModal') managerModal: AlertModal;
94
  @ViewChild('memberModal') memberModal: AlertModal;
95
  @ViewChild('errorModal') errorModal: AlertModal;
96
  public error: string = null;
97
  public loading: boolean = false;
98
  public isMember: boolean = false;
99
  
100
  constructor(private route: ActivatedRoute,
101
              private router: Router,
102
              private fb: FormBuilder,
103
              private userManagementService: UserManagementService,
104
              private userRegistryService: UserRegistryService) {
105
  }
106
  
107
  ngOnInit() {
108
    this.reset();
109
    this.subs.push(this.route.queryParams.subscribe(params => {
110
      if (params && params['verify']) {
111
        this.subs.push(this.userManagementService.getUserInfo(false).subscribe(user => {
112
          this.user = user;
113
          if (this.user) {
114
            this.subs.push(this.userRegistryService.getInvitation(params['verify']).subscribe(verification => {
115
              this.verification = verification;
116
              if (this.user.email === this.verification.email && this.id === this.verification.entity && this.type === this.verification.type) {
117
                if (this.verification.verificationType === 'manager') {
118
                  this.openManagerModal();
119
                } else if (this.verification.verificationType === 'member') {
120
                  this.openMemberModal();
121
                }
122
              } else {
123
                this.openErrorModal();
124
              }
125
            }, error => {
126
              this.openErrorModal();
127
            }));
128
          } else {
129
            this.router.navigate(['/user-info'], {
130
              queryParams: {
131
                'errorCode': LoginErrorCodes.NOT_LOGIN,
132
                'redirectUrl': this.router.url
133
              }
134
            });
135
          }
136
        }));
137
      }
138
    }));
139
  }
140
  
141
  ngOnDestroy() {
142
    this.subs.forEach(value => {
143
      if (value instanceof Subscriber) {
144
        value.unsubscribe();
145
      }
146
    });
147
  }
148
  
149
  public openManagerModal() {
150
    this.error = null;
151
    this.managerModal.okButton = false;
152
    this.managerModal.cancelButton = false;
153
    this.managerModal.alertTitle = 'Manager Invitation';
154
    this.managerModal.open();
155
  }
156
  
157
  public openMemberModal() {
158
    this.error = null;
159
    this.memberModal.okButton = false;
160
    this.memberModal.cancelButton = false;
161
    this.memberModal.alertTitle = 'Member Invitation';
162
    this.memberModal.open();
163
  }
164
  
165
  public openErrorModal() {
166
    this.error = null;
167
    this.errorModal.cancelButton = false;
168
    this.errorModal.okButtonText = 'Ok';
169
    this.errorModal.alertTitle = 'Invalid request';
170
    this.errorModal.open();
171
  }
172
  
173
  public verifyManager() {
174
    this.loading = true;
175
    this.subs.push(this.userRegistryService.verify(this.verification.id, this.code.value).subscribe(() => {
176
      this.loading = false;
177
      this.managerModal.cancel();
178
      this.error = null;
179
      this.userManagementService.updateUserInfo(() => {
180
        this.router.navigate(['/admin/' + this.verification.entity]);
181
      });
182
    }, error => {
183
      this.loading = false;
184
      this.error = 'The verification code is invalid';
185
    }));
186
  }
187
  
188
  public verifyMember() {
189
    this.loading = true;
190
    this.subs.push(this.userRegistryService.verify(this.verification.id, this.code.value, "member").subscribe(() => {
191
      this.loading = false;
192
      this.error = null;
193
      this.userManagementService.updateUserInfo(() => {
194
        this.isMember = true;
195
      });
196
    }, error => {
197
      this.loading = false;
198
      this.error = 'The verification code is invalid';
199
    }));
200
  }
201
  
202
  public reset() {
203
    this.code = this.fb.control('', [Validators.required, Validators.pattern('^[+0-9]{6}$')]);
204
  }
205
  
206
  cancel(modal: AlertModal) {
207
    modal.cancel();
208
    this.router.navigate([this.router.url.split('?')[0]]);
209
  }
210
}
(1-1/2)