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

95.55% Statements 43/45
65% Branches 13/20
90.9% Functions 20/22
95.55% Lines 43/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 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 1485x 5x 5x 5x 5x 5x   5x 5x 5x     5x 5x   5x                           5x     5x   14x 14x 14x 14x     14x 14x               14x 14x       5x 4x             14x 14x                 2x   2x 2x             14x 14x       3x   2x 2x                             14x 14x       3x   2x 2x                             14x 14x       5x 4x                          
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { from } from 'rxjs';
import { concatMap, exhaustMap, filter, map, mergeMap, withLatestFrom } from 'rxjs/operators';
 
import { displaySuccessMessage } from 'ish-core/store/core/messages';
import { selectQueryParam, selectRouteParam, selectUrl } from 'ish-core/store/core/router';
import { mapErrorToAction, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { PunchoutType } from '../../models/punchout-user/punchout-user.model';
import { PunchoutService } from '../../services/punchout/punchout.service';
import { loadPunchoutTypesSuccess } from '../punchout-types';
 
import {
  addPunchoutUser,
  addPunchoutUserFail,
  addPunchoutUserSuccess,
  deletePunchoutUser,
  deletePunchoutUserFail,
  deletePunchoutUserSuccess,
  loadPunchoutUsers,
  loadPunchoutUsersFail,
  loadPunchoutUsersSuccess,
  updatePunchoutUser,
  updatePunchoutUserFail,
  updatePunchoutUserSuccess,
} from './punchout-users.actions';
import { getSelectedPunchoutUser } from './punchout-users.selectors';
 
@Injectable()
export class PunchoutUsersEffects {
  constructor(
    private punchoutService: PunchoutService,
    private actions$: Actions,
    private router: Router,
    private store: Store
  ) {}
 
  loadPunchoutUsersOnChange$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadPunchoutTypesSuccess),
      mapToPayloadProperty('types'),
      concatLatestFrom(() => this.store.pipe(select(selectQueryParam('format')))),
      map(([types, selectedType]) => loadPunchoutUsers({ type: (selectedType as PunchoutType) || types[0] }))
    )
  );
 
  loadPunchoutUsers$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadPunchoutUsers),
      mapToPayloadProperty('type'),
      concatMap(type =>
        this.punchoutService.getUsers(type).pipe(
          map(users => loadPunchoutUsersSuccess({ users })),
          mapErrorToAction(loadPunchoutUsersFail)
        )
      )
    )
  );
 
  loadDetailedUser$ = createEffect(() =>
    this.store.pipe(
      select(selectRouteParam('PunchoutLogin')),
      whenTruthy(),
      withLatestFrom(
        this.store.pipe(select(getSelectedPunchoutUser)),
        this.store.pipe(select(selectUrl)),
        this.store.pipe(select(selectQueryParam('format')))
      ),
      // don't load user on configuration page
      filter(([, user, url]) => !user || !url.includes('cxmlConfiguration')),
      concatMap(([, , , format]) =>
        this.punchoutService.getUsers(format as PunchoutType).pipe(
          map(users => loadPunchoutUsersSuccess({ users })),
          mapErrorToAction(loadPunchoutUsersFail)
        )
      )
    )
  );
 
  createPunchoutUser$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addPunchoutUser),
      mapToPayloadProperty('user'),
      concatMap(newUser =>
        this.punchoutService.createUser(newUser).pipe(
          concatMap(user =>
            from(this.router.navigate([`/account/punchout`], { queryParams: { format: user.punchoutType } })).pipe(
              mergeMap(() => [
                addPunchoutUserSuccess({ user }),
                displaySuccessMessage({
                  message: 'account.punchout.user.created.message',
                  messageParams: { 0: `${user.login}` },
                }),
              ])
            )
          ),
          mapErrorToAction(addPunchoutUserFail)
        )
      )
    )
  );
 
  updatePunchoutUser$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updatePunchoutUser),
      mapToPayloadProperty('user'),
      concatMap(changedUser =>
        this.punchoutService.updateUser(changedUser).pipe(
          concatMap(user =>
            from(this.router.navigate([`/account/punchout`], { queryParams: { format: user.punchoutType } })).pipe(
              mergeMap(() => [
                updatePunchoutUserSuccess({ user }),
                displaySuccessMessage({
                  message: 'account.punchout.user.updated.message',
                  messageParams: { 0: `${user.login}` },
                }),
              ])
            )
          ),
          mapErrorToAction(updatePunchoutUserFail)
        )
      )
    )
  );
 
  deletePunchoutUser$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deletePunchoutUser),
      mapToPayloadProperty('user'),
      exhaustMap(user =>
        this.punchoutService.deleteUser(user).pipe(
          mergeMap(() => [
            deletePunchoutUserSuccess({ login: user.login }),
            displaySuccessMessage({
              message: 'account.punchout.user.delete.confirmation',
              messageParams: { 0: user.login },
            }),
          ]),
          mapErrorToAction(deletePunchoutUserFail)
        )
      )
    )
  );
}