1
|
import { ErrorHandlingService } from './../../../../shared/services/error-handling/error-handling.service';
|
2
|
import { RtaUser } from './../../../../shared/models/rta-user.interface';
|
3
|
import { UserAccess } from './../../../../shared/models/user-access.interface';
|
4
|
import { IPowerClient } from 'src/app/shared/models/ipower-client.interface';
|
5
|
import { Component, OnInit, Input, SimpleChanges, EventEmitter, Output } from '@angular/core';
|
6
|
import { Right, Role } from 'src/app/shared/models';
|
7
|
import { ConfirmationService } from 'primeng/api';
|
8
|
import { AssignUpdateRole } from 'src/app/shared/models/request/assign-update-role.interface';
|
9
|
import { AuthService } from 'src/app/shared/services/auth.service';
|
10
|
import { UsersService } from 'src/app/shared/services/roles-users-management/user.service';
|
11
|
import { RolesService } from 'src/app/shared/services/roles-users-management/role.service';
|
12
|
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
|
13
|
import { environment } from 'src/environments/environment'
|
14
|
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
|
15
|
|
16
|
@Component({
|
17
|
selector: 'app-user-roles',
|
18
|
templateUrl: './user-roles.component.html',
|
19
|
styleUrls: ['./user-roles.component.scss']
|
20
|
})
|
21
|
export class UserRolesComponent implements OnInit {
|
22
|
@Input() user: RtaUser;
|
23
|
@Output() valueChange = new EventEmitter();
|
24
|
selectedRole: Role;
|
25
|
assignNewRole: AssignUpdateRole;
|
26
|
selectedIpowerClient: string;
|
27
|
rights: Right[];
|
28
|
newIPowerClient: IPowerClient;
|
29
|
userIPowerClient: UserAccess[];
|
30
|
roles: Role[] = [];
|
31
|
ipowerClientCodeSuggestions: string[];
|
32
|
iPowerClientSuggestion: IPowerClient[];
|
33
|
loading : boolean;
|
34
|
userRights: Right[] = [];
|
35
|
checkIfUserHasValue: boolean;
|
36
|
|
37
|
|
38
|
constructor(
|
39
|
private confirmationService: ConfirmationService,
|
40
|
private auth: AuthService,
|
41
|
private userservice: UsersService,
|
42
|
private roleservice: RolesService,
|
43
|
private errorHandlingService: ErrorHandlingService,
|
44
|
private notificationService: NotificationsHandlingService
|
45
|
) { }
|
46
|
|
47
|
ngOnInit(): void {
|
48
|
if (this.auth.userRights.find(t => t.client.id == environment.globalRightsClientID)?.rights) {
|
49
|
this.userRights = this.auth.userRights.find(t => t.client.id == environment.globalRightsClientID)?.rights;
|
50
|
}
|
51
|
this.roleservice.getRoles().subscribe(result => {
|
52
|
this.roles = result;
|
53
|
},
|
54
|
error => {
|
55
|
});
|
56
|
console.log(this.userIPowerClient);
|
57
|
}
|
58
|
|
59
|
ngOnChanges(changes: SimpleChanges): void {
|
60
|
if (this.user) {
|
61
|
this.userIPowerClient = this.user?.userAccessUsers?.map((item) => item);
|
62
|
this.checkIfUserHasValue = false;
|
63
|
this.checkIfUserHasValue = this.user?.userAccessUsers?.some(x => x.status == 'Active');
|
64
|
}
|
65
|
|
66
|
|
67
|
}
|
68
|
|
69
|
isDisabled(user: RtaUser): boolean {
|
70
|
if (user != null && USER_RIGHTS.G04.isGrantedToUser(this.userRights)) {
|
71
|
return false;
|
72
|
} else {
|
73
|
return true;
|
74
|
}
|
75
|
}
|
76
|
|
77
|
|
78
|
autosuggestIPowerClientCode(event) {
|
79
|
if (!event.query || event.query.length < 3) {
|
80
|
this.ipowerClientCodeSuggestions = [];
|
81
|
return;
|
82
|
}
|
83
|
this.userservice.getClientsByCode(event.query as string).subscribe(
|
84
|
(values) => {
|
85
|
let temp: string[] = [];
|
86
|
this.iPowerClientSuggestion = values;
|
87
|
values.map(val => temp.push(val.clientCode));
|
88
|
this.ipowerClientCodeSuggestions = temp;
|
89
|
},
|
90
|
err => this.errorHandlingService.showHttpResponseError(err)
|
91
|
);
|
92
|
}
|
93
|
|
94
|
disableUserRole(id: number): void {
|
95
|
console.log(this.userIPowerClient);
|
96
|
this.confirmationService.confirm({
|
97
|
accept: () => {
|
98
|
this.loading = true;
|
99
|
this.userservice.disableUserRole(id).subscribe(result => {
|
100
|
this.valueChange.emit(result);
|
101
|
this.notificationService.showDisableUserRoleSuccess();
|
102
|
this.loading = false;
|
103
|
},
|
104
|
error => {
|
105
|
console.log('error');
|
106
|
this.loading = false;
|
107
|
});
|
108
|
}, reject: () => {
|
109
|
this.loading = false;
|
110
|
}
|
111
|
});
|
112
|
}
|
113
|
|
114
|
assignmentNewRole(): void {
|
115
|
if (this.selectedRole.isGlobal) {
|
116
|
this.newIPowerClient = Object.assign({ id: "PWC" });
|
117
|
} else {
|
118
|
this.newIPowerClient = this.iPowerClientSuggestion.find(client => client.clientCode === this.selectedIpowerClient);
|
119
|
}
|
120
|
this.assignNewRole = { id: null, ipowerClient: Object.assign(this.newIPowerClient), role: Object.assign(this.selectedRole), users: Object.assign(this.user) };
|
121
|
console.log(this.assignNewRole);
|
122
|
this.userservice.assignNewRole(this.assignNewRole).subscribe(result => {
|
123
|
this.notificationService.showAssignmentNewRoleSuccess();
|
124
|
this.valueChange.emit(result);
|
125
|
},
|
126
|
error => {
|
127
|
console.error(error); // TODO: Handle this via a Messaging Service
|
128
|
});
|
129
|
|
130
|
}
|
131
|
|
132
|
|
133
|
}
|