Skip to content
Snippets Groups Projects
backend.service.ts 5.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Martin Mechtel's avatar
    Martin Mechtel committed
    
    
    import { Injectable, Inject } from '@angular/core';
    
    paflov's avatar
    paflov committed
    import {HttpClient, HttpParams} from '@angular/common/http';
    
    import {Observable, of} from 'rxjs';
    
    import {catchError, switchMap} from 'rxjs/operators';
    
    import {
      LoginData,
      BookletStatus,
      PersonTokenAndTestId,
      SysConfig,
      SysCheckInfo,
      AuthData,
    
      WorkspaceData,
    
      BookletData, MonitorScopeData
    
    } from './app.interfaces';
    
    paflov's avatar
    paflov committed
    import {ErrorHandler, ServerError} from 'iqb-components';
    
    // ============================================================================
    
    @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(errCode => of(errCode)),
              switchMap(authData => {
                if (typeof authData === 'number') {
                  const errCode = authData as number;
                  if (errCode === 400) {
    
                      .put<LoginData>(this.serverUrl + 'session/login', {name, password})
    
                      .pipe(catchError(errCode => of(errCode)));
    
                    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(errCode => of(errCode))
          );
    
      codeLogin(code: string): Observable<AuthData | number> {
        return this.http
          .put<AuthData>(this.serverUrl + 'session/person', {code})
          .pipe(
            catchError(errCode => of(errCode))
          );
      }
    
    
      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."
                })
              }));
      }
    
    
      getMonitorScopeData(monitorScopeId: string): Observable<MonitorScopeData> {
        return this.http
          .get<MonitorScopeData>(this.serverUrl + 'monitorscope/' + monitorScopeId) // TODO fix route
          .pipe(catchError(() => {
            console.warn('get monitor scope data failed for ' + monitorScopeId);
            return of(<MonitorScopeData>{
              id: monitorScopeId,
              name: monitorScopeId,
              type: "n.d."
            })
          }));
      }
    
    
      getSessionData(): Observable<AuthData | number> {
        return this.http
          .get<AuthData>(this.serverUrl + 'session')
          .pipe(
            catchError(errCode => of(errCode))
          )
      }
    
      getBookletData(bookletId: string): Observable<BookletData> {
        return this.http
          .get<BookletData>(this.serverUrl + 'booklet/' + bookletId)
          .pipe(catchError(() => {
            console.warn('get booklet data failed for ' + bookletId);
            return of(<BookletData>{
              label: bookletId,
              isEnabled: false,
              statusText: "not found"
            })
          }));
      }
    
    
    
    paflov's avatar
    paflov committed
      getSession(loginToken: string, personToken: string): Observable<LoginData | ServerError> {
    
        const authToken = JSON.stringify({l: loginToken, p: personToken});
    
        return this.http
    
          .get<LoginData>(this.serverUrl + 'session', {headers: {'AuthToken': authToken}})
    
    paflov's avatar
    paflov committed
          .pipe(catchError(ErrorHandler.handle));
    
      getAdminSession(adminToken: string): Observable<LoginData | ServerError> {
        const authToken = JSON.stringify({at: adminToken});
    
        return this.http
    
          .get<LoginData>(this.serverUrl + 'session', {headers: {'AuthToken': authToken}})
          .pipe(catchError(ErrorHandler.handle));
    
          .get<SysConfig>(this.serverUrl + `system/config`)
    
      public getSysCheckInfo(): Observable<SysCheckInfo[]> {
        return this.http
          .get<SysCheckInfo[]>(this.serverUrl + 'sys-checks')
          .pipe(
            catchError(() => {
              const myreturn: SysCheckInfo[] = [];
              return of(myreturn);
            })
          );
      }
    
    paflov's avatar
    paflov committed
      getBookletState(bookletName: string, code = ''): Observable<BookletStatus | ServerError> {
    
    
    paflov's avatar
    paflov committed
        // TODO after https://github.com/iqb-berlin/testcenter-iqb-ng/issues/52 is resolved,
        //  this must be removed, we would have a personToken here
    
    paflov's avatar
    paflov committed
        const params = new HttpParams().set('code', code);
    
        return this.http
    
          .get<BookletStatus>(this.serverUrl + `booklet/${bookletName}/state`, {params})
    
    paflov's avatar
    paflov committed
          .pipe(catchError(ErrorHandler.handle));
    
    paflov's avatar
    paflov committed
    
      startBooklet(code: string, bookletName: string, bookletLabel: string): Observable<PersonTokenAndTestId | ServerError> {
    
    
        return this.http
    
          .put<PersonTokenAndTestId>(this.serverUrl + `test`, {code, bookletName, bookletLabel})
    
    paflov's avatar
    paflov committed
          .pipe(catchError(ErrorHandler.handle));
    
    paflov's avatar
    paflov committed
    
      addBookletLogClose(testId: number): Observable<boolean | ServerError> {
    
    
        return this.http
    
          .put<boolean>(this.serverUrl + `test/${testId}/log`, {timestamp: Date.now(), entry: 'BOOKLETLOCKEDbyTESTEE'})
    
    paflov's avatar
    paflov committed
          .pipe(catchError(ErrorHandler.handle));
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
    
      lockBooklet(testId: number): Observable<boolean | ServerError> {
    
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .patch<boolean>(this.serverUrl + `test/${testId}/lock`, {})
    
          .pipe(catchError(ErrorHandler.handle));
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }