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

100% Statements 52/52
82.6% Branches 19/23
100% Functions 21/21
100% Lines 52/52

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 17523x 23x 23x 23x   23x 23x 23x           23x 23x   23x                               23x   25x 25x 25x 25x           25x 25x     7x 4x                     25x 25x   8x       8x 4x 4x         4x 4x                     25x 25x     9x                       25x 25x         9x   3x 3x     3x 3x     3x 3x       9x               25x 25x     10x     10x 5x 2x                   5x 2x                     25x 25x       5x 2x              
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { map, mergeMap } from 'rxjs/operators';
 
import { AddressService } from 'ish-core/services/address/address.service';
import { BasketService, BasketUpdateType } from 'ish-core/services/basket/basket.service';
import {
  deleteCustomerAddressFail,
  deleteCustomerAddressSuccess,
  updateCustomerAddressFail,
  updateCustomerAddressSuccess,
} from 'ish-core/store/customer/addresses';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import {
  assignBasketAddress,
  createBasketAddress,
  createBasketAddressFail,
  createBasketAddressSuccess,
  deleteBasketShippingAddress,
  loadBasket,
  loadBasketEligibleAddresses,
  loadBasketEligibleAddressesFail,
  loadBasketEligibleAddressesSuccess,
  resetBasketErrors,
  updateBasket,
  updateBasketAddress,
} from './basket.actions';
 
@Injectable()
export class BasketAddressesEffects {
  constructor(
    private actions$: Actions,
    private store: Store,
    private basketService: BasketService,
    private addressService: AddressService
  ) {}
 
  /**
   * The load basket eligible addresses effect.
   */
  loadBasketEligibleAddresses$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadBasketEligibleAddresses),
      mergeMap(() =>
        this.basketService.getBasketEligibleAddresses().pipe(
          map(result => loadBasketEligibleAddressesSuccess({ addresses: result })),
          mapErrorToAction(loadBasketEligibleAddressesFail)
        )
      )
    )
  );
 
  /**
   * Creates a new invoice/shipping address which is assigned to the basket later on
   * if the user is logged in a customer address will be created, otherwise a new basket address will be created
   */
  createAddressForBasket$ = createEffect(() =>
    this.actions$.pipe(
      ofType(createBasketAddress),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInCustomer))),
 
      mergeMap(([action, customer]) => {
        // create address at customer for logged in user
        if (customer) {
          return this.addressService.createCustomerAddress('-', action.payload.address).pipe(
            map(newAddress => createBasketAddressSuccess({ address: newAddress, scope: action.payload.scope })),
            mapErrorToAction(createBasketAddressFail)
          );
          // create address at basket for anonymous user
        } else {
          return this.basketService.createBasketAddress(action.payload.address).pipe(
            map(newAddress => createBasketAddressSuccess({ address: newAddress, scope: action.payload.scope })),
            mapErrorToAction(createBasketAddressFail)
          );
        }
      })
    )
  );
 
  /**
   * Assigns an address that has just created as basket invoice/shipping address
   */
  assignNewAddressToBasket$ = createEffect(() =>
    this.actions$.pipe(
      ofType(createBasketAddressSuccess),
      map(action =>
        assignBasketAddress({
          addressId: action.payload.address.id,
          scope: action.payload.scope,
        })
      )
    )
  );
 
  /**
   * Assigns the address to the basket according to the scope of the payload.
   * Triggers the internal UpdateBasket action that handles the actual updating operation.
   */
  assignBasketAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(assignBasketAddress),
      mapToPayload(),
      map(payload => {
        let body: BasketUpdateType;
        switch (payload.scope) {
          case 'invoice': {
            body = { invoiceToAddress: payload.addressId };
            break;
          }
          case 'shipping': {
            body = { commonShipToAddress: payload.addressId };
            break;
          }
          case 'any': {
            body = { invoiceToAddress: payload.addressId, commonShipToAddress: payload.addressId };
            break;
          }
        }
 
        return updateBasket({ update: body });
      })
    )
  );
 
  /**
   * Updates an address (basket or customer) and reloads the basket in case of success.
   */
  updateBasketAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updateBasketAddress),
      mapToPayloadProperty('address'),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInCustomer))),
      mergeMap(([address, customer]) => {
        // create address at customer for logged in user
        if (customer) {
          return this.addressService.updateCustomerAddress('-', address).pipe(
            mergeMap(() => [
              updateCustomerAddressSuccess({ address }),
              loadBasket(),
              loadBasketEligibleAddresses(),
              resetBasketErrors(),
            ]),
            mapErrorToAction(updateCustomerAddressFail)
          );
          // create address at basket for anonymous user
        } else {
          return this.basketService.updateBasketAddress(address).pipe(
            mergeMap(() => [updateCustomerAddressSuccess({ address }), loadBasket(), resetBasketErrors()]),
            mapErrorToAction(updateCustomerAddressFail)
          );
        }
      })
    )
  );
 
  /**
   * Deletes a basket shipping address and reloads the basket in case of success.
   */
  deleteBasketShippingAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deleteBasketShippingAddress),
      mapToPayloadProperty('addressId'),
      mergeMap(addressId =>
        this.addressService.deleteCustomerAddress('-', addressId).pipe(
          mergeMap(() => [deleteCustomerAddressSuccess({ addressId }), loadBasket(), loadBasketEligibleAddresses()]),
          mapErrorToAction(deleteCustomerAddressFail)
        )
      )
    )
  );
}