All files / projects/requisition-management/src/app/services/requisition-management-breadcrumb requisition-management-breadcrumb.service.ts

100% Statements 23/23
82.6% Branches 19/23
100% Functions 4/4
100% Lines 23/23

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 6925x 25x 25x 25x 25x   25x   25x 25x   25x     25x   9x 9x 9x 9x       8x       5x 1x   4x 1x   3x 2x             2x                                           1x          
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { EMPTY, Observable, of } from 'rxjs';
import { map, switchMap, withLatestFrom } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { BreadcrumbItem } from 'ish-core/models/breadcrumb-item/breadcrumb-item.interface';
import { selectRouteParam } from 'ish-core/store/core/router';
import { whenFalsy, whenTruthy } from 'ish-core/utils/operators';
 
import { RequisitionManagementFacade } from '../../facades/requisition-management.facade';
 
@Injectable({ providedIn: 'root' })
export class RequisitionManagementBreadcrumbService {
  constructor(
    private appFacade: AppFacade,
    private store: Store,
    private requisitionManagementFacade: RequisitionManagementFacade,
    private translateService: TranslateService
  ) {}
 
  breadcrumb$(prefix: string): Observable<BreadcrumbItem[]> {
    return this.appFacade.routingInProgress$.pipe(
      whenFalsy(),
      withLatestFrom(this.appFacade.path$.pipe(whenTruthy())),
      switchMap(([, path]) => {
        if (path.endsWith('/buyer')) {
          return of([{ key: 'account.requisitions.requisitions' }]);
        }
        if (path.endsWith('/approver')) {
          return of([{ key: 'account.requisitions.approvals' }]);
        }
        if (path.includes('/approver/') || path.includes('/buyer/')) {
          return this.requisitionManagementFacade.selectedRequisition$.pipe(
            whenTruthy(),
            withLatestFrom(
              this.translateService.get('approval.details.breadcrumb.order.label'),
              this.store.pipe(select(selectRouteParam('status')))
            ),
            map(([req, translation, status]) =>
              path.includes('/approver/')
                ? [
                    {
                      key: 'account.requisitions.approvals',
                      link: [`${prefix}/approver`, { status }],
                    },
                    {
                      text: `${translation} - ${req.requisitionNo}`,
                    },
                  ]
                : [
                    {
                      key: 'account.requisitions.requisitions',
                      link: [`${prefix}/buyer`, { status }],
                    },
                    {
                      text: `${translation} - ${req.requisitionNo}`,
                    },
                  ]
            )
          );
        }
        return EMPTY;
      })
    );
  }
}