import { Base } from './base'; import { BehaviorSubject } from 'rxjs'; export enum ClaimLevel { notSet = 0, employee = 1, supervisor = 2, administrator = 3, akiba = 4 } export class Claims extends Base { akiba: boolean; administrator: boolean; supervisor: boolean; employee: boolean; role: number; private currentClaimLevel: ClaimLevel; sideBarItems = new Array(); menuItems = new Array(); claimLevelChange: BehaviorSubject = new BehaviorSubject(0); environment: any; constructor(data: any | null = null) { super(); this.akiba = false; this.administrator = false; this.supervisor = false; this.employee = true; this.role = 0; this.currentClaimLevel = ClaimLevel.notSet; this.load(data); if (this.employee) { this.currentClaimLevel = ClaimLevel.employee; } if (this.supervisor) { this.currentClaimLevel = ClaimLevel.supervisor; } if (this.administrator) { this.currentClaimLevel = ClaimLevel.administrator; } if (this.akiba) { this.currentClaimLevel = ClaimLevel.akiba; } } async loadAccessForClaim(): Promise { let mi: Array; switch (this.currentClaimLevel) { case 1: this.sideBarItems = this.environment.sidebarRoutes.employee.tabs; mi = this.environment.sidebarRoutes.employee.menus; this.claimLevelChange.next(1); break; case 2: this.sideBarItems = this.supervisor ? this.environment.sidebarRoutes.supervisor.tabs : []; mi = this.supervisor ? this.environment.sidebarRoutes.supervisor.menus : []; this.claimLevelChange.next(2); break; case 3: this.sideBarItems = this.administrator ? this.environment.sidebarRoutes.administrator.tabs : []; mi = this.administrator ? this.environment.sidebarRoutes.administrator.menus : []; this.claimLevelChange.next(3); break; case 4: this.sideBarItems = this.akiba ? this.environment.sidebarRoutes.akiba.tabs : []; mi = this.akiba ? this.environment.sidebarRoutes.akiba.menus : []; this.claimLevelChange.next(4); break; default: this.sideBarItems = []; this.claimLevelChange.next(1); break; } if (!this.menuItems.length) { this.menuItems = mi; } return true; } async matchPath(path: string): Promise { let tempClaimLevel = 0; const found = this.menuItems.some(element => { const aPath: string = element.path; if (path.startsWith(aPath)) { tempClaimLevel = element.claimLevel; return true; } }); if (found) { this.currentClaimLevel = tempClaimLevel; } return found; } hasValidClaim(): boolean { return this.currentClaimLevel !== ClaimLevel.notSet; } get currentClaim(): ClaimLevel { return this.currentClaimLevel; } }