All files / src/app/extensions/product-notifications/facades product-notifications.facade.ts

31.81% Statements 7/22
37.5% Branches 3/8
0% Functions 0/9
31.81% Lines 7/22

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 754x 4x 4x 4x     4x           4x                   4x                                                                                                        
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { isEqual } from 'lodash-es';
import { Observable, distinctUntilChanged, map, switchMap } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { selectRouteParam } from 'ish-core/store/core/router';
 
import {
  ProductNotification,
  ProductNotificationType,
} from '../models/product-notification/product-notification.model';
import {
  getProductNotificationBySku,
  getProductNotificationsByType,
  getProductNotificationsError,
  getProductNotificationsLoading,
  productNotificationsActions,
} from '../store/product-notification';
 
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class ProductNotificationsFacade {
  constructor(private store: Store) {}
 
  private productNotifications$(type: ProductNotificationType) {
    this.store.dispatch(productNotificationsActions.loadProductNotifications({ type }));
    return this.store.pipe(select(getProductNotificationsByType(type)));
  }
 
  // get a product notification by sku and the type
  productNotificationBySku$(sku: string, type: ProductNotificationType) {
    this.store.dispatch(productNotificationsActions.loadProductNotifications({ type }));
 
    return this.store.pipe(select(getProductNotificationBySku(sku, type))).pipe(
      map(notifications => (notifications?.length ? notifications[0] : undefined)),
      distinctUntilChanged(isEqual)
    );
  }
 
  // create a product notification
  createProductNotification(productNotification: ProductNotification) {
    this.store.dispatch(productNotificationsActions.createProductNotification({ productNotification }));
  }
 
  // update a product notification
  updateProductNotification(sku: string, productNotification: ProductNotification) {
    this.store.dispatch(productNotificationsActions.updateProductNotification({ sku, productNotification }));
  }
 
  // delete a product notification
  deleteProductNotification(
    sku: string,
    productNotificationType: ProductNotificationType,
    productNotificationId: string
  ) {
    this.store.dispatch(
      productNotificationsActions.deleteProductNotification({ sku, productNotificationType, productNotificationId })
    );
  }
 
  productNotificationsLoading$: Observable<boolean> = this.store.pipe(select(getProductNotificationsLoading));
  productNotificationsError$: Observable<HttpError> = this.store.pipe(select(getProductNotificationsError));
 
  productNotificationType$ = this.store.pipe(
    select(selectRouteParam('notificationType')),
    distinctUntilChanged(),
    map(type => type || 'price')
  );
 
  productNotificationsByRoute$ = this.productNotificationType$.pipe(
    switchMap(type => this.productNotifications$(type as ProductNotificationType))
  );
}