All files / app/core/users user.service.ts

100% Statements 24/24
100% Branches 9/9
100% Functions 12/12
100% Lines 18/18

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              12x   12x   12x     2x           2x             3x     2x             2x               5x 3x         2x 5x 4x                           4x 4x   1x     3x   4x      
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { catchError, map, Observable, throwError } from 'rxjs';
import { environment } from '../../../environments/environment';
import { UserRegisterData, UserRegisterFormData, UserRole, UserRoleRequest, UserSearchEmailResult } from './user.interfaces';
 
@Injectable({ providedIn: 'root' })
export class UserService {
 
  private readonly apiBase = environment.authApiBaseUrl;
 
  constructor(private http: HttpClient) {}
 
  sendUserRegister(userData: UserRegisterFormData): Observable<void> {
    const userRegisterData: UserRegisterData = {
      name: userData.name,
      lastName: userData.lastname,
      email: userData.email,
      password: userData.password
    }
    return this.http.post<void>(this.apiBase + '/api/v1/user/register',userRegisterData)
      .pipe (
        catchError(this.handleError)
      )
  }
 
  emailExist(userName: string): Observable<boolean> {
    return this.http.post<boolean>(this.apiBase+'/api/v1/user/exist', userName)
      .pipe(
        map((res) => {
          return res;
        }),
        catchError(this.handleError)
      );
  }
 
  searchUsersByEmail(email: string): Observable<UserSearchEmailResult[]> {
    return this.http.get<UserSearchEmailResult[]>(
      this.apiBase + '/api/v1/user/search/email/' + email
    ).pipe(
      catchError(this.handleError)
    );
  }
  
  searchTeamById(team: UserRoleRequest[]): Observable<UserRole[]> {
    const arrayIds:string[] = team.map(member => member.userId);
    return this.http.post<UserSearchEmailResult[]>(
      this.apiBase + '/api/v1/user/search/team',
      arrayIds
    ).pipe(
      map((results) => {
        return results.map(result => {
          const role = team.find(member => member.userId === result.id)?.role || '';
          return {
            id: result.id,
            name: result.name,
            email: result.email,
            avatar: result.avatar,
            role
          } as UserRole;
        });
      }),
      catchError(this.handleError)
    );
  }
 
  private handleError(error: HttpErrorResponse) {
    let errorMessage = 'Error desconocido';
    if (error.error instanceof ErrorEvent) {
      // Error del lado del cliente
      errorMessage = `Error: ${error.error.message}`;
    } else {
      // Error del lado del servidor
      errorMessage = `Código: ${error.status} - Mensaje: ${error.message}`;
    }
    return throwError(() => new Error(errorMessage));
  }
}