All files / src/app/extensions/punchout/store/cxml-configuration cxml-configuration.effects.ts

96.15% Statements 25/26
75% Branches 9/12
90.9% Functions 10/11
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 58 59 60 61 62 63 64 656x 6x 6x 6x   6x 6x   6x 6x   6x     6x 6x   6x 6x   4x   4x 4x             6x 6x     7x   7x 4x                       6x 6x                            
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, map, mergeMap } from 'rxjs/operators';
 
import { displayErrorMessage, displaySuccessMessage } from 'ish-core/store/core/messages';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import { PunchoutService } from '../../services/punchout/punchout.service';
import { getSelectedPunchoutUser } from '../punchout-users';
 
import { cxmlConfigurationActions, cxmlConfigurationApiActions } from './cxml-configuration.actions';
 
@Injectable()
export class CxmlConfigurationEffects {
  constructor(private actions$: Actions, private punchoutService: PunchoutService, private store: Store) {}
 
  loadCxmlConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(cxmlConfigurationActions.loadCXMLConfiguration),
      concatLatestFrom(() => this.store.pipe(select(getSelectedPunchoutUser))),
      concatMap(([_, user]) =>
        this.punchoutService.getCxmlConfiguration(user.id).pipe(
          map(configuration => cxmlConfigurationApiActions.loadCXMLConfigurationSuccess({ configuration })),
          mapErrorToAction(cxmlConfigurationApiActions.loadCXMLConfigurationFail)
        )
      )
    )
  );
 
  updateCxmlConfiguration$ = createEffect(() =>
    this.actions$.pipe(
      ofType(cxmlConfigurationActions.updateCXMLConfiguration),
      mapToPayloadProperty('configuration'),
      concatLatestFrom(() => this.store.pipe(select(getSelectedPunchoutUser))),
      concatMap(([configuration, user]) =>
        this.punchoutService.updateCxmlConfiguration(configuration, user.id).pipe(
          mergeMap(configuration => [
            cxmlConfigurationApiActions.updateCXMLConfigurationSuccess({ configuration }),
            displaySuccessMessage({
              message: 'account.punchout.cxml.configuration.save_success.message',
            }),
          ]),
          mapErrorToAction(cxmlConfigurationApiActions.updateCXMLConfigurationFail)
        )
      )
    )
  );
 
  displayCxmlConfigurationErrorMessage$ = createEffect(() =>
    this.actions$.pipe(
      ofType(
        cxmlConfigurationApiActions.updateCXMLConfigurationFail,
        cxmlConfigurationApiActions.loadCXMLConfigurationFail
      ),
      mapToPayloadProperty('error'),
      map(error =>
        displayErrorMessage({
          message: error.message,
        })
      )
    )
  );
}