1
|
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from '@angular/core';
|
2
|
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
|
3
|
import {AlertModal} from "../../../utils/modal/alert";
|
4
|
import {UserRegistryService} from "../../../services/user-registry.service";
|
5
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
6
|
import {properties} from "../../../../../environments/environment";
|
7
|
import {Role, Session, User} from "../../../login/utils/helper.class";
|
8
|
import {UserManagementService} from "../../../services/user-management.service";
|
9
|
import {Router} from "@angular/router";
|
10
|
import {StringUtils} from "../../../utils/string-utils.class";
|
11
|
import {NotificationService} from "../../../notifications/notification.service";
|
12
|
import {Subscription} from "rxjs";
|
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
|
@Input()
|
39
|
public inviteDisableMessage: string;
|
40
|
public user: User = null;
|
41
|
public active: any[];
|
42
|
public showActive: any[] = [];
|
43
|
public managers: any[];
|
44
|
public pending: any[];
|
45
|
public showPending: any[] = [];
|
46
|
public showCurrent: boolean = true;
|
47
|
public subs: any[] = [];
|
48
|
public loadActive: boolean = true;
|
49
|
public loadPending: boolean = true;
|
50
|
public selectedUser: string = null;
|
51
|
public invited: FormControl;
|
52
|
public properties: EnvProperties = properties;
|
53
|
public exists: boolean = true;
|
54
|
public roleFb: FormGroup;
|
55
|
/** Paging */
|
56
|
activePage: number = 1;
|
57
|
pendingPage: number = 1;
|
58
|
pageSize: number = 5;
|
59
|
/** Search */
|
60
|
filterForm: FormGroup;
|
61
|
@ViewChild('inviteModal') inviteModal: AlertModal;
|
62
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
63
|
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
|
64
|
@ViewChild('createRoleModal') createRoleModal: AlertModal;
|
65
|
|
66
|
constructor(private userRegistryService: UserRegistryService,
|
67
|
private userManagementService: UserManagementService,
|
68
|
private notificationService: NotificationService,
|
69
|
private router: Router,
|
70
|
private fb: FormBuilder) {
|
71
|
}
|
72
|
|
73
|
ngOnInit() {
|
74
|
this.initForm();
|
75
|
this.updateLists();
|
76
|
this.userManagementService.getUserInfo().subscribe(user => {
|
77
|
this.user = user;
|
78
|
});
|
79
|
}
|
80
|
|
81
|
ngOnChanges(changes: SimpleChanges) {
|
82
|
if (changes.role) {
|
83
|
this.unsubscribe();
|
84
|
this.initForm();
|
85
|
this.updateLists();
|
86
|
}
|
87
|
}
|
88
|
|
89
|
ngOnDestroy() {
|
90
|
this.unsubscribe();
|
91
|
}
|
92
|
|
93
|
unsubscribe() {
|
94
|
this.subs.forEach(sub => {
|
95
|
if (sub instanceof Subscription) {
|
96
|
sub.unsubscribe();
|
97
|
}
|
98
|
});
|
99
|
}
|
100
|
|
101
|
initForm() {
|
102
|
this.filterForm = this.fb.group({
|
103
|
active: this.fb.control(''),
|
104
|
pending: this.fb.control('')
|
105
|
});
|
106
|
this.subs.push(this.filterForm.get('active').valueChanges.subscribe(value => {
|
107
|
this.filterActiveBySearch(value);
|
108
|
}));
|
109
|
this.subs.push(this.filterForm.get('pending').valueChanges.subscribe(value => {
|
110
|
this.filterPendingBySearch(value);
|
111
|
}));
|
112
|
}
|
113
|
|
114
|
updateLists() {
|
115
|
this.loadActive = true;
|
116
|
this.loadPending = true;
|
117
|
this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, this.role).subscribe(users => {
|
118
|
if (this.role === 'member') {
|
119
|
this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, 'manager').subscribe(managers => {
|
120
|
this.managers = managers;
|
121
|
this.active = users;
|
122
|
this.active.forEach(user => {
|
123
|
user['isManager'] = this.managers.find(manager => manager.email === user.email);
|
124
|
});
|
125
|
this.filterActiveBySearch(this.filterForm.value.active);
|
126
|
this.loadActive = false;
|
127
|
this.exists = true;
|
128
|
}))
|
129
|
} else {
|
130
|
this.active = users;
|
131
|
this.filterActiveBySearch(this.filterForm.value.active);
|
132
|
this.loadActive = false;
|
133
|
this.exists = true;
|
134
|
}
|
135
|
}, error => {
|
136
|
this.active = [];
|
137
|
this.showActive = [];
|
138
|
if (error.status === 404) {
|
139
|
this.exists = false;
|
140
|
}
|
141
|
this.loadActive = false;
|
142
|
}));
|
143
|
this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
|
144
|
this.pending = users;
|
145
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
146
|
this.loadPending = false;
|
147
|
}, error => {
|
148
|
this.pending = [];
|
149
|
this.loadPending = false;
|
150
|
}));
|
151
|
}
|
152
|
|
153
|
get currentActivePage(): any[] {
|
154
|
if (this.active) {
|
155
|
return this.active.slice((this.activePage - 1) * this.pageSize, this.activePage * this.pageSize);
|
156
|
} else {
|
157
|
return [];
|
158
|
}
|
159
|
}
|
160
|
|
161
|
get currentPendingPage(): any[] {
|
162
|
if (this.pending) {
|
163
|
return this.pending.slice((this.pendingPage - 1) * this.pageSize, this.pendingPage * this.pageSize);
|
164
|
} else {
|
165
|
return [];
|
166
|
}
|
167
|
}
|
168
|
|
169
|
openDeleteModal(item: any) {
|
170
|
if (this.showCurrent) {
|
171
|
this.selectedUser = item.email;
|
172
|
this.deleteModal.alertTitle = 'Delete ' + this.role;
|
173
|
this.deleteModal.open();
|
174
|
} else {
|
175
|
this.selectedUser = item;
|
176
|
this.deletePendingModal.alertTitle = 'Cancel invitation';
|
177
|
this.deletePendingModal.open();
|
178
|
}
|
179
|
}
|
180
|
|
181
|
openInviteModal() {
|
182
|
this.inviteModal.alertTitle = 'Invite ' + this.role;
|
183
|
this.inviteModal.okButtonLeft = false;
|
184
|
this.inviteModal.okButtonText = 'Send';
|
185
|
this.invited = this.fb.control('', [Validators.required, Validators.email]);
|
186
|
this.inviteModal.open();
|
187
|
}
|
188
|
|
189
|
openCreateRoleModal() {
|
190
|
this.createRoleModal.alertTitle = 'Create group';
|
191
|
this.createRoleModal.okButtonLeft = false;
|
192
|
this.createRoleModal.okButtonText = 'Create';
|
193
|
this.roleFb = this.fb.group({
|
194
|
name: this.fb.control(Role.mapType(this.type, (this.role === 'manager')) + '.' + this.id, Validators.required),
|
195
|
description: this.fb.control('', Validators.required)
|
196
|
});
|
197
|
setTimeout(() => {
|
198
|
this.roleFb.get('name').disable();
|
199
|
}, 0);
|
200
|
this.createRoleModal.open();
|
201
|
}
|
202
|
|
203
|
deleteActive() {
|
204
|
this.loadActive = true;
|
205
|
this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
206
|
this.active = this.active.filter(user => user.email != this.selectedUser);
|
207
|
if(this.currentActivePage.length === 0) {
|
208
|
this.activePage = 1;
|
209
|
}
|
210
|
this.filterActiveBySearch(this.filterForm.value.active);
|
211
|
this.userManagementService.updateUserInfo();
|
212
|
UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
|
213
|
status: 'success',
|
214
|
timeout: 6000,
|
215
|
pos: 'bottom-right'
|
216
|
});
|
217
|
this.loadActive = false;
|
218
|
}, error => {
|
219
|
UIkit.notification('An error has occurred. Please try again later', {
|
220
|
status: 'danger',
|
221
|
timeout: 6000,
|
222
|
pos: 'bottom-right'
|
223
|
});
|
224
|
this.loadActive = false;
|
225
|
}));
|
226
|
}
|
227
|
|
228
|
deletePending() {
|
229
|
this.loadPending = true;
|
230
|
this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
231
|
this.pending = this.pending.filter(user => user != this.selectedUser);
|
232
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
233
|
if(this.currentPendingPage.length === 0) {
|
234
|
this.pendingPage = 1;
|
235
|
}
|
236
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
|
237
|
status: 'success',
|
238
|
timeout: 6000,
|
239
|
pos: 'bottom-right'
|
240
|
});
|
241
|
this.loadPending = false;
|
242
|
}, error => {
|
243
|
UIkit.notification('An error has occurred. Please try again later', {
|
244
|
status: 'danger',
|
245
|
timeout: 6000,
|
246
|
pos: 'bottom-right'
|
247
|
});
|
248
|
this.loadPending = false;
|
249
|
}));
|
250
|
}
|
251
|
|
252
|
invite() {
|
253
|
this.showCurrent = false;
|
254
|
this.loadPending = true;
|
255
|
this.selectedUser = this.invited.value;
|
256
|
let details = {
|
257
|
link: this.link,
|
258
|
email: this.emailComposer(this.name, this.invited.value, this.role)
|
259
|
}
|
260
|
this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
|
261
|
if (!this.pending.includes(this.invited.value)) {
|
262
|
this.pending.push(this.invited.value);
|
263
|
this.pendingPage = Math.ceil(this.pending.length/this.pageSize);
|
264
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
265
|
}
|
266
|
if (this.notificationFn) {
|
267
|
this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
|
268
|
UIkit.notification('A notification has been <b>sent</b> successfully', {
|
269
|
status: 'success',
|
270
|
timeout: 6000,
|
271
|
pos: 'bottom-right'
|
272
|
});
|
273
|
}, error => {
|
274
|
UIkit.notification('An error has occurred. Please try again later', {
|
275
|
status: 'danger',
|
276
|
timeout: 6000,
|
277
|
pos: 'bottom-right'
|
278
|
});
|
279
|
}));
|
280
|
}
|
281
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
|
282
|
status: 'success',
|
283
|
timeout: 6000,
|
284
|
pos: 'bottom-right'
|
285
|
});
|
286
|
this.loadPending = false;
|
287
|
}, error => {
|
288
|
UIkit.notification('An error has occurred. Please try again later', {
|
289
|
status: 'danger',
|
290
|
timeout: 6000,
|
291
|
pos: 'bottom-right'
|
292
|
});
|
293
|
this.loadPending = false;
|
294
|
}));
|
295
|
}
|
296
|
|
297
|
createGroup() {
|
298
|
this.loadActive = true;
|
299
|
this.loadPending = true;
|
300
|
this.roleFb.get('name').enable();
|
301
|
this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
|
302
|
UIkit.notification('Group has been <b> successfully created</b>', {
|
303
|
status: 'success',
|
304
|
timeout: 6000,
|
305
|
pos: 'bottom-right'
|
306
|
});
|
307
|
this.updateLists();
|
308
|
}, error => {
|
309
|
UIkit.notification('An error has occurred. Please try again later', {
|
310
|
status: 'danger',
|
311
|
timeout: 6000,
|
312
|
pos: 'bottom-right'
|
313
|
});
|
314
|
this.loadActive = false;
|
315
|
this.loadPending = false;
|
316
|
});
|
317
|
}
|
318
|
|
319
|
public get isCurator(): boolean {
|
320
|
return this.isPortalAdmin || !!Session.isCurator(this.type, this.user);
|
321
|
}
|
322
|
|
323
|
public get isPortalAdmin(): boolean {
|
324
|
return !!Session.isPortalAdministrator(this.user);
|
325
|
}
|
326
|
|
327
|
updateActivePage(event: any) {
|
328
|
this.activePage = event.value;
|
329
|
}
|
330
|
|
331
|
updatePendingPage(event: any) {
|
332
|
this.pendingPage = event.value;
|
333
|
}
|
334
|
|
335
|
private filterActiveBySearch(value: any) {
|
336
|
this.showActive = this.active.filter(active => !value || active.email.includes(value));
|
337
|
this.activePage = 1;
|
338
|
}
|
339
|
|
340
|
private filterPendingBySearch(value: any) {
|
341
|
this.showPending = this.pending.filter(pending => !value || pending.includes(value));
|
342
|
this.pendingPage = 1;
|
343
|
}
|
344
|
}
|