blob: 99670f078683f44cf28be4cf091d2261e76b3634 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
2 Copyright 2020 TATA ELXSI
3
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17*/
SANDHYA.JS1b17c432023-04-26 17:54:57 +053018/* eslint-disable security/detect-object-injection */
kumaran.m3b4814a2020-05-01 19:48:54 +053019/**
20 * @file users details Component.
21 */
22import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
23import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { AddEditUserComponent } from 'AddEditUserComponent';
SANDHYA.JS1b17c432023-04-26 17:54:57 +053026import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053027import { DataService } from 'DataService';
28import { environment } from 'environment';
29import { LocalDataSource } from 'ng2-smart-table';
30import { ProjectService } from 'ProjectService';
31import { RestService } from 'RestService';
32import { Subscription } from 'rxjs';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053033import { SharedService, isNullOrUndefined } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053034import { UserData, UserDetail } from 'UserModel';
35import { UsersActionComponent } from 'UsersActionComponent';
36
37/**
38 * Creating component
39 * @Component takes UserDetailsComponent.html as template url
40 */
41@Component({
42 templateUrl: './UserDetailsComponent.html',
43 styleUrls: ['./UserDetailsComponent.scss']
44})
45/** Exporting a class @exports UserDetailsComponent */
46export class UserDetailsComponent implements OnInit, OnDestroy {
47 /** Data of smarttable populate through LocalDataSource @public */
48 public dataSource: LocalDataSource = new LocalDataSource();
49
50 /** handle translate @public */
51 public translateService: TranslateService;
52
53 /** To inject services @public */
54 public injector: Injector;
55
56 /** Settings for smarttable to populate the table with columns @public */
57 public settings: object = {};
58
59 /** Check the loading results @public */
60 public isLoadingResults: boolean = true;
61
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
64
65 /** Class for empty and present data @public */
66 public checkDataClass: string;
67
SANDHYA.JS1b17c432023-04-26 17:54:57 +053068 /** user active data @public */
69 public userActive: string = CONFIGCONSTANT.userActive;
70
71 /** user locked data @public */
72 public userLocked: string = CONFIGCONSTANT.userLocked;
73
74 /** user expired data @public */
75 public userExpired: string = CONFIGCONSTANT.userExpired;
76
77 /** user always-active data @public */
78 public userAlwaysActive: string = CONFIGCONSTANT.userAlwaysActive;
79
80 /** Admin Visibility Check @public */
81 public isAdminShow: boolean;
82
kumaran.m3b4814a2020-05-01 19:48:54 +053083 /** Instance of the rest service @private */
84 private restService: RestService;
85
86 /** dataService to pass the data from one component to another @private */
87 private dataService: DataService;
88
89 /** Instance of the modal service @private */
90 private modalService: NgbModal;
91
92 /** Formation of appropriate Data for LocalDatasource @private */
93 private userData: {}[] = [];
94
95 /** Contains all methods related to shared @private */
96 private sharedService: SharedService;
97
98 /** Holds all project details */
99 private projectService: ProjectService;
100
101 /** holds the project information @private */
102 private projectList: {}[] = [];
103
104 /** Columns list of the smart table @public */
105 private columnLists: object = {};
106
107 /** Instance of subscriptions @private */
108 private generateDataSub: Subscription;
109
110 constructor(injector: Injector) {
111 this.injector = injector;
112 this.restService = this.injector.get(RestService);
113 this.dataService = this.injector.get(DataService);
114 this.sharedService = this.injector.get(SharedService);
115 this.modalService = this.injector.get(NgbModal);
116 this.projectService = this.injector.get(ProjectService);
117 this.translateService = this.injector.get(TranslateService);
118 }
119
120 /**
121 * Lifecyle Hooks the trigger before component is instantiate
122 */
123 public ngOnInit(): void {
124 this.projectService.getAllProjects().subscribe((projects: {}[]) => {
125 this.projectList = projects;
126 });
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530127 this.isAdminShow = sessionStorage.getItem('admin_show') === 'true' ? true : false;
kumaran.m3b4814a2020-05-01 19:48:54 +0530128 this.generateColumns();
129 this.generateSettings();
130 this.generateData();
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530131 this.hideColumnForUser();
kumaran.m3b4814a2020-05-01 19:48:54 +0530132 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
133 }
134
135 /** smart table Header Colums @public */
136 public generateColumns(): void {
137 this.columnLists = {
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530138 username: { title: this.translateService.instant('NAME'), width: '10%', sortDirection: 'asc' },
139 projects: { title: this.translateService.instant('PAGE.DASHBOARD.PROJECTS'), width: '15%' },
140 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '10%' },
141 user_status: {
142 type: 'html',
143 title: this.translateService.instant('STATUS'),
144 width: '15%',
145 filter: {
146 type: 'list',
147 config: {
148 selectText: 'Select',
149 list: [
150 { value: this.userActive, title: this.userActive },
151 { value: this.userLocked, title: this.userLocked },
152 { value: this.userExpired, title: this.userExpired },
153 { value: this.userAlwaysActive, title: this.userAlwaysActive }
154 ]
155 }
156 },
157 valuePrepareFunction: (cell: UserData, row: UserData): string => {
158 if (row.user_status === this.userActive) {
159 return `<span class="icon-label" title="${row.user_status}">
160 <i class="fas fa-user-check text-success"></i>
161 </span>`;
162 } else if (row.user_status === this.userLocked) {
163 return `<span class="icon-label" title="${row.user_status}">
164 <i class="fas fa-user-lock text-danger"></i>
165 </span>`;
166 } else if (row.user_status === this.userExpired) {
167 return `<span class="icon-label" title="${row.user_status}">
168 <i class="fas fa-user-times text-warning"></i>
169 </span>`;
170 } else if (row.user_status === this.userAlwaysActive) {
171 return `<span class="icon-label" title="${row.user_status}">
172 <i class="fas fa-user-shield text-info"></i>
173 </span>`;
174 } else {
175 return `<span>${row.user_status}</span>`;
176 }
177 }
178 },
179 account_expire_time: { title: this.translateService.instant('Expires in'), width: '10%' },
180 modified: { title: this.translateService.instant('MODIFIED'), width: '10%' },
181 created: { title: this.translateService.instant('CREATED'), width: '10%' },
kumaran.m3b4814a2020-05-01 19:48:54 +0530182 Actions: {
183 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
184 valuePrepareFunction: (cell: UserData, row: UserData): UserData => row,
185 renderComponent: UsersActionComponent
186 }
187 };
188 }
189
190 /** smart table Data Settings @public */
191 public generateSettings(): void {
192 this.settings = {
193 edit: { editButtonContent: '<i class="fa fa-edit" title="Edit"></i>', confirmSave: true },
194 delete: { deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>', confirmDelete: true },
195 columns: this.columnLists,
196 actions: { add: false, edit: false, delete: false, position: 'right' },
197 attr: this.sharedService.tableClassConfig(),
198 pager: this.sharedService.paginationPagerConfig(),
199 noDataMessage: this.translateService.instant('NODATAMSG')
200 };
201 }
202
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530203 /** To hide coulmns in smart table @public */
204 public hideColumnForUser(): void {
205 if (!this.isAdminShow) {
206 const userStatus: string = 'user_status';
207 const expire: string = 'account_expire_time';
208 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
209 delete this.columnLists[userStatus];
210 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
211 delete this.columnLists[expire];
212 } else {
213 const modified: string = 'modified';
214 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
215 delete this.columnLists[modified];
216 }
217 }
218
kumaran.m3b4814a2020-05-01 19:48:54 +0530219 /** on Navigate to Composer Page @public */
220 public composeUser(): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530221 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530222 const modalRef: NgbModalRef = this.modalService.open(AddEditUserComponent, { backdrop: 'static' });
223 modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.NEWUSER');
224 modalRef.componentInstance.userType = 'add';
225 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
226 if (result) {
227 this.sharedService.callData();
228 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530229 }).catch((): void => {
230 // Catch Navigation Error
231 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530232 }
233
234 /** smart table listing manipulation @private */
235 public onChange(perPageValue: number): void {
236 this.dataSource.setPaging(1, perPageValue, true);
237 }
238
239 /** OnUserRowSelect function @private */
240 public onUserRowSelect(event: MessageEvent): void {
241 Object.assign(event.data, { page: 'users' });
242 this.dataService.changeMessage(event.data);
243 }
244
245 /** Set up user details @public */
246 public setUserDetails(userData: UserDetail): void {
247 const userDataObj: UserData = {
248 username: userData.username,
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530249 modified: this.sharedService.convertEpochTime(!isNullOrUndefined(userData._admin) ? userData._admin.modified : null),
250 created: this.sharedService.convertEpochTime(!isNullOrUndefined(userData._admin) ? userData._admin.created : null),
kumaran.m3b4814a2020-05-01 19:48:54 +0530251 projects: userData.projectListName,
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530252 identifier: userData._id,
253 user_status: userData._admin.user_status,
SANDHYA.JS5f8c8022023-10-13 11:45:25 +0530254 account_expire_time: (!isNullOrUndefined(userData._admin) && !isNullOrUndefined(userData._admin.account_expire_time)) ? this.sharedService.convertEpochTime(
SANDHYA.JS6c686082024-06-10 21:39:41 +0530255 userData._admin.account_expire_time) : 'N/A',
256 email_id: userData.email_id
kumaran.m3b4814a2020-05-01 19:48:54 +0530257 };
258 this.userData.push(userDataObj);
259 }
260
261 /**
262 * Lifecyle hook which get trigger on component destruction
263 */
264 public ngOnDestroy(): void {
265 this.generateDataSub.unsubscribe();
266 }
267
268 /** Fetching the data from server to Load in the smarttable @protected */
269 protected generateData(): void {
270 this.isLoadingResults = true;
271 this.restService.getResource(environment.USERS_URL).subscribe((usersData: UserDetail[]) => {
272 this.userData = [];
273 usersData.forEach((userData: UserDetail) => {
274 if (userData.projects.length > 0) {
275 userData.projectListName = userData.projects.join(', ');
276 } else {
277 userData.projectListName = '';
278 }
279 this.setUserDetails(userData);
280 });
281 if (this.userData.length > 0) {
282 this.checkDataClass = 'dataTables_present';
283 } else {
284 this.checkDataClass = 'dataTables_empty';
285 }
286 this.dataSource.load(this.userData).then((data: {}) => {
287 this.isLoadingResults = false;
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530288 }).catch((): void => {
289 // Catch Navigation Error
290 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530291 }, (error: ERRORDATA) => {
292 this.restService.handleError(error, 'get');
293 this.isLoadingResults = false;
294 });
295 }
296}