All files / src/app/log log.service.ts

88.88% Statements 16/18
100% Branches 0/0
87.5% Functions 7/8
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                  1x     4x   4x 4x 4x 4x     4x     2x   14x   2x             2x       1x       1x             1x        
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { concatMap, tap, catchError } from 'rxjs/operators';
import { JsonSimpleValue, LogMessageDetail } from '@reflex/shared-types';
 
@Injectable({
  providedIn: 'root'
})
export class LogService {
 
  // eslint-disable-next-line @typescript-eslint/naming-convention
  protected readonly headers = new HttpHeaders({ 'Content-Type': 'application/json' });
 
  private readonly baseRoute = 'api/Log';
  private readonly route = `${this.baseUrl}${this.baseRoute}/Messages/`;
  private readonly routeAdd = `${this.baseUrl}${this.baseRoute}/Add/`;
  private index = 0;
 
  // eslint-disable-next-line new-cap
  public constructor(private readonly http: HttpClient, @Inject('BASE_URL') private readonly baseUrl: string) { }
 
  public getLogs(): Observable<LogMessageDetail> {
    const list = this.http.get<Array<LogMessageDetail>>(`${this.route}${this.index}`).pipe(
      tap((completeMessages) => {
        this.index = Math.max(...completeMessages.map((elem) => elem.id));
      }),
      concatMap((result) => [...result]),
      catchError((error) => {
        console.error(error);
        throw error;
      })
    );
 
    return list;
  }
 
  public sendErrorLog(msg: string): void {
    const value: JsonSimpleValue = {
      name: 'message',
      value: msg
    };
    this.http.post<JsonSimpleValue>(this.routeAdd, value, { headers: this.headers }).subscribe(
      console.log,
      console.error
    );
  }
 
  public reset(): void {
    this.index = 0;
  }
}