Project

General

Profile

1
import { RtaUser } from './../../../shared/models/rta-user.interface';
2
import { UserAccess } from './../../../shared/models/user-access.interface';
3
import { Component, OnInit } from '@angular/core';
4
import { Right, Role } from './../../../shared/models/';
5
import { ConfirmationService } from 'primeng/api';
6
import { LazyLoadEvent } from 'primeng/api';
7
import { DialogService } from 'primeng/dynamicdialog';
8
import { CreateRoleComponent } from './create-role/create-role.component';
9
import { TranslateService } from '@ngx-translate/core';
10
import { EditRoleComponent } from './edit-role/edit-role.component';
11
import { AuthService } from 'src/app/shared/services/auth.service';
12
import { RolesService } from 'src/app/shared/services/roles-users-management/role.service';
13
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
14
import { environment } from 'src/environments/environment';
15
import { Page } from 'src/app/shared/models/paging/page.interface';
16
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
17

    
18
@Component({
19
  selector: 'app-roles-management',
20
  templateUrl: './roles-management.component.html',
21
  styleUrls: ['./roles-management.component.scss']
22
})
23
export class RolesManagementComponent implements OnInit {
24

    
25
  loading = true;
26
  roles: Role[] = [];
27
  selectedRole: Role;
28
  totalRecords: number;
29
  rows = 10;
30
  users: RtaUser[];
31
  userAccessList: UserAccess[];
32
  event2: LazyLoadEvent;
33
  rights: Right[] = [];
34

    
35

    
36
  constructor(private confirmationService: ConfirmationService,
37
              private dialogService: DialogService, private translate: TranslateService, private auth: AuthService, private roleservice: RolesService,
38
              private notificationHandling: NotificationsHandlingService) { }
39

    
40
  ngOnInit(): void {
41
    if(this.auth.userRights.find(t=> t.client.id == environment.globalRightsClientID)?.rights){
42
      this.rights = this.auth.userRights.find(t=> t.client.id == environment.globalRightsClientID)?.rights;
43
    }
44
  }
45

    
46
  loadRoles(event: LazyLoadEvent): void {
47
    this.event2 = event;
48
    const pageNumber = Math.floor(event.first / event.rows);
49
    const pageSize = event.rows;
50
    this.loading = true;
51
    this.roleservice.getAllPaged(pageNumber, pageSize).subscribe((result: Page<Role>) => {
52
      this.roles = result.data;
53
      console.log(this.roles);
54
      this.totalRecords = result.totalElements;
55
      this.loading = false;
56
    },
57
      error => {
58
        this.loading = false;
59
      });
60
  }
61

    
62
  updateRoleStatus(role: Role): void {
63
    this.loading = true;
64
    this.roleservice.update(role).subscribe(result => {
65
      this.loading = false;
66
      this.loadRoles(this.event2);
67
      this.notificationHandling.showUpdateRoleStatusSuccess();
68

    
69
    },
70
      error => {
71
        this.loading = false;
72
      });
73

    
74
  }
75

    
76
  show(): void {
77
    const ref = this.dialogService.open(CreateRoleComponent, {
78
      header: this.translate.instant('CREATE_ROLE')
79
    });
80

    
81
    ref.onClose.subscribe((role: Role) => {
82
      if (role) {
83
        this.loadRoles(this.event2);
84
      }
85
  });
86
  }
87

    
88
  edit(selected: Role): void {
89
    const ref = this.dialogService.open(EditRoleComponent, {
90
      header: this.translate.instant('EDIT_ROLE_BUTTON'),
91
      //width: '70%',
92
      data: selected
93
    });
94

    
95
    ref.onClose.subscribe((role: Role) => {
96
      if (role) {
97
        this.loadRoles(this.event2);
98
      }
99
  });
100

    
101
  }
102

    
103
  editRoleIsDisabled(selectedRole: Role): boolean {
104
    if (selectedRole && !selectedRole.readOnly && USER_RIGHTS.G02.isGrantedToUser(this.rights)) {
105
      return false;
106
    } else {
107
      return true;
108
    }
109
  }
110

    
111
  createRoleIsDisabled(): boolean {
112
    if ( USER_RIGHTS.G01.isGrantedToUser(this.rights)) {
113
      return false;
114
    } else {
115
      return true;
116
    }
117
  }
118

    
119
  statusEnable(role: Role): boolean{
120
    if (USER_RIGHTS.G02.isGrantedToUser(this.rights) && !role.readOnly  ) {
121
      return false;
122
    } else {
123
      return true;
124
    }
125

    
126
  }
127

    
128
  roleCanBeDeleted(role: Role): boolean{
129
    if (USER_RIGHTS.G02.isGrantedToUser(this.rights) && role.canBeDeleted) {
130
      return false;
131
    } else {
132
      return true;
133
    }
134
  }
135

    
136
  deleteRole(id: number): void {
137
    this.loading = true;
138
    this.confirmationService.confirm({
139
      accept: () => {
140
        this.roleservice.delete(id).subscribe(result => {
141
          console.log(result);
142
          this.loading = false;
143
          this.loadRoles(this.event2);
144
          this.notificationHandling.showDeleteRoleSuccess();
145
        },
146
          error => {
147
            console.log('error');
148
            this.loading = false;
149
          });
150
        // Actual logic to perform a confirmation
151
      }, reject: () => {
152
        this.loading = false;
153
      }
154
    });
155
  }
156

    
157
  passEvent(passEvent : Role) {
158
    if(passEvent){
159
      this.loadRoles(this.event2);
160
    }
161

    
162
  }
163
}
(4-4/4)