import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { environment } from '@environment'; import { Applicant } from '../typings/applicant.typing'; import { User, UserTypes } from '../typings/client-user.typing'; export type PortalTypes = 'apply'|'platform'|'management'; @Injectable({ providedIn: 'root' }) export class PortalDeterminationService { _routeBase: string; protected _prefix: string; private restrictedPrefixes = [ 'apply', 'platform' ]; constructor ( private router: Router ) { this._prefix = location.hostname .replace(environment.locationBase, '') .replace('.', ''); } get isTest () { return environment.isTest; } get isManager () { return this.routeBase === 'management' || location.pathname.startsWith('/management'); } get isApply () { return this.routeBase === 'apply' || location.pathname.startsWith('/apply'); } get isPlatform () { return this.routeBase === 'platform' || location.pathname.startsWith('/platform'); } get isOnCustom () { return !!this.getCurrentPrefix(); } get routeBase () { if (!this._routeBase) { this._routeBase = environment.routeBase; } return this._routeBase; } /** * Checks if the user is on a subdomain or not (including apply or platform) */ getIsOnSubdomain () { return location.hostname !== environment.locationBase; } getRoute (route: string): string { return `${this.isManager ? '/management/' : this.isPlatform ? '/platform/' : '/apply/' }${route}`.replace('//', '/'); } checkIfIsUser (arg: User|Applicant): arg is User { return this.isManager; } navigate (route: string) { this.router.navigateByUrl(this.getRoute(route)); } getCurrentPrefix () { if (this.restrictedPrefixes.includes(this._prefix)) { return ''; } return this._prefix; } getUserType () { return this.isManager ? UserTypes.MANAGER : UserTypes.APPLICANT; } }