File

feature-libs/organization/order-approval/core/services/order-approval.service.ts

Index

Methods

Constructor

constructor(store: Store, userIdService: UserIdService)
Parameters :
Name Type Optional
store Store<OrderApprovalState | StateWithProcess> No
userIdService UserIdService No

Methods

get
get(orderApprovalCode: string)
Parameters :
Name Type Optional
orderApprovalCode string No
Returns : Observable<OrderApproval>
getList
getList(params: SearchConfig)
Parameters :
Name Type Optional
params SearchConfig No
Returns : Observable<EntitiesModel<OrderApproval>>
getMakeDecisionResultError
getMakeDecisionResultError()

Returns the makeDecision failure outcome. Returns true when the outcome of makeDecision() was an error.

Returns : Observable<boolean>
getMakeDecisionResultLoading
getMakeDecisionResultLoading()

Returns the makeDecision loading flag. Returns true when the process triggered by makeDecision() is currently running.

Returns : Observable<boolean>
getMakeDecisionResultSuccess
getMakeDecisionResultSuccess()

Returns the makeDecision process success outcome. Returns true when the outcome of makeDecision() was a success.

Returns : Observable<boolean>
Private getOrderApproval
getOrderApproval(orderApprovalCode: string)
Parameters :
Name Type Optional
orderApprovalCode string No
Returns : Observable<StateUtils.LoaderState<OrderApproval>>
Private getOrderApprovalList
getOrderApprovalList(params)
Parameters :
Name Optional
params No
Returns : Observable<StateUtils.LoaderState<EntitiesModel<OrderApproval>>>
getOrderApprovalLoading
getOrderApprovalLoading(orderApprovalCode: string)

Emits true if a request is currently fetching order approval data from the server.

Parameters :
Name Type Optional Description
orderApprovalCode string No

The approval code for which we want the loading status.

Returns : Observable<boolean>
loadOrderApproval
loadOrderApproval(orderApprovalCode: string)
Parameters :
Name Type Optional
orderApprovalCode string No
Returns : void
loadOrderApprovals
loadOrderApprovals(params?: SearchConfig)
Parameters :
Name Type Optional
params SearchConfig Yes
Returns : void
makeDecision
makeDecision(orderApprovalCode: string, orderApprovalDecision: OrderApprovalDecision)
Parameters :
Name Type Optional
orderApprovalCode string No
orderApprovalDecision OrderApprovalDecision No
Returns : void
resetMakeDecisionProcessState
resetMakeDecisionProcessState()

Resets the makeDecision process state. It is usually preferable to reset the process state before making a call to makeDecision() for which we then want to monitor the loading state or the outcome.

Returns : void
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import {
  EntitiesModel,
  ProcessSelectors,
  SearchConfig,
  StateUtils,
  StateWithProcess,
  UserIdService,
} from '@spartacus/core';
import { Observable, queueScheduler } from 'rxjs';
import { filter, map, observeOn, pluck, tap } from 'rxjs/operators';
import {
  OrderApproval,
  OrderApprovalDecision,
} from '../model/order-approval.model';
import { OrderApprovalActions } from '../store/actions/index';
import {
  OrderApprovalState,
  ORDER_APPROVAL_MAKE_DECISION_PROCESS_ID,
} from '../store/order-approval-state';
import { OrderApprovalSelectors } from '../store/selectors';

@Injectable({ providedIn: 'root' })
export class OrderApprovalService {
  constructor(
    protected store: Store<OrderApprovalState | StateWithProcess<void>>,
    protected userIdService: UserIdService
  ) {}

  loadOrderApproval(orderApprovalCode: string): void {
    this.userIdService.takeUserId().subscribe((userId) =>
      this.store.dispatch(
        new OrderApprovalActions.LoadOrderApproval({
          userId,
          orderApprovalCode,
        })
      )
    );
  }

