All files / projects/organization-management/src/app/store/cost-centers cost-centers.effects.ts

100% Statements 58/58
72.22% Branches 13/18
100% Functions 31/31
100% Lines 58/58

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2176x 6x 6x 6x 6x 6x   6x 6x 6x   6x   6x                                                       6x   27x 27x 27x 27x     27x 27x     3x 2x             27x 27x     1x       27x 27x       3x 2x             27x 27x       4x   3x 3x                             27x 27x       3x   2x 2x                             27x 27x       3x 2x                       27x 27x       4x   3x 3x                             27x 27x       3x 2x                         27x 27x       3x 2x                             8x 8x 6x   8x      
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { from } from 'rxjs';
import { concatMap, exhaustMap, map, mergeMap } from 'rxjs/operators';
 
import { displaySuccessMessage } from 'ish-core/store/core/messages/messages.actions';
import { selectRouteParam } from 'ish-core/store/core/router';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { CostCentersService } from '../../services/cost-centers/cost-centers.service';
 
import {
  addCostCenter,
  addCostCenterBuyers,
  addCostCenterBuyersFail,
  addCostCenterBuyersSuccess,
  addCostCenterFail,
  addCostCenterSuccess,
  deleteCostCenter,
  deleteCostCenterBuyer,
  deleteCostCenterBuyerFail,
  deleteCostCenterBuyerSuccess,
  deleteCostCenterFail,
  deleteCostCenterSuccess,
  loadCostCenter,
  loadCostCenterFail,
  loadCostCenterSuccess,
  loadCostCenters,
  loadCostCentersFail,
  loadCostCentersSuccess,
  updateCostCenter,
  updateCostCenterBuyer,
  updateCostCenterBuyerFail,
  updateCostCenterBuyerSuccess,
  updateCostCenterFail,
  updateCostCenterSuccess,
} from './cost-centers.actions';
 
@Injectable()
export class CostCentersEffects {
  constructor(
    private actions$: Actions,
    private costCentersService: CostCentersService,
    private router: Router,
    private store: Store
  ) {}
 
  loadCostCenters$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadCostCenters),
      exhaustMap(() =>
        this.costCentersService.getCostCenters().pipe(
          map(costCenters => loadCostCentersSuccess({ costCenters })),
          mapErrorToAction(loadCostCentersFail)
        )
      )
    )
  );
 
  triggerLoadCostCenter$ = createEffect(() =>
    this.store.pipe(
      select(selectRouteParam('CostCenterId')),
      whenTruthy(),
      map(costCenterId => loadCostCenter({ costCenterId }))
    )
  );
 
  loadCostCenter$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadCostCenter),
      mapToPayloadProperty('costCenterId'),
      mergeMap(costCenterId =>
        this.costCentersService.getCostCenter(costCenterId).pipe(
          map(costCenter => loadCostCenterSuccess({ costCenter })),
          mapErrorToAction(loadCostCenterFail)
        )
      )
    )
  );
 
  addCostCenter$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addCostCenter),
      mapToPayloadProperty('costCenter'),
      concatMap(cc =>
        this.costCentersService.addCostCenter(cc).pipe(
          concatMap(costCenter =>
            this.navigateTo(`../${costCenter.id}`).pipe(
              mergeMap(() => [
                addCostCenterSuccess({ costCenter }),
                displaySuccessMessage({
                  message: 'account.organization.cost_center_management.create.confirmation',
                  messageParams: { 0: `${costCenter.name}` },
                }),
              ])
            )
          ),
          mapErrorToAction(addCostCenterFail)
        )
      )
    )
  );
 
  updateCostCenter$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updateCostCenter),
      mapToPayloadProperty('costCenter'),
      concatMap(cc =>
        this.costCentersService.updateCostCenter(cc).pipe(
          concatMap(costCenter =>
            this.navigateTo('../').pipe(
              mergeMap(() => [
                updateCostCenterSuccess({ costCenter }),
                displaySuccessMessage({
                  message: 'account.organization.cost_center_management.update.confirmation',
                  messageParams: { 0: `${costCenter.name}` },
                }),
              ])
            )
          ),
          mapErrorToAction(updateCostCenterFail)
        )
      )
    )
  );
 
  deleteCostCenter$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deleteCostCenter),
      mapToPayloadProperty('id'),
      exhaustMap(id =>
        this.costCentersService.deleteCostCenter(id).pipe(
          mergeMap(() => [
            deleteCostCenterSuccess({ id }),
            displaySuccessMessage({
              message: 'account.organization.cost_center_management.delete.confirmation',
            }),
          ]),
          mapErrorToAction(deleteCostCenterFail)
        )
      )
    )
  );
 
  addCostCenterBuyers$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addCostCenterBuyers),
      mapToPayload(),
      mergeMap(payload =>
        this.costCentersService.addCostCenterBuyers(payload.costCenterId, payload.buyers).pipe(
          concatMap(costCenter =>
            this.navigateTo('../').pipe(
              mergeMap(() => [
                addCostCenterBuyersSuccess({ costCenter }),
                displaySuccessMessage({
                  message: 'account.organization.cost_center_management.buyer.add.confirmation',
                  messageParams: { 0: `${payload.buyers.length}` },
                }),
              ])
            )
          ),
          mapErrorToAction(addCostCenterBuyersFail)
        )
      )
    )
  );
 
  updateCostCenterBuyer$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updateCostCenterBuyer),
      mapToPayload(),
      mergeMap(payload =>
        this.costCentersService.updateCostCenterBuyer(payload.costCenterId, payload.buyer).pipe(
          mergeMap(costCenter => [
            updateCostCenterBuyerSuccess({ costCenter }),
            displaySuccessMessage({
              message: 'account.organization.cost_center_management.buyer.update.confirmation',
              messageParams: { 0: `${payload.buyer.firstName} ${payload.buyer.lastName}` || `${payload.buyer.login}` },
            }),
          ]),
          mapErrorToAction(updateCostCenterBuyerFail)
        )
      )
    )
  );
 
  deleteCostCenterBuyer$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deleteCostCenterBuyer),
      mapToPayload(),
      mergeMap(payload =>
        this.costCentersService.deleteCostCenterBuyer(payload.costCenterId, payload.login).pipe(
          mergeMap(costCenter => [
            deleteCostCenterBuyerSuccess({ costCenter }),
            displaySuccessMessage({
              message: 'account.organization.cost_center_management.buyer.remove.confirmation',
              messageParams: { 0: `${payload.login}` },
            }),
          ]),
          mapErrorToAction(deleteCostCenterBuyerFail)
        )
      )
    )
  );
 
  private navigateTo(path: string) {
    // find current ActivatedRoute by following first activated children
    let currentRoute = this.router.routerState.root;
    while (currentRoute.firstChild) {
      currentRoute = currentRoute.firstChild;
    }
    return from(this.router.navigate([path], { relativeTo: currentRoute }));
  }
}