All files / projects/requisition-management/src/app/facades requisition-management.facade.ts

64% Statements 16/25
42.85% Branches 6/14
12.5% Functions 1/8
62.5% Lines 15/24

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 7030x 30x 30x 30x 30x   30x 30x     30x                     30x 3x   3x 3x   3x         3x                               3x                                          
import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { combineLatest } from 'rxjs';
import { distinctUntilChanged, filter, map, sample, startWith, switchMap } from 'rxjs/operators';
 
import { selectRouteParam, selectUrl } from 'ish-core/store/core/router';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { RequisitionStatus, RequisitionViewer } from '../models/requisition/requisition.model';
import {
  getRequisition,
  getRequisitions,
  getRequisitionsError,
  getRequisitionsLoading,
  loadRequisition,
  loadRequisitions,
} from '../store/requisitions';
 
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class RequisitionManagementFacade {
  constructor(private store: Store, private router: Router) {}
 
  requisitionsError$ = this.store.pipe(select(getRequisitionsError));
  requisitionsLoading$ = this.store.pipe(select(getRequisitionsLoading));
 
  requisitionsStatus$ = this.store.pipe(
    select(selectRouteParam('status')),
    map(status => status || 'PENDING')
  );
 
  selectedRequisition$ = this.store.pipe(
    select(selectRouteParam('requisitionId')),
    whenTruthy(),
    switchMap(requisitionId => this.store.pipe(select(getRequisition(requisitionId))))
  );
 
  requisition$(requisitionId: string) {
    this.store.dispatch(loadRequisition({ requisitionId }));
    return this.store.pipe(select(getRequisition(requisitionId)));
  }
 
  requisitions$(view: RequisitionViewer, status: RequisitionStatus) {
    this.store.dispatch(loadRequisitions({ view, status }));
    return this.store.pipe(select(getRequisitions(view, status)));
  }
 
  requisitionsByRoute$ = combineLatest([
    this.store.pipe(
      select(selectUrl),
      map(url => (url.includes('/buyer') ? 'buyer' : 'approver')),
      distinctUntilChanged()
    ),
    this.store.pipe(select(selectRouteParam('status')), distinctUntilChanged()),
  ]).pipe(
    sample(
      this.router.events.pipe(
        // only when navigation is finished
        filter(e => e instanceof NavigationEnd),
        // fire on first subscription
        startWith({})
      )
    ),
    switchMap(([view, status]) =>
      this.requisitions$(view as RequisitionViewer, (status as RequisitionStatus) || 'PENDING')
    )
  );
}