import {Injectable} from '@angular/core'; import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router'; import {PatientContextService} from '../services/patient-context.service'; import {appUrls} from '../../utils/URL_STRING_LITERALS'; @Injectable({ providedIn: 'root' }) export class PrivatelyMarkedGuard implements CanActivate { constructor(private router: Router, private patientContextService: PatientContextService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { const needConsent = !!this.patientContextService.getCurrentHasPrivatelyMarkedData() && !this.patientContextService.getCurrentXBreakTheGlassReason(); if (needConsent) { // If we didn't come from privatelyMarkedUrl then redirect. Avoids infinite-loop in negate-privately-marked.guard if (!state.url.includes(appUrls.privatelyMarked)) { this.router.navigate([appUrls.privatelyMarked]); } return false; } return true; } }