All files / src/app/extensions/tacton/store/tacton-config tacton-config.effects.ts

32.25% Statements 10/31
56.25% Branches 9/16
0% Functions 0/15
32.25% Lines 10/31

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 663x 3x 3x 3x   3x 3x 3x       3x 3x     3x                                                                                                    
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { filter, map, mergeMap, switchMap } from 'rxjs/operators';
 
import { getProductEntities, loadProductSuccess, productSpecialUpdate } from 'ish-core/store/shopping/products';
import { mapToPayloadProperty, whenFalsy, whenTruthy } from 'ish-core/utils/operators';
import { StatePropertiesService } from 'ish-core/utils/state-transfer/state-properties.service';
 
import { TactonConfig } from '../../models/tacton-config/tacton-config.model';
 
import { loadTactonConfig, setTactonConfig } from './tacton-config.actions';
import { getTactonConfig } from './tacton-config.selectors';
 
@Injectable()
export class TactonConfigEffects {
  constructor(
    private actions$: Actions,
    private store: Store,
    private statePropertiesService: StatePropertiesService
  ) {}
 
  loadTactonConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadTactonConfig),
      switchMap(() =>
        this.statePropertiesService
          .getStateOrEnvOrDefault<TactonConfig>('TACTON', 'tacton')
          .pipe(map(config => setTactonConfig({ config })))
      )
    )
  );
 
  loadConfigOnInit$ = createEffect(() =>
    this.store.pipe(
      select(getTactonConfig),
      whenFalsy(),
      map(() => loadTactonConfig())
    )
  );
 
  setSpecialTactonProductOnProductLoad$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadProductSuccess),
      mapToPayloadProperty('product'),
      concatLatestFrom(() => this.store.pipe(select(getTactonConfig))),
      filter(([product, config]) => !!config && !!config?.productMappings?.[product.sku]),
      map(([{ sku }]) => productSpecialUpdate({ sku, update: { type: 'TactonProduct' } }))
    )
  );
 
  setSpecialTactonProductOnConfigLoad$ = createEffect(() =>
    this.actions$.pipe(
      ofType(setTactonConfig),
      mapToPayloadProperty('config'),
      whenTruthy(),
      concatLatestFrom(() => this.store.pipe(select(getProductEntities))),
      mergeMap(([config, entities]) =>
        Object.keys(entities)
          .filter(sku => !!config && !!config?.productMappings?.[sku])
          .map(sku => productSpecialUpdate({ sku, update: { type: 'TactonProduct' } }))
      )
    )
  );
}