  loadOrderApprovals(params?: SearchConfig): void {
    this.userIdService
      .takeUserId()
      .subscribe((userId) =>
        this.store.dispatch(
          new OrderApprovalActions.LoadOrderApprovals({ userId, params })
        )
      );
  }

  private getOrderApproval(
    orderApprovalCode: string
  ): Observable<StateUtils.LoaderState<OrderApproval>> {
    return this.store.select(
      OrderApprovalSelectors.getOrderApproval(orderApprovalCode)
    );
  }

  private getOrderApprovalList(
    params
  ): Observable<StateUtils.LoaderState<EntitiesModel<OrderApproval>>> {
    return this.store.select(
      OrderApprovalSelectors.getOrderApprovalList(params)
    );
  }

  get(orderApprovalCode: string): Observable<OrderApproval> {
    return this.getOrderApproval(orderApprovalCode).pipe(
      observeOn(queueScheduler),
      tap((state) => {
        if (!(state.loading || state.success || state.error)) {
          this.loadOrderApproval(orderApprovalCode);
        }
      }),
      filter((state) => state.success || state.error),
      map((state) => state.value)
    );
  }

  /**
   * Emits true if a request is currently fetching order approval data from
   * the server.
   *
   * @param orderApprovalCode The approval code for which we want the loading status.
   */
  getOrderApprovalLoading(orderApprovalCode: string): Observable<boolean> {
    return this.getOrderApproval(orderApprovalCode).pipe(pluck('loading'));
  }

  getList(params: SearchConfig): Observable<EntitiesModel<OrderApproval>> {
    return this.getOrderApprovalList(params).pipe(
      observeOn(queueScheduler),
      tap((process: StateUtils.LoaderState<EntitiesModel<OrderApproval>>) => {
        if (!(process.loading || process.success || process.error)) {
          this.loadOrderApprovals(params);
        }
      }),
      filter(
        (process: StateUtils.LoaderState<EntitiesModel<OrderApproval>>) =>
          process.success || process.error
      ),
      map((result) => result.value)
    );
  }

  makeDecision(
    orderApprovalCode: string,
    orderApprovalDecision: OrderApprovalDecision
  ): void {
    this.userIdService.takeUserId().subscribe((userId) =>
      this.store.dispatch(
        new OrderApprovalActions.MakeDecision({
          userId,
          orderApprovalCode,
          orderApprovalDecision,
        })
      )
    );
  }

  /**
   * Returns the makeDecision loading flag.  Returns true when the process triggered
   * by makeDecision() is currently running.
   */
  getMakeDecisionResultLoading(): Observable<boolean> {
    return this.store.pipe(
      select(
        ProcessSelectors.getProcessLoadingFactory(
          ORDER_APPROVAL_MAKE_DECISION_PROCESS_ID
        )
      )
    );
  }

  /**
   * Returns the makeDecision failure outcome.  Returns true when the outcome
   * of makeDecision() was an error.
   */
  getMakeDecisionResultError(): Observable<boolean> {
    return this.store.pipe(
      select(
        ProcessSelectors.getProcessErrorFactory(
          ORDER_APPROVAL_MAKE_DECISION_PROCESS_ID
        )
      )
    );
  }

  /**
   * Returns the makeDecision process success outcome.  Returns true when the outcome
   * of makeDecision() was a success.
   */
  getMakeDecisionResultSuccess(): Observable<boolean> {
    return this.store.pipe(
      select(
        ProcessSelectors.getProcessSuccessFactory(
          ORDER_APPROVAL_MAKE_DECISION_PROCESS_ID
        )
      )
    );
  }

  /**
   * Resets the makeDecision process state. It is usually preferable to reset the
   * process state before making a call to makeDecision() for which we then want
   * to monitor the loading state or the outcome.
   */
  resetMakeDecisionProcessState(): void {
    this.store.dispatch(new OrderApprovalActions.MakeDecisionReset());
  }
}

result-matching ""

    No results matching ""