Skip to content
Snippets Groups Projects
backend.service.ts 5.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* eslint-disable no-console */
    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';
    
    import {
      UnitData,
      TaggedString,
      TestData,
    
    } from './test-controller.interfaces';
    
    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 })
    
            catchError((err: ApiError) => {
              console.warn(`saveUnitReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      saveTestReview(testId: string, priority: number, categories: string, entry: string): Observable<boolean> {
    
        return this.http
    
          .put(`${this.serverUrl}test/${testId}/review`, { priority, categories, entry })
    
              console.warn(`saveTestReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      getTestData(testId: string): Observable<TestData | boolean> {
    
        return this.http
    
          .get<TestData>(`${this.serverUrl}test/${testId}`)
    
    mechtelm's avatar
    mechtelm committed
          .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, unitalias: string): Observable<UnitData | boolean> {
    
        return this.http
    
          .get<UnitData>(`${this.serverUrl}test/${testId}/unit/${unitid}/alias/${unitalias}`)
    
    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
          );
    
      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'
    
            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);
    
      updateTestState(testId: string, newState: StateReportEntry[]): Subscription {
    
        return this.http
    
          .patch(`${this.serverUrl}test/${testId}/state`, newState)
          .subscribe({ error: (err: ApiError) => console.error(`updateTestState Api-Error: ${err.code} ${err.info}`) });
    
      addTestLog(testId: string, logEntries: StateReportEntry[]): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(`${this.serverUrl}test/${testId}/log`, logEntries)
          .subscribe({ error: (err: ApiError) => console.error(`addTestLog Api-Error: ${err.code} ${err.info}`) });
    
      updateUnitState(testId: string, unitName: string, newState: StateReportEntry[]): Subscription {
    
          .patch(`${this.serverUrl}test/${testId}/unit/${unitName}/state`, newState)
          .subscribe({ error: (err: ApiError) => console.error(`setUnitState Api-Error: ${err.code} ${err.info}`) });
    
      addUnitLog(testId: string, unitName: string, logEntries: StateReportEntry[]): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(`${this.serverUrl}test/${testId}/unit/${unitName}/log`, logEntries)
          .subscribe({ error: (err: ApiError) => console.error(`addUnitLog Api-Error: ${err.code} ${err.info}`) });
    
    paflov's avatar
    paflov committed
      notifyDyingTest(testId: string): void {
    
    mechtelm's avatar
    mechtelm committed
        if (navigator.sendBeacon) {
    
          navigator.sendBeacon(`${this.serverUrl}test/${testId}/connection-lost`);
    
          fetch(`${this.serverUrl}test/${testId}/connection-lost`, {
    
    paflov's avatar
    paflov committed
      updateUnitStateData(testId: string, timeStamp: number, unitName: string,
                          dataPartsAllString: string, unitStateDataType: string) : 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 })
    
            switchMap(() => 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, timeStamp: number, content: string): Observable<boolean> {
    
        return this.http
    
          .patch<boolean>(`${this.serverUrl}test/${testId}/lock`, { timeStamp, content })
    
          .pipe(
            map(() => true),
            catchError((err: ApiError) => {
              console.warn(`lockBooklet Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);