File

src/app/backend.service.ts

Index

Methods

Constructor

constructor(serverUrl: string, http: HttpClient)
Parameters :
Name Type Optional
serverUrl string No
http HttpClient No

Methods

codeLogin
codeLogin(code: string)
Parameters :
Name Type Optional
code string No
getBookletData
getBookletData(bookletId: string)
Parameters :
Name Type Optional
bookletId string No
Returns : Observable<BookletData>
getGroupData
getGroupData(groupName: string)
Parameters :
Name Type Optional
groupName string No
getSessionData
getSessionData()
getSysCheckInfo
getSysCheckInfo()
getSysConfig
getSysConfig()
getWorkspaceData
getWorkspaceData(workspaceId: string)
Parameters :
Name Type Optional
workspaceId string No
login
login(name: string, password: string)
Parameters :
Name Type Optional
name string No
password string No
nameOnlyLogin
nameOnlyLogin(name: string)
Parameters :
Name Type Optional
name string No
startTest
startTest(bookletName: string)
Parameters :
Name Type Optional
bookletName string No
Returns : Observable<string | number>
import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import {
  SysCheckInfo,
  AuthData,
  WorkspaceData,
  BookletData, ApiError, AccessObject
} from './app.interfaces';
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> {
    if (password) {
      return this.http
        .put<AuthData>(`${this.serverUrl}session/admin`, { name, password })
        .pipe(
          catchError((err: ApiError) => {
            console.warn(`login Api-Error: ${err.code} ${err.info} `);
            return of(err.code);
          }),
          switchMap(authData => {
            if (typeof authData === 'number') {
              const errCode = authData as number;
              if (errCode === 400) {
                return this.http
                  .put<AuthData>(`${this.serverUrl}session/login`, { name, password })
                  .pipe(catchError((err: ApiError) => of(err.code)));
              }
              return of(errCode);
            }
            return of(authData);
          })
        );
    }
    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} `);
          return of(err.code);
        })
      );
  }

  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} `);
          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,
          role: 'n.d.'
        });
      }));
  }

  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))
      );
  }

  getBookletData(bookletId: string): Observable<BookletData> {
    return this.http
      .get<BookletData>(`${this.serverUrl}booklet/${bookletId}/data`)
      .pipe(
        map(bData => {
          bData.id = bookletId;
          return bData;
        }),
        catchError(() => of(<BookletData>{
          id: bookletId,
          label: bookletId,
          locked: true,
          running: false
        }))
      );
  }

  startTest(bookletName: string): Observable<string | number> {
    return this.http
      .put<number>(`${this.serverUrl}test`, { bookletName })
      .pipe(
        map((testId: number) => String(testId)),
        catchError((err: ApiError) => of(err.code))
      );
  }

  getSysConfig(): Observable<SysConfig> {
    return this.http
      .get<SysConfig>(`${this.serverUrl}system/config`)
      .pipe(catchError(() => of(null)));
  }

  getSysCheckInfo(): Observable<SysCheckInfo[]> {
    return this.http
      .get<SysCheckInfo[]>(`${this.serverUrl}sys-checks`)
      .pipe(
        catchError(() => of([]))
      );
  }
}

results matching ""

    No results matching ""