Project

General

Profile

1
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from '@angular/core';
2
import {Subscription} from 'rxjs/Rx';
3
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
4
import {AlertModal} from "../../../utils/modal/alert";
5
import {UserRegistryService} from "../../../services/user-registry.service";
6
import {EnvProperties} from "../../../utils/properties/env-properties";
7
import {properties} from "../../../../../environments/environment";
8
import {Role, Session, User} from "../../../login/utils/helper.class";
9
import {UserManagementService} from "../../../services/user-management.service";
10
import {Router} from "@angular/router";
11
import {StringUtils} from "../../../utils/string-utils.class";
12
import {NotificationService} from "../../../notifications/notification.service";
13

    
14
declare var UIkit;
15

    
16
@Component({
17
  selector: 'role-users',
18
  templateUrl: 'role-users.component.html'
19
})
20
export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
21
  
22
  @Input()
23
  public id: string;
24
  @Input()
25
  public type: string;
26
  @Input()
27
  public name: string;
28
  @Input()
29
  public link: string;
30
  @Input()
31
  public role: "member" | "manager" = "manager";
32
  @Input()
33
  public message: string = null;
34
  @Input()
35
  public emailComposer: Function;
36
  @Input()
37
  public notificationFn: Function;
38
  public user: User = null;
39
  public active: any[];
40
  public managers: any[];
41
  public pending: any[];
42
  public showActive: boolean = true;
43
  public subs: any[] = [];
44
  public loadActive: boolean = true;
45
  public loadPending: boolean = true;
46
  public selectedUser: string = null;
47
  public invited: FormControl;
48
  public properties: EnvProperties = properties;
49
  public exists: boolean = true;
50
  public roleFb: FormGroup;
51
  @ViewChild('inviteModal') inviteModal: AlertModal;
52
  @ViewChild('deleteModal') deleteModal: AlertModal;
53
  @ViewChild('deletePendingModal') deletePendingModal: AlertModal;
54
  @ViewChild('createRoleModal') createRoleModal: AlertModal;
55
  
56
  constructor(private userRegistryService: UserRegistryService,
57
              private userManagementService: UserManagementService,
58
              private notificationService: NotificationService,
59
              private router: Router,
60
              private fb: FormBuilder) {
61
  }
62
  
63
  ngOnInit() {
64
   this.updateLists();
65
   this.userManagementService.getUserInfo().subscribe(user => {
66
     this.user = user;
67
   });
68
  }
69
  
70
  ngOnChanges(changes: SimpleChanges) {
71
    if(changes.role) {
72
      this.unsubscribe();
73
      this.updateLists();
74
    }
75
  }
76
  
77
  ngOnDestroy() {
78
    this.unsubscribe();
79
  }
80
  
81
  unsubscribe() {
82
    this.subs.forEach(sub => {
83
      if (sub instanceof Subscription) {
84
        sub.unsubscribe();
85
      }
86
    });
87
  }
88
  
89
  
90
  updateLists() {
91
    this.loadActive = true;
92
    this.loadPending = true;
93
    this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, this.role).subscribe(users => {
94
      if(this.role === 'member') {
95
        this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, 'manager').subscribe(managers => {
96
          this.managers = managers;
97
          this.active = users;
98
          this.active.forEach(user => {
99
            user['isManager'] = this.managers.find(manager => manager.email === user.email);
100
          });
101
          this.loadActive = false;
102
          this.exists = true;
103
        }))
104
      } else {
105
        this.active = users;
106
        this.loadActive = false;
107
        this.exists = true;
108
      }
109
    }, error => {
110
      this.active = [];
111
      if(error.status === 404) {
112
        this.exists = false;
113
      }
114
      this.loadActive = false;
115
    }));
116
    this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
117
      this.pending = users;
118
      this.loadPending = false;
119
    }, error => {
120
      this.pending = [];
121
      this.loadPending = false;
122
    }));
123
  }
124
  
125
  openDeleteModal(item: any) {
126
    if (this.showActive) {
127
      this.selectedUser = item.email;
128
      this.deleteModal.alertTitle = 'Delete ' + this.role;
129
      this.deleteModal.open();
130
    } else {
131
      this.selectedUser = item;
132
      this.deletePendingModal.alertTitle = 'Cancel invitation';
133
      this.deletePendingModal.open();
134
    }
135
  }
136
  
