All files / src/app/core/services user.service.ts

60.71% Statements 34/56
60% Branches 3/5
58.82% Functions 20/34
93.54% Lines 29/31

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 98 99 100 101 102 103 104 105 106 107 10822x 22x 22x 22x                         22x   18x   18x   18x   18x     1x   1x           3x   3x           1x       1x           1x               1x                 1x       4x 4x     1x 1x       3x         5x   3x   5x       1x       1x       1x      
import { Injectable, signal, Signal, WritableSignal } from '@angular/core';
import { catchError, Observable, switchMap, throwError } from 'rxjs';
import { DataService } from './data.service';
import { CaptchaService } from './captcha.service';
import { ResetPasswordRequest } from '@core/model/reset-password-request.interface';
import { NewPasswordRequest } from '@core/model/new-password-request.interface';
import { ValidateEmailRequest } from '@core/model/validate-email-request.interface';
import { User } from '@core/model/user.interface';
import { ChangePasswordRequest } from '@core/model/change-password-request.interface';
import { EditUserRequest } from '@core/model/edit-user-request.interface';
import { EditUserLangRequest } from '@core/model/edit-user-lang-request.interface';
import { UserSummary } from '../model/user-summary.interface';
 
@Injectable({
  providedIn: 'root'
})
export class UserService {
 
  private readonly apiPath = 'user';
 
  private readonly userSummary: WritableSignal<UserSummary | null | false> = signal(null); 
 
  private userSummaryLoaded: boolean = false;
 
  constructor(private dataService: DataService, private captchaService: CaptchaService) { }
 
  public resetPassword(resetPasswordRequest: ResetPasswordRequest): Observable<void> {
    return this.captchaService.getCaptchaToken().pipe(
      switchMap(() => {
        return this.dataService.post<void>(`${this.apiPath}/password/reset`, resetPasswordRequest);
      })
    );
  }
 
  public newPassword(newPasswordRequest: NewPasswordRequest): Observable<void> {
    return this.captchaService.getCaptchaToken().pipe(
      switchMap(() => {
        return this.dataService.post<void>(`${this.apiPath}/password`, newPasswordRequest);
      })
    );
  }
 
  public changePassword(changePasswordRequest: ChangePasswordRequest): Observable<void> {
    return this.dataService.put<void>(`${this.apiPath}/me/password`, changePasswordRequest);
  }
 
  public sendVerificationMail(): Observable<void> {
    return this.dataService.post<void>(`${this.apiPath}/me/email/verification`, null);
  }
 
  public validateEmail(validateEmailRequest: ValidateEmailRequest): Observable<void> {
    /*return this.captchaService.getCaptchaToken().pipe(
      switchMap(() => {*/
        return this.dataService.post<void>(`${this.apiPath}/me/email/validation`, validateEmailRequest);
      /*})
    );*/
  }
 
  public getUser(): Observable<User> {
    /*return this.captchaService.getCaptchaToken().pipe(
      switchMap(() => {*/
        return this.dataService.get<User>(`${this.apiPath}/me`);
        // return this.dataService.get<User>(`${this.apiPath}`).pipe(map((user: User) => {user.emailStatus = user.emailStatus as EmailStatus; return user;}));
      /*})
    );*/
  }
 
 
  /** Manually reload summary */
  public async reloadUserSummary(): Promise<void> {
    await this.loadUserSummary();
  }
 
  private async loadUserSummary(): Promise<void> {
    this.userSummaryLoaded = true;
    this.dataService.get<UserSummary>(`${this.apiPath}/me/summary`)
    .pipe(
      catchError((error) => {
        this.userSummary.set(false);
        return throwError(() => error);
      })
    )
    .subscribe(summary => {
      this.userSummary.set(summary);
    });
  }
 
  public getUserSummary(): Signal<UserSummary | null | false> {
    if(!this.userSummaryLoaded) {
      // force first load if necessary
      this.loadUserSummary();
    }
    return this.userSummary.asReadonly();
  }
 
  public deleteUser() :Observable<void> {
    return this.dataService.delete<void>(`${this.apiPath}/me`);
  }
 
  public editUser(editUserRequest: EditUserRequest) :Observable<User> {
    return this.dataService.patch<User>(`${this.apiPath}/me`, editUserRequest);
  }
 
  public saveUserLang(newLang: string) :Observable<void> {
    return this.dataService.put<void>(`${this.apiPath}/me/lang`, {lang: newLang.toUpperCase()} as EditUserLangRequest);
  }
}