All files / src/app/core/facades app.facade.ts

74.57% Statements 44/59
40.9% Branches 9/22
43.47% Functions 10/23
75% Lines 42/56

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 150216x 216x 216x 216x 216x 216x   216x               216x 216x 216x 216x 216x 216x 216x 216x     216x 64x 64x   64x       64x 64x 64x   64x 64x 64x   64x 64x 64x   64x   64x                             64x   1152x 109x     1152x 103x     1152x 6x     64x         64x                 64x                 1x                         42x                                                                          
import { getCurrencySymbol } from '@angular/common';
import { Injectable } from '@angular/core';
import { NavigationCancel, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { combineLatest, merge, noop } from 'rxjs';
import { filter, map, sample, shareReplay, startWith, withLatestFrom } from 'rxjs/operators';
 
import {
  getAvailableLocales,
  getCurrentCurrency,
  getCurrentLocale,
  getDeviceType,
  getICMBaseURL,
  getRestEndpoint,
} from 'ish-core/store/core/configuration';
import { businessError, getGeneralError, getGeneralErrorType } from 'ish-core/store/core/error';
import { selectPath } from 'ish-core/store/core/router';
import { getExtraConfigParameter, getServerConfigParameter } from 'ish-core/store/core/server-config';
import { getBreadcrumbData, getHeaderType, getWrapperClass, isStickyHeader } from 'ish-core/store/core/viewconf';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
import { getAllCountries, loadCountries } from 'ish-core/store/general/countries';
import { getRegionsByCountryCode, loadRegions } from 'ish-core/store/general/regions';
import { whenTruthy } from 'ish-core/utils/operators';
 
@Injectable({ providedIn: 'root' })
export class AppFacade {
  constructor(private store: Store, private router: Router) {
    this.routingInProgress$.subscribe(noop);
 
    store.pipe(select(getICMBaseURL)).subscribe(icmBaseUrl => (this.icmBaseUrl = icmBaseUrl));
  }
  icmBaseUrl: string;
 
  headerType$ = this.store.pipe(select(getHeaderType));
  deviceType$ = this.store.pipe(select(getDeviceType));
  stickyHeader$ = this.store.pipe(select(isStickyHeader));
 
  currentLocale$ = this.store.pipe(select(getCurrentLocale));
  availableLocales$ = this.store.pipe(select(getAvailableLocales));
  currentCurrency$ = this.store.pipe(select(getCurrentCurrency));
 
  generalError$ = this.store.pipe(select(getGeneralError));
  generalErrorType$ = this.store.pipe(select(getGeneralErrorType));
  breadcrumbData$ = this.store.pipe(select(getBreadcrumbData));
 
  getRestEndpoint$ = this.store.pipe(select(getRestEndpoint));
 
  appWrapperClasses$ = combineLatest([
    this.store.pipe(
      select(getWrapperClass),
      sample(this.router.events.pipe(filter(event => event instanceof NavigationEnd)))
    ),
    this.store.pipe(
      select(isStickyHeader),
      map(isSticky => (isSticky ? 'sticky-header' : ''))
    ),
    this.store.pipe(
      select(getDeviceType),
      map(deviceType => (deviceType === 'mobile' ? 'sticky-header' : ''))
    ),
  ]).pipe(map(classes => classes.filter(c => !!c)));
 
  routingInProgress$ = merge(
    this.router.events.pipe(
      filter(event => event instanceof NavigationStart),
      map(() => true)
    ),
    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(() => false)
    ),
    this.router.events.pipe(
      filter(event => event instanceof NavigationCancel),
      map(() => false)
    )
  ).pipe(startWith(true), shareReplay(1));
  path$ = this.store.pipe(select(selectPath));
 
  /**
   * selects whether the current application type is 'REST'. If the application type is unknown it returns true
   */
  isAppTypeREST$ = this.store.pipe(
    select(
      getServerConfigParameter<'intershop.REST' | 'intershop.B2CResponsive' | 'intershop.SMBResponsive'>(
        'application.applicationType'
      )
    ),
    map(appType => appType === 'intershop.REST' || !appType)
  );
 
  customerRestResource$ = this.isAppTypeREST$.pipe(
    withLatestFrom(this.store.pipe(select(getLoggedInCustomer))),
    map(([isRest, customer]) => AppFacade.getCustomerRestResource(!customer || customer.isBusinessCustomer, isRest))
  );
 
  static getCustomerRestResource(
    isBusinessCustomer: boolean,
    isAppTypeREST: boolean
  ): 'customers' | 'privatecustomers' {
    return isAppTypeREST && !isBusinessCustomer ? 'privatecustomers' : 'customers';
  }
 
  setBusinessError(error: string) {
    this.store.dispatch(businessError({ error }));
  }
 
  /**
   * extracts a specific server setting from the store.
   *
   * @param path the path to the server setting, starting from the serverConfig/_config store.
   */
  serverSetting$<T>(path: string) {
    return this.store.pipe(select(getServerConfigParameter<T>(path)));
  }
 
  // not-dead-code
  /**
   * extracts a specific extra server setting from the store (intended for custom ConfigurationJSON)
   *
   * @param path the path to the server setting, starting from the serverConfig/extra store
   */
  extraSetting$<T>(path: string) {
    return this.store.pipe(select(getExtraConfigParameter<T>(path)));
  }
 
  /**
   * returns the currency symbol for the currency parameter in the current locale.
   * If no parameter is given, the the default currency is taken instead of it.
   *
   * @param currency The currency
   */
  currencySymbol$(currency?: string) {
    return this.currentLocale$.pipe(
      whenTruthy(),
      withLatestFrom(this.currentCurrency$),
      map(([locale, defaultCurrency]) => getCurrencySymbol(currency || defaultCurrency, 'narrow', locale))
    );
  }
 
  countries$() {
    this.store.dispatch(loadCountries());
    return this.store.pipe(select(getAllCountries));
  }
 
  regions$(countryCode: string) {
    this.store.dispatch(loadRegions({ countryCode }));
    return this.store.pipe(select(getRegionsByCountryCode(countryCode)));
  }
}