Skip to content
Snippets Groups Projects
backend.service.ts 4.53 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Injectable, Inject } from '@angular/core';
    
    mechtelm's avatar
    mechtelm committed
    import {HttpClient} from '@angular/common/http';
    
    import {Observable, of} from 'rxjs';
    
    mechtelm's avatar
    mechtelm committed
    import {catchError, map, switchMap} from 'rxjs/operators';
    
        SysCheckInfo,
        AuthData,
        WorkspaceData,
        BookletData, ApiError, AccessObject
    
    } from './app.interfaces';
    
    paf's avatar
    paf committed
    import {SysConfig} from './config/app.config';
    
    
    @Injectable({
      providedIn: 'root'
    })
    
    export class BackendService {
    
      constructor(
    
        @Inject('SERVER_URL') private readonly serverUrl: string,
    
        private http: HttpClient
      ) {}
    
      login(name: string, password: string): Observable<AuthData | number> {
    
    mechtelm's avatar
    mechtelm committed
        if (password) {
          return this.http
    
            .put<AuthData>(this.serverUrl + 'session/admin', {name, password})
    
              catchError((err: ApiError) => {
                console.warn(`login Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
                return of(err.code);
    
              switchMap(authData => {
                if (typeof authData === 'number') {
                  const errCode = authData as number;
                  if (errCode === 400) {
    
    mechtelm's avatar
    mechtelm committed
                      .put<AuthData>(this.serverUrl + 'session/login', {name, password})
    
                      .pipe(catchError((err: ApiError) => of(err.code)));
    
                    return of(errCode);
    
                  return of(authData);
    
    paflov's avatar
    paflov committed
            );
    
    mechtelm's avatar
    mechtelm committed
        } else {
          return this.nameOnlyLogin(name);
        }
      }
    
      nameOnlyLogin(name: string): Observable<AuthData | number> {
        return this.http
          .put<AuthData>(this.serverUrl + 'session/login', {name})
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`nameOnlyLogin Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(err.code);
    
    mechtelm's avatar
    mechtelm committed
          );
    
      codeLogin(code: string): Observable<AuthData | number> {
        return this.http
          .put<AuthData>(this.serverUrl + 'session/person', {code})
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`codeLogin Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(err.code);
    
      getWorkspaceData(workspaceId: string): Observable<WorkspaceData> {
        return this.http
    
          .get<WorkspaceData>(this.serverUrl + 'workspace/' + workspaceId)
          .pipe(catchError(() => {
            console.warn('get workspace data failed for ' + workspaceId);
            return of(<WorkspaceData>{
              id: workspaceId,
              name: workspaceId,
    
    paf's avatar
    paf committed
              role: 'n.d.'
            });
    
    paf's avatar
    paf committed
      getGroupData(groupName: string): Observable<AccessObject> {
    
        // TODO find consistent terminology. in XSD they are called name & label and likewise (mostly) in newer BE-versions
        interface NameAndLabel {
            name: string;
            label: string;
        }
    
        return this.http
            .get<NameAndLabel>(this.serverUrl + 'monitor/group/' + groupName)
            .pipe(map((r: NameAndLabel): AccessObject => ({id: r.name, name: r.label})))
            .pipe(catchError(() => {
                console.warn('get group data failed for ' + groupName);
                return of(<AccessObject>{
                    id: groupName,
                    name: groupName,
                });
            }));
    
      getSessionData(): Observable<AuthData | number> {
        return this.http
          .get<AuthData>(this.serverUrl + 'session')
          .pipe(
    
            catchError((err: ApiError) => of(err.code))
    
    paf's avatar
    paf committed
          );
    
      getBookletData(bookletId: string): Observable<BookletData> {
        return this.http
    
          .get<BookletData>(this.serverUrl + 'booklet/' + bookletId + '/data')
    
          .pipe(
            map(bData => {
              bData.id = bookletId;
    
    paf's avatar
    paf committed
              return bData;
    
            }),
            catchError(() => {
    
            console.warn('get booklet data failed for ' + bookletId);
            return of(<BookletData>{
    
              id: bookletId,
    
              label: bookletId,
    
    mechtelm's avatar
    mechtelm committed
              locked: true,
              running: false
    
    paf's avatar
    paf committed
            });
    
      startTest(bookletName: string): Observable<string | number> {
    
        return this.http
    
          .put<number>(this.serverUrl + 'test', {bookletName})
    
    mechtelm's avatar
    mechtelm committed
          .pipe(
    
            map((testId: number) => String(testId)),
    
            catchError((err: ApiError) => of(err.code))
    
    mechtelm's avatar
    mechtelm committed
          );
    
          .get<SysConfig>(this.serverUrl + `system/config`)
    
    paf's avatar
    paf committed
          .pipe(catchError(() => of(null)));
    
    mechtelm's avatar
    mechtelm committed
      getSysCheckInfo(): Observable<SysCheckInfo[]> {
    
        return this.http
          .get<SysCheckInfo[]>(this.serverUrl + 'sys-checks')
          .pipe(
            catchError(() => {
    
    mechtelm's avatar
    mechtelm committed
              return of([]);