All files / src/app/core/store/customer/basket basket.selectors.ts

87.03% Statements 47/54
58.82% Branches 10/17
76.92% Functions 20/26
84.44% Lines 38/45

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 105354x 354x   354x     354x 354x 354x       354x   354x   354x 354x   59x       59x 59x       2x     2x             354x   354x       52x     354x         3x     354x 15x     354x       354x   354x   354x   354x   354x   354x   3x     354x 4x     354x                 354x 354x     354x 354x     354x          
import { createSelector, createSelectorFactory, resultMemoize } from '@ngrx/store';
import { isEqual } from 'lodash-es';
 
import { AddressHelper } from 'ish-core/models/address/address.helper';
import { Address } from 'ish-core/models/address/address.model';
import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model';
import { BasketView, createBasketView } from 'ish-core/models/basket/basket.model';
import { getCustomerState } from 'ish-core/store/customer/customer-store';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
 
import { BasketState } from './basket.reducer';
 
const getBasketState = createSelector(getCustomerState, state => state?.basket);
 
const getInternalBasket = createSelector(getBasketState, basket => basket.basket);
 
export const getBasketValidationResults = createSelectorFactory<object, BasketValidationResultType>(projector =>
  resultMemoize(projector, isEqual)
)(getBasketState, (basket: BasketState): BasketValidationResultType => {
  Iif (!basket?.validationResults) {
    return;
  }
 
  const basketResults = basket.validationResults;
  return {
    ...basketResults,
    infos: basketResults.infos || [],
    errors: basketResults.errors
      ? basketResults.errors.map(error => ({
          ...error,
          lineItem: error.parameters?.lineItemId && {
            ...basket.basket.lineItems.find(item => item.id === error.parameters.lineItemId),
          },
        }))
      : [],
  };
});
 
export const getBasketInfo = createSelector(getBasketState, basket => basket.info);
 
export const getCurrentBasket = createSelector(
  getInternalBasket,
  getBasketValidationResults,
  getBasketInfo,
  (basket, validationResults, basketInfo): BasketView => createBasketView(basket, validationResults, basketInfo)
);
 
export const getSubmittedBasket = createSelector(
  getBasketState,
  getBasketValidationResults,
  getBasketInfo,
  (basket, validationResults, basketInfo): BasketView =>
    createBasketView(basket.submittedBasket, validationResults, basketInfo)
);
 
export const getCurrentBasketId = createSelector(getBasketState, basket =>
  basket.basket ? basket.basket.id : undefined
);
 
export const getBasketIdOrCurrent = createSelector(getBasketState, basket =>
  basket.basket ? basket.basket.id : 'current'
);
 
export const getBasketLoading = createSelector(getBasketState, basket => basket.loading);
 
export const getBasketError = createSelector(getBasketState, basket => basket.error);
 
export const getBasketPromotionError = createSelector(getBasketState, basket => basket.promotionError);
 
export const getBasketLastTimeProductAdded = createSelector(getBasketState, basket => basket.lastTimeProductAdded);
 
export const getBasketEligibleAddresses = createSelector(getBasketState, basket => basket.eligibleAddresses);
 
export const getBasketEligibleShippingMethods = createSelector(
  getBasketState,
  basket => basket.eligibleShippingMethods
);
 
export const getBasketEligiblePaymentMethods = createSelector(getBasketState, getLoggedInCustomer, (basket, customer) =>
  basket?.eligiblePaymentMethods?.map(pm => (customer ? pm : { ...pm, saveAllowed: false }))
);
 
export const getEligibleFastCheckoutPaymentMethods = createSelector(
  getBasketState,
  getLoggedInCustomer,
  (basket, customer) =>
    basket?.eligiblePaymentMethods
      ?.filter(pm => pm.capabilities && pm.capabilities.includes('FastCheckout'))
      .map(pm => (customer ? pm : { ...pm, saveAllowed: false }))
);
 
export const getBasketInvoiceAddress = createSelectorFactory<object, Address>(projector =>
  resultMemoize(projector, isEqual)
)(getCurrentBasket, (basket: BasketView) => basket?.invoiceToAddress);
 
export const getBasketShippingAddress = createSelectorFactory<object, Address>(projector =>
  resultMemoize(projector, isEqual)
)(getCurrentBasket, (basket: BasketView) => basket?.commonShipToAddress);
 
export const isBasketInvoiceAndShippingAddressEqual = createSelector(
  getBasketInvoiceAddress,
  getBasketShippingAddress,
  AddressHelper.equal
);