Skip to content
Snippets Groups Projects
backend.service.ts 5.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Injectable, Inject } from '@angular/core';
    
    mechtelm's avatar
    mechtelm committed
    import { HttpClient, HttpParams } from '@angular/common/http';
    
    import {Observable, of, Subscription} from 'rxjs';
    
    import {catchError, map, switchMap} from 'rxjs/operators';
    
    mechtelm's avatar
    mechtelm committed
    import {UnitData, TaggedString, TestData, UnitStatus, LastStateKey} from './test-controller.interfaces';
    
    paf's avatar
    paf committed
    import {ApiError} from '../app.interfaces';
    
    
    
    @Injectable({
      providedIn: 'root'
    })
    export class BackendService {
    
      constructor(
        @Inject('SERVER_URL') private serverUrl: string,
    
    mechtelm's avatar
    mechtelm committed
      saveUnitReview(testId: string, unitName: string, priority: number, categories: string, entry: string)
        : Observable<boolean> {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/review`, {priority, categories, entry})
          .pipe(
            map(() => true),
    
            catchError((err: ApiError) => {
              console.warn(`saveUnitReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    mechtelm's avatar
    mechtelm committed
      saveBookletReview(testId: string, priority: number, categories: string, entry: string): Observable<boolean> {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/review`, {priority, categories, entry})
          .pipe(
            map(() => true),
    
            catchError((err: ApiError) => {
              console.warn(`saveBookletReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      getTestData(testId: string): Observable<TestData | boolean> {
    
        return this.http
    
    mechtelm's avatar
    mechtelm committed
          .get<TestData>(this.serverUrl + 'test/' + testId)
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`getTestData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    mechtelm's avatar
    mechtelm committed
          );
    
      getUnitData(testId: string, unitid: string): Observable<UnitData | boolean> {
    
        return this.http
    
          .get<UnitData>(this.serverUrl + 'test/' + testId + '/unit/' + unitid)
    
    mechtelm's avatar
    mechtelm committed
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`getUnitData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    mechtelm's avatar
    mechtelm committed
          );
    
    mechtelm's avatar
    mechtelm committed
      getResource(testId: string, internalKey: string, resId: string, versionning = false): Observable<TaggedString | number> {
    
        return this.http
          .get(
    
            this.serverUrl + `test/${testId}/resource/${resId}`,
    
            {
              params: new HttpParams().set('v', versionning ? '1' : 'f'),
              responseType: 'text'
            })
    
    mechtelm's avatar
    mechtelm committed
            map(def => <TaggedString>{tag: internalKey, value: def}),
    
            catchError((err: ApiError) => {
              console.warn(`getResource Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(err.code);
    
      addUnitLog(testId: string, timestamp: number, unitName: string, entry: string): Subscription {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/log`, {timestamp, entry})
    
          .subscribe({error: (err: ApiError) => console.error(`addUnitLog Api-Error: ${err.code} ${err.info}`)});
    
      addTestLog(testId: string, timestamp: number, entry: string): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(this.serverUrl + `test/${testId}/log`, {timestamp, entry})
    
          .subscribe({error: (err: ApiError) => console.error(`addTestLog Api-Error: ${err.code} ${err.info}`)});
    
    mechtelm's avatar
    mechtelm committed
      updateUnitState(testId: string, unitName: string, newUnitState: UnitStatus): Subscription {
    
    mechtelm's avatar
    mechtelm committed
          .patch(this.serverUrl + `test/${testId}/unit/${unitName}/state`, newUnitState)
    
          .subscribe({error: (err: ApiError) => console.error(`setUnitState Api-Error: ${err.code} ${err.info}`)});
    
    mechtelm's avatar
    mechtelm committed
      updateTestState(testId: string, stateKey: string, state: string): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .patch(this.serverUrl + `test/${testId}/state`, {key: stateKey, value: state})
    
    mechtelm's avatar
    mechtelm committed
          .subscribe({error: (err: ApiError) => console.error(`updateTestState Api-Error: ${err.code} ${err.info}`)});
    
    mechtelm's avatar
    mechtelm committed
      notifyDyingTest(testId: string) {
        // TODO add auth header (?)
        if (navigator.sendBeacon) {
          navigator.sendBeacon(this.serverUrl + `test/${testId}/state`, JSON.stringify({key: LastStateKey.DEAD, value: Date.now().toString()}));
          navigator.sendBeacon(this.serverUrl + `test/${testId}/log`, JSON.stringify({timestamp: Date.now(), entry: LastStateKey.DEAD}));
        }
      }
    
      updateUnitStateData(testId: string, timestamp: number, unitName: string, dataPartsAllString: string, unitStateDataType: string)
    
    mechtelm's avatar
    mechtelm committed
        : Observable<boolean> {
    
        // TODO remove after api changed
        const response = dataPartsAllString;
        const restorePoint = dataPartsAllString;
        const responseType = unitStateDataType;
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/response`, {timestamp, response, responseType})
          .pipe(
    
            switchMap(() => {
              return this.http
                .patch(this.serverUrl + `test/${testId}/unit/${unitName}/restorepoint`, {timestamp, restorePoint})
                .pipe(
                  map(() => true),
                  catchError((err: ApiError) => {
                    console.warn(`newUnitStateData/restorepoint Api-Error: ${err.code} ${err.info} `);
                    return of(false);
                  })
                );
            }),
    
              console.warn(`newUnitStateData/response Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      lockTest(testId: string): Observable<boolean> {
    
        return this.http
          .patch<boolean>(this.serverUrl + `test/${testId}/lock`, {})
          .pipe(
            map(() => true),
            catchError((err: ApiError) => {
              console.warn(`lockBooklet Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);