All files / src/app/shared/services/maindata maindata.service.ts

27.02% Statements 20/74
0% Branches 0/14
12.5% Functions 3/24
27.77% Lines 20/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                1x         1x 1x                           1x         1x             1x 1x             1x           1x 1x 1x 1x   1x 1x 1x   1x                                                           1x 1x 1x   1x                                                                                                                                
import { Injectable } from '@angular/core';
import {
  BehaviorSubject, Observable, ReplaySubject, Subject
} from 'rxjs';
import { Router } from '@angular/router';
import { distinct } from 'rxjs/internal/operators/distinct';
import { shareReplay } from 'rxjs/internal/operators/shareReplay';
import { filter } from 'rxjs/operators';
import { CustomtextService } from '../customtext/customtext.service';
import {
  AccessObject, AppError, AuthAccessType, AuthData
} from '../../../app.interfaces';
import { AppConfig } from '../../classes/app.config';
import { BackendService } from '../backend.service';
import { SysStatus } from '../../interfaces/service-status.interfaces';
 
const localStorageAuthDataKey = 'iqb-tc-a';
 
@Injectable({
  providedIn: 'root'
})
export class MainDataService {
  private _appError$ = new ReplaySubject<AppError | void>(0);
  get appError$(): Observable<AppError> {
    return this._appError$
      .pipe(
        filter((v: AppError | void): v is AppError => v !== undefined),
        distinct(error => (error?.stack ?? '') + (error?.errorId ?? '')),
        shareReplay(0)
      );
  }
 
  set appError(error: AppError) {
    this._appError$.next(error);
  }
 
  private _authData$ = new BehaviorSubject<AuthData | null>(null);
  get authData$(): Observable<AuthData | null> {
    return this._authData$.asObservable();
  }
 
  private _appConfig$ = new BehaviorSubject<AppConfig | null>(null);
  // TODO remove this function everywhere and replace with appConfig$ and wait unit it's there to avoid race conditions
  get appConfig(): AppConfig | null {
    return this._appConfig$.getValue();
  }
 
  get appConfig$(): Observable<AppConfig> {
    return this._appConfig$
      .pipe(filter((v): v is AppConfig => v instanceof AppConfig));
  }
 
  set appConfig$(appConfig: AppConfig) {
    this._appConfig$.next(appConfig);
  }
 
  sysStatus: SysStatus = {
    fileService: 'unknown',
    broadcastingService: 'unknown',
    cacheService: 'unknown'
  };
 
  sysCheckAvailableForAll = false;
  appTitle$ = new BehaviorSubject<string>('IQB-Testcenter');
  appSubTitle$ = new BehaviorSubject<string>('');
  globalWarning = '';
 
  postMessage$ = new Subject<MessageEvent>();
  appWindowHasFocus$ = new Subject<boolean>();
  isFullScreen: boolean = false;
 
  isExtendedKbUsed: boolean | null = null;
 
  getAuthData(): AuthData | null {
    Iif (this._authData$.getValue()) {
      return this._authData$.getValue();
    }
    try {
      const entry = localStorage.getItem(localStorageAuthDataKey);
      Iif (!entry) {
        return null;
      }
      return JSON.parse(entry);
    } catch (e) {
      return null;
    }
  }
 
  getAccessObject(type: AuthAccessType, id: string): AccessObject {
    const authData = this.getAuthData();
    Iif (!authData) {
      throw new AppError({ type: 'session', description: '', label: 'Nicht angemeldet' });
    }
    const claim = authData.claims[type].find(accessObject => accessObject.id === id);
    Iif (!claim) {
      throw new AppError({ type: 'session', description: '', label: `${type} ${id} nicht freigegeben.` });
    }
    return claim;
  }
 
  constructor(
    private cts: CustomtextService,
    private bs: BackendService,
    private router: Router
  ) {
    this.appConfig$.subscribe(appConfig => {
      this.appTitle$.next(appConfig.appTitle);
      appConfig.applyBackgroundColors();
      this.globalWarning = appConfig.warningMessage;
      const authData = this.getAuthData();
      Iif (authData) {
        this.cts.addCustomTexts(authData.customTexts);
      }
    });
  }
 
  setAuthData(authData: AuthData): void {
    this._authData$.next(authData);
    Iif (this.appConfig?.customTexts) {
      this.cts.addCustomTexts(this.appConfig.customTexts);
    }
    Iif (authData.customTexts) {
      this.cts.addCustomTexts(authData.customTexts);
    }
    localStorage.setItem(localStorageAuthDataKey, JSON.stringify(authData));
  }
 
  logOut(): void {
    this.cts.restoreDefault(true);
    this.bs.deleteSession()
      .subscribe(() => {
        this.quit();
      });
  }
 
  quit(): void {
    this._authData$.next(null);
    localStorage.removeItem(localStorageAuthDataKey);
    this.router.navigate(['/']);
  }
 
  resetAuthData(): void {
    const storageEntry = localStorage.getItem(localStorageAuthDataKey);
    Iif (storageEntry) {
      localStorage.removeItem(localStorageAuthDataKey);
    }
    this._authData$.next(this.getAuthData());
  }
 
  reloadPage(logOut: boolean = false): void {
    Iif (logOut) {
      this._authData$.next(null);
      localStorage.removeItem(localStorageAuthDataKey);
    }
    this.bs.clearCache();
    setTimeout(() => { window.location.href = '/'; }, 100);
  }
 
  refreshSysStatus(): void {
    this.bs.getSysStatus()
      .subscribe(sysStatus => {
        this.sysStatus = sysStatus;
      });
  }
 
  clearErrorBuffer(): void {
    this._appError$.next();
  }
}