All files / src/app/extensions/product-notifications/store/product-notification product-notification.effects.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 15/15
100% Lines 29/29

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 1092x 2x 2x   2x 2x   2x   2x     2x   13x 13x     13x 13x       8x   4x               13x 13x         5x 2x                       13x 13x         5x 2x                       13x 13x       5x 2x                           13x 13x                 1x              
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, map, mergeMap, switchMap } from 'rxjs';
 
import { displayErrorMessage, displaySuccessMessage } from 'ish-core/store/core/messages';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { ProductNotificationsService } from '../../services/product-notifications/product-notifications.service';
 
import { productNotificationsActions, productNotificationsApiActions } from './product-notification.actions';
 
@Injectable()
export class ProductNotificationEffects {
  constructor(
    private actions$: Actions,
    private productNotificationsService: ProductNotificationsService
  ) {}
 
  loadProductNotifications$ = createEffect(() =>
    this.actions$.pipe(
      ofType(productNotificationsActions.loadProductNotifications),
      mapToPayloadProperty('type'),
      switchMap(type =>
        this.productNotificationsService.getProductNotifications(type).pipe(
          map(productNotifications =>
            productNotificationsApiActions.loadProductNotificationsSuccess({ productNotifications, type })
          ),
          mapErrorToAction(productNotificationsApiActions.loadProductNotificationsFail)
        )
      )
    )
  );
 
  createProductNotification$ = createEffect(() =>
    this.actions$.pipe(
      ofType(productNotificationsActions.createProductNotification),
      mapToPayload(),
      whenTruthy(),
      mergeMap(payload =>
        this.productNotificationsService.createProductNotification(payload.productNotification).pipe(
          mergeMap(productNotification => [
            productNotificationsApiActions.createProductNotificationSuccess({ productNotification }),
            displaySuccessMessage({
              message: 'product.notification.create.success.message',
            }),
          ]),
          mapErrorToAction(productNotificationsApiActions.createProductNotificationFail)
        )
      )
    )
  );
 
  updateProductNotification$ = createEffect(() =>
    this.actions$.pipe(
      ofType(productNotificationsActions.updateProductNotification),
      mapToPayload(),
      whenTruthy(),
      concatMap(payload =>
        this.productNotificationsService.updateProductNotification(payload.sku, payload.productNotification).pipe(
          mergeMap(productNotification => [
            productNotificationsApiActions.updateProductNotificationSuccess({ productNotification }),
            displaySuccessMessage({
              message: 'product.notification.update.success.message',
            }),
          ]),
          mapErrorToAction(productNotificationsApiActions.updateProductNotificationFail)
        )
      )
    )
  );
 
  deleteProductNotification$ = createEffect(() =>
    this.actions$.pipe(
      ofType(productNotificationsActions.deleteProductNotification),
      mapToPayload(),
      mergeMap(payload =>
        this.productNotificationsService.deleteProductNotification(payload.sku, payload.productNotificationType).pipe(
          mergeMap(() => [
            productNotificationsApiActions.deleteProductNotificationSuccess({
              productNotificationId: payload.productNotificationId,
            }),
            displaySuccessMessage({
              message: 'product.notification.delete.success.message',
            }),
          ]),
          mapErrorToAction(productNotificationsApiActions.deleteProductNotificationFail)
        )
      )
    )
  );
 
  displayProductNotificationErrorMessage$ = createEffect(() =>
    this.actions$.pipe(
      ofType(
        productNotificationsApiActions.loadProductNotificationsFail,
        productNotificationsApiActions.createProductNotificationFail,
        productNotificationsApiActions.updateProductNotificationFail,
        productNotificationsApiActions.deleteProductNotificationFail
      ),
      mapToPayloadProperty('error'),
      map(error =>
        displayErrorMessage({
          message: error.message,
        })
      )
    )
  );
}