All files / src/app/core/store/shopping/recommendations recommendations.effects.ts

95.83% Statements 23/24
75% Branches 12/16
88.88% Functions 8/9
95.83% Lines 23/24

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 5819x 19x 19x 19x   19x 19x 19x 19x 19x 19x   19x     19x   51x 51x 51x     51x 51x   8x   8x         8x                 8x       5x       8x                
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, mergeMap } from 'rxjs/operators';
 
import { RecommendationsServiceProvider } from 'ish-core/service-provider/recommendations.service-provider';
import { getCurrentBasket } from 'ish-core/store/customer/basket';
import { getSelectedCategory } from 'ish-core/store/shopping/categories';
import { getSelectedProduct } from 'ish-core/store/shopping/products';
import { loadProductSuccess } from 'ish-core/store/shopping/products/products.actions';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import { recommendationsActions } from './recommendations.actions';
 
@Injectable()
export class RecommendationsEffects {
  constructor(
    private actions$: Actions,
    private recommendationsServiceProvider: RecommendationsServiceProvider,
    private store: Store
  ) {}
 
  loadProductRecommendations$ = createEffect(() =>
    this.actions$.pipe(
      ofType(recommendationsActions.loadProductRecommendations),
      filter(() => !!this.recommendationsServiceProvider.get()),
      mapToPayloadProperty('recommendationsParams'),
      concatLatestFrom(() => [
        this.store.pipe(select(getCurrentBasket)),
        this.store.pipe(select(getSelectedCategory)),
        this.store.pipe(select(getSelectedProduct)),
      ]),
      map(([recommendationsParams, basket, category, product]) => ({
        recommendationsContext: {
          ...recommendationsParams,
          productId: product?.sku,
          categoryId: category?.uniqueId ? category.uniqueId.split('.').pop() : undefined,
          cartProductIds: basket?.lineItems?.map(item => item.productSKU) || [],
        },
      })),
      mergeMap(({ recommendationsContext }) =>
        this.recommendationsServiceProvider
          .get()
          .getRecommendations(recommendationsContext)
          .pipe(
            concatMap(response => [
              recommendationsActions.loadProductRecommendationsSuccess({
                recommendations: response.recommendations,
              }),
              ...response.products.map(product => loadProductSuccess({ product })),
            ]),
            mapErrorToAction(recommendationsActions.loadProductRecommendationsFail)
          )
      )
    )
  );
}