All files / projects/requisition-management/src/app/store/requisitions requisitions.effects.ts

94.87% Statements 37/39
64.7% Branches 11/17
100% Functions 16/16
94.59% Lines 35/37

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1242x 2x 2x 2x 2x   2x 2x 2x 2x   2x   2x                         2x 9x   9x 9x       2x 2x             9x 9x       2x 2x                     9x 9x     1x   1x           9x 9x       3x       3x       3x               3x         3x                           9x   9x       1x            
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { from } from 'rxjs';
import { concatMap, map, switchMap } from 'rxjs/operators';
 
import { ProductCompletenessLevel } from 'ish-core/models/product/product.model';
import { displayInfoMessage, displaySuccessMessage } from 'ish-core/store/core/messages';
import { loadProductIfNotLoaded } from 'ish-core/store/shopping/products';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import { RequisitionsService } from '../../services/requisitions/requisitions.service';
 
import {
  loadRequisition,
  loadRequisitionFail,
  loadRequisitionSuccess,
  loadRequisitions,
  loadRequisitionsFail,
  loadRequisitionsSuccess,
  updateRequisitionStatus,
  updateRequisitionStatusFail,
  updateRequisitionStatusSuccess,
} from './requisitions.actions';
 
@Injectable()
export class RequisitionsEffects {
  constructor(private actions$: Actions, private requisitionsService: RequisitionsService, private router: Router) {}
 
  loadRequisitions$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadRequisitions),
      mapToPayload(),
      concatMap(({ view, status }) =>
        this.requisitionsService.getRequisitions(view, status).pipe(
          map(requisitions => loadRequisitionsSuccess({ requisitions, view, status })),
          mapErrorToAction(loadRequisitionsFail)
        )
      )
    )
  );
 
  loadRequisition$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadRequisition),
      mapToPayload(),
      switchMap(({ requisitionId }) =>
        this.requisitionsService.getRequisition(requisitionId).pipe(
          map(requisition => loadRequisitionSuccess({ requisition })),
          mapErrorToAction(loadRequisitionFail)
        )
      )
    )
  );
 
  /**
   * After selecting and successfully loading a requisition, triggers a LoadProduct action
   * for each product that is missing in the current product entities state.
   */
  loadProductsForSelectedRequisition$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadRequisitionSuccess),
      mapToPayloadProperty('requisition'),
      switchMap(requisition => [
        ...requisition.lineItems.map(({ productSKU }) =>
          loadProductIfNotLoaded({ sku: productSKU, level: ProductCompletenessLevel.List })
        ),
      ])
    )
  );
 
  updateRequisitionStatus$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updateRequisitionStatus),
      mapToPayload(),
      concatMap(payload =>
        this.requisitionsService
          .updateRequisitionStatus(payload.requisitionId, payload.status, payload.approvalComment)
          .pipe(
            concatMap(requisition =>
              from(this.router.navigate([`/account/requisitions/approver`])).pipe(
                concatMap(() => {
                  let messageAction;
                  // keep-localization-pattern: ^approval\.order_.*\.text$
                  switch (requisition.approval?.statusCode) {
                    case 'APPROVED':
                    case 'REJECTED':
                      messageAction = displaySuccessMessage({
                        message: `approval.order_${requisition.approval.statusCode.toLowerCase()}.text`,
                      });
                      break;
                    case 'PENDING':
                      messageAction = displayInfoMessage({
                        message: `approval.order_partially_approved.text`,
                      });
                  }
 
                  return [updateRequisitionStatusSuccess({ requisition }), messageAction];
                })
              )
            ),
            mapErrorToAction(updateRequisitionStatusFail)
          )
      )
    )
  );
 
  /**
   * In case the requisition (status) update failed because the requisition is invalid
   * and rejected by system the user is navigated to the requisition overview page
   */
  redirectAfterUpdateRequisitionStatusFail$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(updateRequisitionStatusFail),
        mapToPayloadProperty('error'),
        concatMap(error =>
          error.status === 422 ? this.router.navigate([`/account/requisitions/approver`]) : undefined
        )
      ),
    { dispatch: false }
  );
}