137
  openInviteModal() {
138
    this.inviteModal.alertTitle = 'Invite ' + this.role;
139
    this.inviteModal.okButtonLeft = false;
140
    this.inviteModal.okButtonText = 'Send';
141
    this.invited = this.fb.control('', [Validators.required, Validators.email]);
142
    this.inviteModal.open();
143
  }
144
  
145
  openCreateRoleModal() {
146
    this.createRoleModal.alertTitle = 'Create group';
147
    this.createRoleModal.okButtonLeft = false;
148
    this.createRoleModal.okButtonText = 'Create';
149
    this.roleFb = this.fb.group({
150
      name: this.fb.control(Role.mapType(this.type, (this.role === 'manager')) + '.' + this.id, Validators.required),
151
      description: this.fb.control('', Validators.required)
152
    });
153
    setTimeout(() => {
154
      this.roleFb.get('name').disable();
155
    }, 0);
156
    this.createRoleModal.open();
157
  }
158
  
159
  deleteActive() {
160
    this.loadActive = true;
161
    this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
162
      this.active = this.active.filter(user => user.email != this.selectedUser);
163
      this.userManagementService.updateUserInfo();
164
      UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
165
        status: 'success',
166
        timeout: 6000,
167
        pos: 'bottom-right'
168
      });
169
      this.loadActive = false;
170
    }, error => {
171
      UIkit.notification('An error has occurred. Please try again later', {
172
        status: 'danger',
173
        timeout: 6000,
174
        pos: 'bottom-right'
175
      });
176
      this.loadActive = false;
177
    }));
178
  }
179
  
180
  deletePending() {
181
    this.loadPending = true;
182
    this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
183
      this.pending = this.pending.filter(user => user != this.selectedUser);
184
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
185
        status: 'success',
186
        timeout: 6000,
187
        pos: 'bottom-right'
188
      });
189
      this.loadPending = false;
190
    }, error => {
191
      UIkit.notification('An error has occurred. Please try again later', {
192
        status: 'danger',
193
        timeout: 6000,
194
        pos: 'bottom-right'
195
      });
196
      this.loadPending = false;
197
    }));
198
  }
199
  
200
  invite() {
201
    this.showActive = false;
202
    this.loadPending = true;
203
    this.selectedUser = this.invited.value;
204
    let details = {
205
      link: this.link,
206
      email: this.emailComposer(this.name, this.invited.value, this.role)
207
    }
208
    this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
209
      if (!this.pending.includes(this.invited.value)) {
210
        this.pending.push(this.invited.value);
211
      }
212
      if(this.notificationFn) {
213
        this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
214
          UIkit.notification('A notification has been <b>sent</b> successfully', {
215
            status: 'success',
216
            timeout: 6000,
217
            pos: 'bottom-right'
218
          });
219
        }, error => {
220
          UIkit.notification('An error has occurred. Please try again later', {
221
            status: 'danger',
222
            timeout: 6000,
223
            pos: 'bottom-right'
224
          });
225
        }));
226
      }
227
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
228
        status: 'success',
229
        timeout: 6000,
230
        pos: 'bottom-right'
231
      });
232
      this.loadPending = false;
233
    }, error => {
234
      UIkit.notification('An error has occurred. Please try again later', {
235
        status: 'danger',
236
        timeout: 6000,
237
        pos: 'bottom-right'
238
      });
239
      this.loadPending = false;
240
    }));
241
  }
242
  
243
  createGroup() {
244
    this.loadActive = true;
245
    this.loadPending = true;
246
    this.roleFb.get('name').enable();
247
    this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
248
      UIkit.notification('Group has been <b> successfully created</b>', {
249
        status: 'success',
250
        timeout: 6000,
251
        pos: 'bottom-right'
252
      });
253
      this.updateLists();
254
    }, error => {
255
      UIkit.notification('An error has occurred. Please try again later', {
256
        status: 'danger',
257
        timeout: 6000,
258
        pos: 'bottom-right'
259
      });
260
      this.loadActive = false;
261
      this.loadPending = false;
262
    });
263
  }
264
  
265
  public get isCurator(): boolean {
266
    return this.isPortalAdmin || !!Session.isCurator(this.type, this.user);
267
  }
268
  
269
  public get isPortalAdmin(): boolean {
270
    return !!Session.isPortalAdministrator(this.user);
271
  }
272
}
(2-2/3)