All files / src/app/core/facades withdrawal.facade.ts

100% Statements 39/39
80% Branches 8/10
100% Functions 8/8
100% Lines 38/38

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 952x 2x 2x       2x     2x   19x 19x   19x 19x 19x 1x   19x         19x 19x 19x 19x     19x 19x 19x   19x             7x 7x   7x         4x 4x 4x     3x     3x                   4x 4x   4x         2x 2x 2x     2x     2x           18x 18x        
import { Injectable, OnDestroy, signal } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { take } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { Withdrawal } from 'ish-core/models/withdrawal/withdrawal.model';
import { WithdrawalService } from 'ish-core/services/withdrawal/withdrawal.service';
 
@Injectable()
export class WithdrawalFacade implements OnDestroy {
  constructor(
    private withdrawalService: WithdrawalService,
    private toastrService: ToastrService
  ) {
    Eif (!SSR) {
      const storedWithdrawal = sessionStorage.getItem('withdrawal');
      if (storedWithdrawal) {
        this.withdrawalSignal.set(JSON.parse(storedWithdrawal));
      }
      this.initializedSignal.set(true);
    }
  }
 
  // Private writable signals for internal state management
  private withdrawalSignal = signal<Withdrawal | undefined>(undefined);
  private loadingSignal = signal<boolean>(false);
  private errorSignal = signal<HttpError | undefined>(undefined);
  private initializedSignal = signal<boolean>(false);
 
  // Public readonly signals - exposed to components
  withdrawal = this.withdrawalSignal.asReadonly();
  loading = this.loadingSignal.asReadonly();
  error = this.errorSignal.asReadonly();
  /** Indicates whether the client has finished initializing (sessionStorage loaded). */
  initialized = this.initializedSignal.asReadonly();
 
  /**
   * Creates a new withdrawal entry.
   * Updates loading, withdrawal, and error signals based on the result.
   */
  createWithdrawal(withdrawal: Withdrawal): void {
    this.loadingSignal.set(true);
    this.errorSignal.set(undefined);
 
    this.withdrawalService
      .createWithdrawal(withdrawal)
      .pipe(take(1))
      .subscribe({
        next: data => {
          this.toastrService.clear();
          this.withdrawalSignal.set(data);
          this.loadingSignal.set(false);
        },
        error: (err: HttpError) => {
          this.errorSignal.set(
            err.errors ? err : { name: 'HttpErrorResponse', message: 'common.request.error.invalid_data' }
          );
          this.loadingSignal.set(false);
        },
      });
  }
 
  /**
   * Submits a withdrawal request.
   * Updates loading, withdrawal, and error signals based on the result.
   */
  sendWithdrawal(withdraw: Withdrawal): void {
    this.loadingSignal.set(true);
    this.errorSignal.set(undefined);
 
    this.withdrawalService
      .sendWithdrawalRequest(withdraw)
      .pipe(take(1))
      .subscribe({
        next: data => {
          this.toastrService.clear();
          this.withdrawalSignal.set(data);
          this.loadingSignal.set(false);
        },
        error: (err: HttpError) => {
          this.errorSignal.set(
            err.errors ? err : { name: 'HttpErrorResponse', message: 'common.request.error.invalid_data' }
          );
          this.loadingSignal.set(false);
        },
      });
  }
 
  ngOnDestroy(): void {
    Eif (!SSR) {
      sessionStorage.removeItem('withdrawal');
    }
  }
}