All files / app/service/api user.service.ts

48.38% Statements 15/31
100% Branches 5/5
20% Functions 4/20
40.74% Lines 11/27

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97  118x   118x   118x 118x               168x     168x 168x 168x       1x                                     1x       1x                                                                                                    
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, map, Observable } from 'rxjs';
import { mapPage, Page } from '../../model/page';
import { TagPageArgs } from '../../model/tag';
import { mapUser, Roles, User, writeUser } from '../../model/user';
import { params } from '../../util/http';
import { OpPatch } from '../../util/json-patch';
import { ConfigService } from '../config.service';
import { LoginService } from '../login.service';
 
@Injectable({
  providedIn: 'root',
})
export class UserService {
 
  constructor(
    private http: HttpClient,
    private config: ConfigService,
    private login: LoginService,
  ) { }
 
  private get base() {
    return this.config.api + '/api/v1/user';
  }
 
  create(user: User): Observable<string> {
    return this.http.post<string>(this.base, writeUser(user)).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  get(tag: string): Observable<User> {
    return this.http.get(this.base, {
      params: params({ tag }),
    }).pipe(
      map(mapUser),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  page(args: TagPageArgs): Observable<Page<User>> {
    return this.http.get(`${this.base}/page`, {
      params: params(args),
    }).pipe(
      map(mapPage(mapUser)),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  update(user: User): Observable<string> {
    return this.http.put<string>(this.base, writeUser(user)).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  keygen(tag: string): Observable<void> {
    return this.http.post<void>(`${this.base}/keygen`, null, {
      params: params({ tag }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  patch(tag: string, cursor: string, patch: OpPatch[]): Observable<string> {
    return this.http.patch<string>(this.base, patch, {
      headers: { 'Content-Type': 'application/json-patch+json' },
      params: params({ tag, cursor }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  merge(tag: string, cursor: string, patch: Partial<User>): Observable<string> {
    return this.http.patch<string>(this.base, patch, {
      headers: { 'Content-Type': 'application/merge-patch+json' },
      params: params({ tag, cursor }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  delete(tag: string): Observable<void> {
    return this.http.delete<void>(this.base, {
      params: params({ tag }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  whoAmI(): Observable<Roles> {
    return this.http.get<Roles>(`${this.base}/whoami`).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
}