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
import {properties} from "../../../environments/environment";
11
import {EmailService} from "../utils/email/email.service";
12
import {Composer} from "../utils/email/composer";
13

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