2022-03-22 16:47:38 +01:00
|
|
|
import {Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild} from '@angular/core';
|
2020-11-03 19:25:03 +01:00
|
|
|
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
|
|
|
|
import {AlertModal} from "../../../utils/modal/alert";
|
|
|
|
import {UserRegistryService} from "../../../services/user-registry.service";
|
|
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
|
|
|
import {properties} from "../../../../../environments/environment";
|
2021-02-04 14:36:29 +01:00
|
|
|
import {Role, Session, User} from "../../../login/utils/helper.class";
|
2020-11-12 18:49:20 +01:00
|
|
|
import {UserManagementService} from "../../../services/user-management.service";
|
|
|
|
import {Router} from "@angular/router";
|
2020-11-26 17:13:52 +01:00
|
|
|
import {StringUtils} from "../../../utils/string-utils.class";
|
2021-03-04 12:32:57 +01:00
|
|
|
import {NotificationService} from "../../../notifications/notification.service";
|
2021-07-14 13:19:57 +02:00
|
|
|
import {Subscription} from "rxjs";
|
2020-11-03 19:25:03 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'role-users',
|
|
|
|
templateUrl: 'role-users.component.html'
|
|
|
|
})
|
|
|
|
export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
public id: string;
|
|
|
|
@Input()
|
|
|
|
public type: string;
|
|
|
|
@Input()
|
|
|
|
public name: string;
|
|
|
|
@Input()
|
|
|
|
public link: string;
|
|
|
|
@Input()
|
2020-11-19 18:13:27 +01:00
|
|
|
public role: "member" | "manager" = "manager";
|
|
|
|
@Input()
|
2021-01-15 11:19:08 +01:00
|
|
|
public message: string = null;
|
2021-01-15 12:58:34 +01:00
|
|
|
@Input()
|
|
|
|
public emailComposer: Function;
|
2021-03-01 19:19:53 +01:00
|
|
|
@Input()
|
|
|
|
public notificationFn: Function;
|
2021-05-17 13:28:04 +02:00
|
|
|
@Input()
|
|
|
|
public inviteDisableMessage: string;
|
2022-03-22 16:47:38 +01:00
|
|
|
@Output()
|
|
|
|
public stickyEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
|
2021-01-15 11:19:08 +01:00
|
|
|
public user: User = null;
|
2021-09-17 19:13:35 +02:00
|
|
|
public active: any[] = [];
|
2021-03-28 13:07:48 +02:00
|
|
|
public showActive: any[] = [];
|
2020-11-03 19:25:03 +01:00
|
|
|
public pending: any[];
|
2021-03-28 13:07:48 +02:00
|
|
|
public showPending: any[] = [];
|
|
|
|
public showCurrent: boolean = true;
|
2020-11-03 19:25:03 +01:00
|
|
|
public subs: any[] = [];
|
|
|
|
public loadActive: boolean = true;
|
|
|
|
public loadPending: boolean = true;
|
|
|
|
public selectedUser: string = null;
|
|
|
|
public invited: FormControl;
|
|
|
|
public properties: EnvProperties = properties;
|
|
|
|
public exists: boolean = true;
|
|
|
|
public roleFb: FormGroup;
|
2021-03-28 13:07:48 +02:00
|
|
|
/** Paging */
|
|
|
|
activePage: number = 1;
|
|
|
|
pendingPage: number = 1;
|
2022-03-22 16:47:38 +01:00
|
|
|
pageSize: number = 10;
|
2021-03-28 13:07:48 +02:00
|
|
|
/** Search */
|
|
|
|
filterForm: FormGroup;
|
2020-11-03 19:25:03 +01:00
|
|
|
@ViewChild('inviteModal') inviteModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
|
|
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
|
|
|
|
@ViewChild('createRoleModal') createRoleModal: AlertModal;
|
|
|
|
|
|
|
|
constructor(private userRegistryService: UserRegistryService,
|
2020-11-12 18:49:20 +01:00
|
|
|
private userManagementService: UserManagementService,
|
2021-03-04 12:32:57 +01:00
|
|
|
private notificationService: NotificationService,
|
2020-11-12 18:49:20 +01:00
|
|
|
private router: Router,
|
2020-11-03 19:25:03 +01:00
|
|
|
private fb: FormBuilder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2021-03-28 13:15:33 +02:00
|
|
|
this.initForm();
|
|
|
|
this.updateLists();
|
|
|
|
this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.user = user;
|
2021-03-28 13:07:48 +02:00
|
|
|
});
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2021-03-28 13:15:33 +02:00
|
|
|
if (changes.role) {
|
2021-01-27 15:15:50 +01:00
|
|
|
this.unsubscribe();
|
2021-03-28 13:15:33 +02:00
|
|
|
this.initForm();
|
2020-11-03 19:25:03 +01:00
|
|
|
this.updateLists();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2021-01-27 15:15:50 +01:00
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe() {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.subs.forEach(sub => {
|
|
|
|
if (sub instanceof Subscription) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-28 13:15:33 +02:00
|
|
|
initForm() {
|
|
|
|
this.filterForm = this.fb.group({
|
|
|
|
active: this.fb.control(''),
|
|
|
|
pending: this.fb.control('')
|
|
|
|
});
|
|
|
|
this.subs.push(this.filterForm.get('active').valueChanges.subscribe(value => {
|
|
|
|
this.filterActiveBySearch(value);
|
|
|
|
}));
|
|
|
|
this.subs.push(this.filterForm.get('pending').valueChanges.subscribe(value => {
|
|
|
|
this.filterPendingBySearch(value);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:25:03 +01:00
|
|
|
updateLists() {
|
|
|
|
this.loadActive = true;
|
|
|
|
this.loadPending = true;
|
2021-09-17 19:13:35 +02:00
|
|
|
this.subs.push(this.userRegistryService.getActive(this.type, this.id, this.role).subscribe(users => {
|
|
|
|
this.active = users;
|
2022-03-22 16:47:38 +01:00
|
|
|
// users.forEach(user => this.active.push(user));
|
2021-09-17 19:13:35 +02:00
|
|
|
this.filterActiveBySearch(this.filterForm.value.active);
|
|
|
|
this.loadActive = false;
|
|
|
|
this.exists = true;
|
2020-11-03 19:25:03 +01:00
|
|
|
}, error => {
|
|
|
|
this.active = [];
|
2021-06-10 21:03:16 +02:00
|
|
|
this.showActive = [];
|
2021-03-28 13:15:33 +02:00
|
|
|
if (error.status === 404) {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.exists = false;
|
|
|
|
}
|
|
|
|
this.loadActive = false;
|
|
|
|
}));
|
|
|
|
this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
|
|
|
|
this.pending = users;
|
2021-03-28 13:07:48 +02:00
|
|
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
2020-11-03 19:25:03 +01:00
|
|
|
this.loadPending = false;
|
|
|
|
}, error => {
|
2021-03-11 13:55:59 +01:00
|
|
|
this.pending = [];
|
2020-11-26 17:13:52 +01:00
|
|
|
this.loadPending = false;
|
2020-11-03 19:25:03 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:35:39 +02:00
|
|
|
get currentActivePage(): any[] {
|
2021-08-03 11:54:41 +02:00
|
|
|
if (this.showActive) {
|
|
|
|
return this.showActive.slice((this.activePage - 1) * this.pageSize, this.activePage * this.pageSize);
|
2021-04-08 16:35:39 +02:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentPendingPage(): any[] {
|
2021-08-03 11:54:41 +02:00
|
|
|
if (this.showPending) {
|
|
|
|
return this.showPending.slice((this.pendingPage - 1) * this.pageSize, this.pendingPage * this.pageSize);
|
2021-04-08 16:35:39 +02:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:25:03 +01:00
|
|
|
openDeleteModal(item: any) {
|
2021-03-28 13:07:48 +02:00
|
|
|
if (this.showCurrent) {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.selectedUser = item.email;
|
|
|
|
this.deleteModal.alertTitle = 'Delete ' + this.role;
|
|
|
|
this.deleteModal.open();
|
|
|
|
} else {
|
|
|
|
this.selectedUser = item;
|
|
|
|
this.deletePendingModal.alertTitle = 'Cancel invitation';
|
|
|
|
this.deletePendingModal.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openInviteModal() {
|
|
|
|
this.inviteModal.alertTitle = 'Invite ' + this.role;
|
|
|
|
this.inviteModal.okButtonLeft = false;
|
|
|
|
this.inviteModal.okButtonText = 'Send';
|
|
|
|
this.invited = this.fb.control('', [Validators.required, Validators.email]);
|
|
|
|
this.inviteModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
openCreateRoleModal() {
|
|
|
|
this.createRoleModal.alertTitle = 'Create group';
|
|
|
|
this.createRoleModal.okButtonLeft = false;
|
2021-01-27 15:06:28 +01:00
|
|
|
this.createRoleModal.okButtonText = 'Create';
|
2020-11-03 19:25:03 +01:00
|
|
|
this.roleFb = this.fb.group({
|
2021-09-17 19:13:35 +02:00
|
|
|
name: this.fb.control(Role.mapType(this.type) + '.' + this.id, Validators.required),
|
|
|
|
description: this.fb.control(Role.mapType(this.type) + ' ' + this.id, Validators.required)
|
2020-11-03 19:25:03 +01:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.roleFb.get('name').disable();
|
2021-09-17 19:13:35 +02:00
|
|
|
this.roleFb.get('description').disable();
|
2020-11-03 19:25:03 +01:00
|
|
|
}, 0);
|
|
|
|
this.createRoleModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteActive() {
|
|
|
|
this.loadActive = true;
|
2021-03-01 19:19:53 +01:00
|
|
|
this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.active = this.active.filter(user => user.email != this.selectedUser);
|
2021-09-17 19:13:35 +02:00
|
|
|
if (this.currentActivePage.length === 0) {
|
2021-04-08 16:35:39 +02:00
|
|
|
this.activePage = 1;
|
|
|
|
}
|
2021-03-28 13:07:48 +02:00
|
|
|
this.filterActiveBySearch(this.filterForm.value.active);
|
2020-11-12 18:49:20 +01:00
|
|
|
this.userManagementService.updateUserInfo();
|
2020-12-01 16:43:12 +01:00
|
|
|
UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
|
2020-11-26 17:13:52 +01:00
|
|
|
status: 'success',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2020-11-03 19:25:03 +01:00
|
|
|
this.loadActive = false;
|
|
|
|
}, error => {
|
2020-11-26 17:13:52 +01:00
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2020-11-03 19:25:03 +01:00
|
|
|
this.loadActive = false;
|
2021-03-01 19:19:53 +01:00
|
|
|
}));
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
deletePending() {
|
|
|
|
this.loadPending = true;
|
2021-03-01 19:19:53 +01:00
|
|
|
this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.pending = this.pending.filter(user => user != this.selectedUser);
|
2021-03-28 13:07:48 +02:00
|
|
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
2021-09-17 19:13:35 +02:00
|
|
|
if (this.currentPendingPage.length === 0) {
|
2021-04-08 16:35:39 +02:00
|
|
|
this.pendingPage = 1;
|
|
|
|
}
|
2020-12-01 16:43:12 +01:00
|
|
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
|
2020-11-26 17:13:52 +01:00
|
|
|
status: 'success',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2020-11-03 19:25:03 +01:00
|
|
|
this.loadPending = false;
|
|
|
|
}, error => {
|
2020-11-26 17:13:52 +01:00
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2020-11-03 19:25:03 +01:00
|
|
|
this.loadPending = false;
|
2021-03-01 19:19:53 +01:00
|
|
|
}));
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
invite() {
|
2021-03-28 13:07:48 +02:00
|
|
|
this.showCurrent = false;
|
2020-11-13 13:51:35 +01:00
|
|
|
this.loadPending = true;
|
2021-01-22 17:17:18 +01:00
|
|
|
this.selectedUser = this.invited.value;
|
2020-11-03 19:25:03 +01:00
|
|
|
let details = {
|
2020-11-19 18:13:27 +01:00
|
|
|
link: this.link,
|
2021-01-15 12:58:34 +01:00
|
|
|
email: this.emailComposer(this.name, this.invited.value, this.role)
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
2021-03-01 19:19:53 +01:00
|
|
|
this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
|
2020-11-03 19:25:03 +01:00
|
|
|
if (!this.pending.includes(this.invited.value)) {
|
|
|
|
this.pending.push(this.invited.value);
|
2021-09-17 19:13:35 +02:00
|
|
|
this.pendingPage = Math.ceil(this.pending.length / this.pageSize);
|
2021-03-28 13:07:48 +02:00
|
|
|
this.filterPendingBySearch(this.filterForm.value.pending);
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
2021-03-28 13:15:33 +02:00
|
|
|
if (this.notificationFn) {
|
2021-03-04 12:32:57 +01:00
|
|
|
this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
|
|
|
|
UIkit.notification('A notification has been <b>sent</b> successfully', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
2020-12-01 16:43:12 +01:00
|
|
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
|
2020-11-26 17:13:52 +01:00
|
|
|
status: 'success',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2020-11-13 13:51:35 +01:00
|
|
|
this.loadPending = false;
|
2020-11-03 19:25:03 +01:00
|
|
|
}, error => {
|
2020-11-26 17:13:52 +01:00
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-26 17:13:52 +01:00
|
|
|
});
|
2021-01-22 17:17:18 +01:00
|
|
|
this.loadPending = false;
|
2021-03-01 19:19:53 +01:00
|
|
|
}));
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createGroup() {
|
|
|
|
this.loadActive = true;
|
|
|
|
this.loadPending = true;
|
|
|
|
this.roleFb.get('name').enable();
|
2021-09-17 19:13:35 +02:00
|
|
|
this.userRegistryService.createRole(this.type, this.id).subscribe(() => {
|
2020-12-01 16:43:12 +01:00
|
|
|
UIkit.notification('Group has been <b> successfully created</b>', {
|
2020-11-03 19:25:03 +01:00
|
|
|
status: 'success',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-03 19:25:03 +01:00
|
|
|
});
|
|
|
|
this.updateLists();
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
2020-12-01 16:43:12 +01:00
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
2020-11-03 19:25:03 +01:00
|
|
|
});
|
|
|
|
this.loadActive = false;
|
|
|
|
this.loadPending = false;
|
|
|
|
});
|
|
|
|
}
|
2021-01-15 11:19:08 +01:00
|
|
|
|
2021-02-17 15:37:28 +01:00
|
|
|
public get isCurator(): boolean {
|
|
|
|
return this.isPortalAdmin || !!Session.isCurator(this.type, this.user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get isPortalAdmin(): boolean {
|
|
|
|
return !!Session.isPortalAdministrator(this.user);
|
2021-01-15 11:19:08 +01:00
|
|
|
}
|
2021-03-28 13:07:48 +02:00
|
|
|
|
|
|
|
updateActivePage(event: any) {
|
|
|
|
this.activePage = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePendingPage(event: any) {
|
|
|
|
this.pendingPage = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private filterActiveBySearch(value: any) {
|
|
|
|
this.showActive = this.active.filter(active => !value || active.email.includes(value));
|
|
|
|
this.activePage = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private filterPendingBySearch(value: any) {
|
|
|
|
this.showPending = this.pending.filter(pending => !value || pending.includes(value));
|
|
|
|
this.pendingPage = 1;
|
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|