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

23.75% Statements 19/80
60% Branches 12/20
0% Functions 0/45
23.75% Lines 19/80

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 2313x 3x 3x 3x 3x 3x 3x                       3x 3x 3x 3x 3x 3x   3x 3x 3x   3x                         3x     3x                                                                                                                                                                                                                                                                                                                                                                                  
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { combineLatest, from, of } from 'rxjs';
import {
  catchError,
  concatMap,
  distinctUntilChanged,
  filter,
  map,
  switchMap,
  take,
  throttleTime,
  withLatestFrom,
} from 'rxjs/operators';
 
import { generateProductUrl } from 'ish-core/routing/product/product.route';
import { displayErrorMessage, displaySuccessMessage } from 'ish-core/store/core/messages';
import { selectRouteParam, selectUrl } from 'ish-core/store/core/router';
import { getLoggedInUser } from 'ish-core/store/customer/user';
import { getSelectedProduct } from 'ish-core/store/shopping/products';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { TactonSelfServiceApiService } from '../../services/tacton-self-service-api/tacton-self-service-api.service';
import { getSavedTactonConfiguration } from '../saved-tacton-configuration';
import { getTactonProductForSelectedProduct } from '../tacton-config';
 
import {
  acceptTactonConfigurationConflictResolution,
  changeTactonConfigurationStep,
  clearTactonConfiguration,
  commitTactonConfigurationValue,
  continueConfigureTactonProduct,
  setCurrentConfiguration,
  startConfigureTactonProduct,
  submitTactonConfiguration,
  submitTactonConfigurationFail,
  submitTactonConfigurationSuccess,
  uncommitTactonConfigurationValue,
} from './product-configuration.actions';
import { getCurrentProductConfigurationStepName } from './product-configuration.selectors';
 
@Injectable()
export class ProductConfigurationEffects {
  constructor(
    private actions$: Actions,
    private store: Store,
    private tactonSelfServiceApiService: TactonSelfServiceApiService,
    private router: Router
  ) {}
 
  startOrContinueTactonProductConfiguration$ = createEffect(() =>
    combineLatest([
      this.store.pipe(select(getTactonProductForSelectedProduct), whenTruthy()),
      this.store.pipe(
        select(selectUrl),
        map(url => url?.startsWith('/configure')),
        distinctUntilChanged()
      ),
      this.store.pipe(select(getLoggedInUser), whenTruthy()),
    ]).pipe(
      filter(([, url]) => !!url),
      switchMap(([productPath]) =>
        of(productPath).pipe(withLatestFrom(this.store.pipe(select(getSavedTactonConfiguration(productPath)))))
      ),
      map(([productPath, savedConfig]) =>
        savedConfig ? continueConfigureTactonProduct({ savedConfig }) : startConfigureTactonProduct({ productPath })
      )
    )
  );
 
  startTactonProductConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(startConfigureTactonProduct),
      mapToPayloadProperty('productPath'),
      switchMap(productPath =>
        this.tactonSelfServiceApiService
          .startConfiguration(productPath)
          .pipe(map(configuration => setCurrentConfiguration({ configuration })))
      )
    )
  );
 
  continueTactonProductConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(continueConfigureTactonProduct),
      mapToPayloadProperty('savedConfig'),
      switchMap(savedConfig =>
        this.tactonSelfServiceApiService.continueConfiguration(savedConfig).pipe(
          map(configuration => setCurrentConfiguration({ configuration })),
          catchError(() => of(startConfigureTactonProduct({ productPath: savedConfig.productId })))
        )
      )
    )
  );
 
  routeToActiveConfigurationStep$ = createEffect(
    () =>
      this.store.pipe(
        select(getCurrentProductConfigurationStepName),
        withLatestFrom(
          this.store.pipe(select(selectRouteParam('sku'))),
          this.store.pipe(select(selectRouteParam('mainStep')))
        ),
        filter(([step, , previous]) => step && previous !== step),
        switchMap(([step, sku]) => from(this.router.navigate(['/configure', sku, step])))
      ),
    { dispatch: false }
  );
 
  clearConfigurationWhenRoutingAway$ = createEffect(() =>
    this.actions$.pipe(
      ofType(routerNavigatedAction),
      filter(action => !action.payload.routerState.url.startsWith('/configure')),
      map(() => clearTactonConfiguration())
    )
  );
 
  switchConfigurationStep$ = createEffect(() =>
    this.actions$.pipe(
      ofType(changeTactonConfigurationStep),
      mapToPayloadProperty('step'),
      switchMap(step =>
        this.tactonSelfServiceApiService
          .changeStep(step)
          .pipe(map(configuration => setCurrentConfiguration({ configuration })))
      )
    )
  );
 
  commitTactonConfigurationValue$ = createEffect(() =>
    this.actions$.pipe(
      ofType(commitTactonConfigurationValue),
      throttleTime(1000),
      mapToPayload(),
      concatMap(({ valueId, value }) =>
        this.tactonSelfServiceApiService
          .commitValue(valueId, value)
          .pipe(map(configuration => setCurrentConfiguration({ configuration })))
      )
    )
  );
 
  uncommitTactonConfigurationValue$ = createEffect(() =>
    this.actions$.pipe(
      ofType(uncommitTactonConfigurationValue),
      throttleTime(1000),
      mapToPayload(),
      concatMap(({ valueId }) =>
        this.tactonSelfServiceApiService
          .uncommitValue(valueId)
          .pipe(map(configuration => setCurrentConfiguration({ configuration })))
      )
    )
  );
 
  acceptTactonConfigurationConflictResolution$ = createEffect(() =>
    this.actions$.pipe(
      ofType(commitTactonConfigurationValue),
      mapToPayload(),
      switchMap(({ valueId, value }) =>
        this.actions$.pipe(
          ofType(acceptTactonConfigurationConflictResolution),
          switchMap(() =>
            this.tactonSelfServiceApiService
              .acceptConflictResolution(valueId, value)
              .pipe(map(configuration => setCurrentConfiguration({ configuration })))
          )
        )
      )
    )
  );
 
  submitTactonConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(submitTactonConfiguration),
      switchMap(() => this.store.pipe(select(getTactonProductForSelectedProduct), whenTruthy(), take(1))),
      switchMap(productPath =>
        of(productPath).pipe(withLatestFrom(this.store.pipe(select(getSavedTactonConfiguration(productPath)))))
      ),
      switchMap(([, savedConfiguration]) =>
        this.tactonSelfServiceApiService.submitConfiguration(savedConfiguration).pipe(
          map(() =>
            submitTactonConfigurationSuccess({ productId: savedConfiguration.productId, user: savedConfiguration.user })
          ),
          mapErrorToAction(submitTactonConfigurationFail, {
            productId: savedConfiguration.productId,
            user: savedConfiguration.user,
          })
        )
      )
    )
  );
 
  submitTactonConfigurationSuccessToast$ = createEffect(() =>
    this.actions$.pipe(
      ofType(submitTactonConfigurationSuccess),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInUser))),
      map(([, user]) =>
        displaySuccessMessage({
          message: 'tacton.submit.success.message',
          messageParams: { 0: user.email },
        })
      )
    )
  );
 
  submitTactonConfigurationErrorToast$ = createEffect(() =>
    this.actions$.pipe(
      ofType(submitTactonConfigurationFail),
      map(() =>
        displayErrorMessage({
          message: 'tacton.submit.error.message',
        })
      )
    )
  );
 
  submitTactonConfigurationRedirect$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(submitTactonConfigurationSuccess, submitTactonConfigurationFail),
        concatLatestFrom(() => this.store.pipe(select(getSelectedProduct))),
        switchMap(([, product]) => from(this.router.navigateByUrl(generateProductUrl(product))))
      ),
    { dispatch: false }
  );
}