import {Component, EventEmitter, OnInit, Output, Input, ChangeDetectionStrategy} from '@angular/core'; import {FormControl, FormGroup} from '@angular/forms'; import {Role} from '../../../_models/Authentication/role'; import {User} from '../../../_models/Authentication/user'; import {AuthService} from '../../../_services/auth/auth.service'; import {HRMNotificationService} from '../../../app.notification.service'; import {ActivatedRoute, Router} from '@angular/router'; import {EnumExtension, EnumRoleType, EnumUserType, EnumAuthStatus} from '../../../_models/enums'; import {SelectableSettings} from '@progress/kendo-angular-grid'; import {UserRole} from '../../../_models/Authentication/UserRole'; import { SearchEmployee } from '../../../_models/Employee/searchEmployee'; import { PayrollType } from '../../../_models/Authentication/payrollType'; import { EmployeeServices } from '../../../_services/employee/employee.service'; import { UntilityHandlerService } from '../../../utility.hanldler.service'; import { LoadingPanelComponent } from 'src/app/hrm-loding panel/loading-panel.component'; import { ButtonModule } from '@progress/kendo-angular-buttons'; import { DropDownListModule } from '@progress/kendo-angular-dropdowns'; import { ReactiveFormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms'; import { KENDO_GRID } from '@progress/kendo-angular-grid'; import { DatePickerModule } from '@progress/kendo-angular-dateinputs'; import { ApiService } from 'src/app/app.api.service'; @Component({ selector: 'app-user-role-entry', templateUrl: './user-role-entry.component.html', styleUrls: ['./user-role-entry.component.css'], changeDetection: ChangeDetectionStrategy.Eager, standalone: false, }) export class UserRoleEntryComponent implements OnInit { userRolePermissionForm: FormGroup; public active = false; _userRoles: UserRole[]; _allroles: Role[]; _gridRoles: Role[]; _users: User[]; _gridUsers: User[]; _selectedUserID: number; _previousRoles: number[] = []; _selectedRoles: number[] = []; _selectedRoleType: EnumRoleType ; roletypeEnum: typeof EnumRoleType = EnumRoleType; _payrollTypes: PayrollType[]; _selectedPayrollTypeid: number; roleTypes: any = EnumExtension.getNamesAndValues(EnumRoleType); public selectableSettings: SelectableSettings; @Output() saveCompleted: EventEmitter = new EventEmitter(); _selectedEmployees: SearchEmployee[]; _selectedLoginIDs: string[]; _initializeValue : boolean; constructor(public userService: AuthService, public apiService: ApiService, public notificationService: HRMNotificationService, public router: Router, public route: ActivatedRoute, public employeeService: EmployeeServices, public utilityService: UntilityHandlerService) { } @Input() public set InitializeForm(initializeValue: boolean) { this._initializeValue = initializeValue; if (this._initializeValue == true) { this._gridRoles = []; this._gridUsers = []; this._selectedEmployees = []; this._selectedLoginIDs = []; this._selectedPayrollTypeid = undefined; this._selectedRoleType = undefined; } } ngOnInit() { this.utilityService.ShowLoadingPanel = true; this._selectedRoleType = null; this._gridUsers = []; this._gridRoles = []; this._selectedUserID = 0; this._selectedLoginIDs = []; this.userRolePermissionForm = new FormGroup({ selectedUsers: new FormControl() }); this.userService.GetUsers(EnumUserType.User) .subscribe( (resp: any) => { this._users = resp; }, (err: any) => { // ON ERROR }, () => { // ON Success // this.loadGrid(); } ); this.userService.getAllRoles() .subscribe( (resp: any) => { this._allroles = resp; }, (err: any) => { // ON ERROR }, () => { this.pageLoaded(); } ); this.userService.GetAllPayrollTypes() .subscribe( (resp: any) => { this._payrollTypes = resp; }, (err: any) => { //ON ERROR }, () => { } ); this.setSelectableSettings(); } pageLoaded() { this.utilityService.ShowLoadingPanel = false; } setSelectableSettings(): void { this.selectableSettings = { checkboxOnly: true, mode: 'multiple', // this.mode }; } onRoleChange(value: any) { this._gridRoles = []; this._selectedRoles = []; this._gridUsers = []; this._gridRoles = this._allroles.filter(x => x.roleType === this._selectedRoleType); } SearchUnAssingedEmp() { this.utilityService.ShowLoadingPanel = true; this.employeeService.GetEmployeeNotYetUser(this._selectedPayrollTypeid) .subscribe( (resp: any) => { this._selectedEmployees = resp; }, (err: any) => { //ON ERROR this.utilityService.ShowLoadingPanel = false; }, () => { this.utilityService.ShowLoadingPanel = false; this._selectedEmployees.forEach(x => { var ouser: User = new User(); ouser.loginID = x.employeeNo; ouser.userName = x.name; ouser.employeeID = x.employeeID; var Item = this._gridUsers.find(u => u.employeeID == x.employeeID); if (Item == undefined) this._gridUsers.push(ouser) }); } ); } public onSelectionRoleChange(event: any): void { // this._selectedRoles = event.selectedRows.map(r => r.dataItem.id); } public Remove(nIndex:number) { this._gridUsers.splice(nIndex, 1); } public onSave(): void { if (this._gridUsers.length == 0) { this.notificationService.showError("There is no user/employee selected"); return; } if (this._selectedRoles.length == 0) { this.notificationService.showError("please select a role"); return; } let userRoles: UserRole[] = []; this._gridUsers.forEach(u => { this._selectedRoles.forEach(r => { var userRole: UserRole = new UserRole(); if (this._selectedRoleType == EnumRoleType.Self_Service) { userRole.payrollTypeID = this._selectedPayrollTypeid; userRole.employeeID = u.employeeID; userRole.roleType = EnumRoleType.Self_Service; } if (this._selectedRoleType == EnumRoleType.Admin) { userRole.userID = u.id; userRole.roleType = EnumRoleType.Admin; } userRole.loginIDView = u.loginID; userRole.roleID = r; userRoles.push(userRole); }); }); this.utilityService.ShowLoadingPanel = true; if(userRoles && userRoles.length > 0) { userRoles.forEach(x=> { let isFind = this._previousRoles.some(y => y === x.roleID); if(!isFind){ x.isOld = false; let role = this._gridRoles.find(z => z.id === x.roleID); if(role && role.id > 0){ x.newRoleName = role.name; } } }); } this.userService.saveUserRole(userRoles) .subscribe( (resp: any) => { }, (err: any) => { //ON ERROR this.notificationService.showSuccess(err.error); this.utilityService.ShowLoadingPanel = false; }, () => { this.saveCompleted.emit(true); this.utilityService.ShowLoadingPanel = false; this.notificationService.showSuccess("Data Saved Successfully."); } ); } public GetSelectedEmployee(childData) { this._selectedEmployees = childData; if (this._selectedEmployees == undefined) { return; } this._selectedEmployees.forEach(x => { var ouser: User = new User(); ouser.loginID = x.employeeNo; ouser.userName = x.name; ouser.employeeID = x.employeeID; var Item = this._gridUsers.find(u => u.employeeID == x.employeeID); if (Item == undefined) this._gridUsers.push(ouser); }); this.AddEmployeeAsUser(); } AddUser() { this.userService.getUserRoles(EnumRoleType.Admin, this._selectedUserID); let oUser: User = this._users.find(x => x.id === this._selectedUserID); let userRole: UserRole[]; this.userService.getUserRoles(EnumRoleType.Admin, this._selectedUserID) .subscribe( (resp: any) => { this._userRoles = resp; }, (err: any) => { // ON ERROR }, () => { const Users: User[] = []; Users.push(oUser); var uRole: UserRole = new UserRole(); var Item = this._gridUsers.find(u => u.id == oUser.id); if (Item == undefined) this._gridUsers.push(oUser); this.setPrviousRole(); } ); } private setPrviousRole() { this._gridUsers.forEach(x => { var empUseRoles: UserRole[] = this._userRoles.filter(t => t.loginIDView == x.loginID) if (empUseRoles != undefined) { empUseRoles.forEach(u => { if (x.roleNames == undefined) x.roleNames = ''; x.roleNames = x.roleNames + u.roleNameView + ", "; x.id = u.userID; if (this._selectedRoles.findIndex(r => r == u.roleID) == -1) { this._selectedRoles.push(u.roleID); } }) } }); this._selectedRoles = [...this._selectedRoles]; this._previousRoles = [...this._selectedRoles]; } AddEmployeeAsUser() { this.userService.GetEmpUserRole(this._selectedEmployees).subscribe( (resp: any) => { this._userRoles = resp; }, (err: any) => { }, () => { this.setPrviousRole(); } ); return; this._users = []; this._selectedEmployees.forEach(x => { var ouser: User = new User(); ouser.loginID = x.employeeNo; ouser.userName = x.name; oUser.userStatus = EnumAuthStatus.New_Not_Yet_Approve; this._users.push(oUser); }); this.userService.getUserRoles(EnumRoleType.Admin, this._selectedUserID); let oUser: User = this._users.find(x => x.id === this._selectedUserID); let userRole: UserRole[]; this.userService.getUserRoles(EnumRoleType.Admin, this._selectedUserID) .subscribe( (resp: any) => { userRole = resp; }, (err: any) => { // ON ERROR }, () => { const Users: User[] = []; Users.push(oUser); /*var uRole: UserRole = new UserRole(); uRole.getRoleNames(Users, this.userRoles);*/ var uRole: UserRole = new UserRole(); // uRole.getRoleNames(Users, userRole); this._gridUsers.push(oUser); } ); } public onCancel(e): void { this.saveCompleted.emit(false); } private closeForm(): void { } }