EchoTex_Payroll/HRM.UI/ClientApp/src/app/app.topbar.component.ts
2024-10-14 10:01:49 +06:00

366 lines
13 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import {AppComponent} from './app.component';
import {AppMainComponent} from './app.main.component';
import {Employee} from './_models/Employee/employee';
import {AuthService} from './_services/auth/auth.service';
import {AuthGuard} from './_guards/auth.guard';
import {User} from './_models/Authentication/user';
import {ApiService} from './app.api.service';
import {MenuService} from './app.menu.service';
import {Bookmark} from './_models/Basic/bookmark';
import {HRMNotificationService} from './app.notification.service';
import {PendingJobDTO} from './_models/Work-Flow/pendingJobDTO';
import {RolePermission} from './_models/Authentication/role';
import {JwtHelperService} from '@auth0/angular-jwt';
import { EnumUserType } from './_models/enums';
import { loadingPanelService } from './hrm-loding panel/loding.panel.service';
@Component({
selector: 'app-topbar',
templateUrl: './app.topbar.component.html',
styles: [`.topbar-bar-icon {
color: #000000;
}
`]
})
export class AppTopBarComponent implements OnInit {
model: any = {};
employee: Employee;
loginName: string = '';
selectedMenuName: string = 'selected Menu';
bookmarkIcon = 'pi pi-bookmark';
currentRoute: string;
pendingJobDTOs: PendingJobDTO[] = [];
pendingJobCount: number = 0;
isSuperUser: boolean = false;
swithtypeName: string = "Super-User"
switchUserID: number = undefined;
constructor(public userService: AuthService,
public app: AppComponent,
public appMain: AppMainComponent, public loadingPanel: loadingPanelService,
public authService: AuthService,
private router: Router, public _auth: AuthGuard,
public apiService: ApiService, public menuService: MenuService, public notificationService: HRMNotificationService,) {
this._auth.menuEvent.subscribe(value => this.getloginUser());
this.bookmarkIcon = this.menuService?.clickedMenuItem?.isBookmarked ? 'pi pi-bookmark-fill' : 'pi pi-bookmark';
this.pendingJobCount = 0;
if (performance.navigation.type == performance.navigation.TYPE_RELOAD) {
this.pendingJobDTOs = JSON.parse(localStorage.getItem('pendingjobs'));
if (this.pendingJobDTOs != undefined) {
this.pendingJobCount = this.pendingJobDTOs != null || this.pendingJobDTOs != undefined
? this.pendingJobDTOs.length
: 0;
}
this.pendingJobDTOs = JSON.parse(localStorage.getItem('pendingjobs'));
this.menuService.clickedMenuItem = JSON.parse(localStorage.getItem('selectedMenu'));
this.menuService.bookMarkpages = JSON.parse(localStorage.getItem('bookmark'));
const jwtHelper = new JwtHelperService();
const user = jwtHelper.decodeToken(localStorage.getItem('token'));
if (user != undefined) {
this.loginName = user.UserName;
this.isSuperUser = true;
var spItem = localStorage.getItem('switchUserID');
if (spItem != undefined && spItem != null) {
this.isSuperUser = false;
this.swithtypeName = "Switch to Super-User";
this.switchUserID = parseInt(localStorage.getItem('switchUserID'));
}
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined) {
this.swithtypeName = "Switch to ESS";
}
}
}
if (this.pendingJobDTOs != undefined && this.pendingJobDTOs.length === 0) {
this.pendingJobDTOs = JSON.parse(localStorage.getItem('pendingjobs'));
this.pendingJobCount = this.pendingJobDTOs != null || this.pendingJobDTOs != undefined
? this.pendingJobDTOs.length
: 0;
}
//router.events.filter(event => event instanceof NavigationEnd)
// .subscribe(event => {
// this.currentRoute = event.url;
// console.log(event);
// });
//this.userService.GetLogInUserType().subscribe(
// (resc: any) => {
// console.log(resc);
// },
// (errmenu: any) => {
// },
// () => {
// }
//);
}
ngOnInit() {
//this.loadBookmark();
this.loadPendingJobs();
}
loadBookmark() {
this.authService.subject.subscribe(
(resp) => {
this.menuService.bookMarkpages = resp;
},
() => {
},
() => {
},
);
}
loadPendingJobs() {
this.authService.subject2.subscribe(
(resp) => {
this.pendingJobDTOs = resp;
this.pendingJobCount = this.pendingJobDTOs.length;
},
() => {
},
() => {
},
);
}
getloginUser() {
debugger;
const jwtHelper = new JwtHelperService();
const user = jwtHelper.decodeToken(localStorage.getItem('token'));
this.loginName = user.UserName;
this.isSuperUser = true;
localStorage.removeItem('switchUserID');
this.switchUserID = undefined;
if ((user.UserType == EnumUserType.Employee || user.UserType == EnumUserType.User) && user.EmployeeID != undefined) {
// if (this.apiService.isSSO ==true && user.UserType == EnumUserType.User && user.EmployeeID != undefined) {
this.isSuperUser = false;
console.log(user);
this.swithtypeName = "Switch to Super-User";
this.switchUserID = undefined;
var userid = undefined;
this.authService.getSwitchUserId(EnumUserType.SuperUser).subscribe(next => {
userid = next;
}, error => {
this.swithtypeName = undefined;
console.log(error);
}, () => {
if (userid != 0){
this.switchUserID = userid;
localStorage.setItem('switchUserID', JSON.stringify(this.switchUserID));
}
});
}
//else (this.apiService.isSSO ==true && user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined)
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined)
{
this.swithtypeName = "Switch to ESS";
}
//this.loginName = '';
//var ouser: User = undefined;
//this.userService.GetCurrentUser().subscribe(
// (resp) => {
// ouser = resp;
// },
// () => {
// },
// () => {
// if (ouser != undefined) {
// this.loginName = ouser.userName;
// if (this.loginName.length > 6) {
// this.loginName = this.loginName.substring(0, 6);
// }
// }
// },
//);
}
//ngOnInit() {
// this.employee = new Employee();
// this.loadData();
//}
loadData() {
//this.employeeService.getCurrentEmployee().subscribe(
// (resp) => {
// this.employee = resp;
// },
// () => { },
// () => {
// },
//);
}
login() {
this.authService.login(this.model).subscribe(next => {
//show notification
}, error => {
console.log(error);
}, () => {
this.router.navigate(['/training']);
});
}
loggedIn() {
return this.authService.loggedIn();
}
SwitchUser() {
const jwtHelper = new JwtHelperService();
const user = jwtHelper.decodeToken(localStorage.getItem('token'));
if (user.UserType != EnumUserType.SuperUser) {
user.UserID = this.switchUserID;
user.UserType = EnumUserType.SuperUser;
this.swithtypeName = "Switch to ESS";
}
else {
user.UserType = EnumUserType.Employee;
this.swithtypeName = "Switch to Super-User";
}
return this.switchTheUser(user);
}
switchTheUser(oUser: User) {
const data = {
user: oUser,
payrollTypeId: oUser.payrollTypeID,
};
this.loadingPanel.ShowLoadingPanel = true;
this.userService.SwtichUserMode(data).subscribe(
(resp: any) => {
localStorage.setItem('token', resp);
ApiService.AuthenticationToken = resp;
},
(err: any) => {
this.notificationService.showError('Invalid Login-ID or Password');
this.loadingPanel.ShowLoadingPanel = false;
},
() => {
const jwtHelper = new JwtHelperService();
const user = jwtHelper.decodeToken(localStorage.getItem('token'));
if (user.ChangePasswordAtNextLogon === 'True') {
localStorage.removeItem('menuList');
this._auth.menuEvent.emit(false);
} else {
this.userService.GetRolePermissionbyUserID().subscribe(
(menu: any) => {
localStorage.setItem('menuList', JSON.stringify(menu));
},
(errmenu: any) => {
},
() => {
this._auth.menuEvent.emit(true);
this.loadingPanel.ShowLoadingPanel = false;
if (user.UserType == EnumUserType.Employee) {
this.router.navigate(['/ess/ess-dashboard']);
} else {
this.router.navigate(['/']);
}
}
);
}
}
);
}
logout() {
this.authService.logout();
//localStorage.removeItem('forgotpass');
//localStorage.removeItem('token');
//localStorage.removeItem('menuList');
////show notification
//this.router.navigate(['/login']);
}
onBookMark() {
const bookmark: Bookmark = new Bookmark();
if (this.menuService.clickedMenuItem !== undefined) {
if (this.menuService.clickedMenuItem.isBookmarked === false) {
bookmark.link = this.menuService.clickedMenuItem.link;
bookmark.description = this.menuService.clickedMenuItem.name;
bookmark.menuCode = this.menuService.clickedMenuItem.menuKey;
this.menuService.bookMarkpages.push(bookmark);
// this.bookmarkIcon = 'pi pi-bookmark-fill' ;
this.menuService.clickedMenuItem.isBookmarked = true;
this.authService.SaveBookmark(bookmark).subscribe(
(x) => {
},
(x) => {
this.notificationService.showError(x.error);
},
() => {
this.notificationService.showSuccess('You have successfully bookmarked this page.');
this.getBookmark();
localStorage.setItem('selectedMenu', JSON.stringify(this.menuService.clickedMenuItem));
});
} else {
bookmark.link = this.menuService.clickedMenuItem.link;
bookmark.description = this.menuService.clickedMenuItem.name;
bookmark.menuCode = this.menuService.clickedMenuItem.menuKey;
const index = this.menuService.bookMarkpages.findIndex(x => x.menuCode === bookmark.menuCode);
this.menuService.bookMarkpages.splice(index, 1);
this.menuService.clickedMenuItem.isBookmarked = false;
this.authService.deleteBookmarkByMenuKey(bookmark).subscribe(
(x) => {
},
(x) => {
this.notificationService.showError(x.error);
},
() => {
this.notificationService.showSuccess('You have successfully removed bookmark this page.');
this.getBookmark();
localStorage.setItem('selectedMenu', JSON.stringify(this.menuService.clickedMenuItem));
});
}
}
}
bookMarkedLinkClicked(b: Bookmark) {
this.menuService.clickedMenuItem = new RolePermission();
this.menuService.clickedMenuItem.name = b.description;
this.menuService.clickedMenuItem.menuKey = b.menuCode;
this.menuService.clickedMenuItem.isBookmarked = true;
this.router.navigate([b.link]);
}
getBookmark() {
this.userService.getBookmarks()
.subscribe(
(resp: any) => {
if (resp != undefined) {
this.menuService.bookMarkpages = resp;
}
},
(x) => {
},
() => {
localStorage.removeItem('bookmark');
localStorage.setItem('bookmark', JSON.stringify(this.menuService.bookMarkpages));
});
}
}