All files / src/app/core/store/customer/basket basket-promotion-code.effects.ts

100% Statements 23/23
75% Branches 6/8
100% Functions 11/11
100% Lines 22/22

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 7424x 24x 24x   24x 24x   24x                     24x 11x         11x 11x       7x 4x                   11x 11x   3x             11x 11x       7x 4x                   11x 11x   3x        
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, map } from 'rxjs/operators';
 
import { BasketService } from 'ish-core/services/basket/basket.service';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import {
  addPromotionCodeToBasket,
  addPromotionCodeToBasketFail,
  addPromotionCodeToBasketSuccess,
  loadBasket,
  removePromotionCodeFromBasket,
  removePromotionCodeFromBasketFail,
  removePromotionCodeFromBasketSuccess,
} from './basket.actions';
 
@Injectable()
export class BasketPromotionCodeEffects {
  constructor(private actions$: Actions, private basketService: BasketService) {}
 
  /**
   * Add promotion code to the current basket.
   */
  addPromotionCodeToBasket$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addPromotionCodeToBasket),
      mapToPayloadProperty('code'),
      concatMap(code =>
        this.basketService.addPromotionCodeToBasket(code).pipe(
          map(() => addPromotionCodeToBasketSuccess()),
          mapErrorToAction(addPromotionCodeToBasketFail)
        )
      )
    )
  );
 
  /**
   * Reload basket after successfully adding a promo code
   */
  loadBasketAfterAddPromotionCodeToBasketChangeSuccess$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addPromotionCodeToBasketSuccess),
      map(() => loadBasket())
    )
  );
 
  /**
   * Remove promotion code from the current basket.
   */
  removePromotionCodeFromBasket$ = createEffect(() =>
    this.actions$.pipe(
      ofType(removePromotionCodeFromBasket),
      mapToPayloadProperty('code'),
      concatMap(code =>
        this.basketService.removePromotionCodeFromBasket(code).pipe(
          map(() => removePromotionCodeFromBasketSuccess()),
          mapErrorToAction(removePromotionCodeFromBasketFail)
        )
      )
    )
  );
 
  /**
   * Reload basket after successfully removing a promo code
   */
  loadBasketAfterRemovePromotionCodeFromBasketChangeSuccess$ = createEffect(() =>
    this.actions$.pipe(
      ofType(removePromotionCodeFromBasketSuccess),
      map(() => loadBasket())
    )
  );
}