All files / src/app/core/configurations configuration.meta.ts

76.74% Statements 33/43
57.14% Branches 12/21
81.81% Functions 9/11
76.74% Lines 33/43

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  100x         100x 100x     100x     7x   77x     16x           7x               7x 42x 5x 5x   7x           7x 2x 1x   1x       7x       7x       7x       7x 6x   1x                           100x 8x   8x       71x 71x 7x 7x 7x     7x     71x      
import { Params } from '@angular/router';
import { RouterNavigationPayload, routerNavigationAction } from '@ngrx/router-store';
import { ActionReducer } from '@ngrx/store';
 
import { FeatureToggleType } from 'ish-core/feature-toggle.module';
import { SparqueConfig } from 'ish-core/models/sparque/sparque-config.model';
import { applyConfiguration } from 'ish-core/store/core/configuration';
import { ConfigurationState, configurationReducer } from 'ish-core/store/core/configuration/configuration.reducer';
import { CoreState } from 'ish-core/store/core/core-store';
import { RouterState } from 'ish-core/store/core/router/router.reducer';
import { mergeDeep } from 'ish-core/utils/functions';
 
class SimpleParamMap {
  constructor(private obj: Params) {}
  has(key: string): boolean {
    return this.obj[key] !== undefined;
  }
  get<T>(key: string): T {
    return this.obj[key];
  }
}
 
// eslint-disable-next-line complexity
function extractConfigurationParameters(state: ConfigurationState, paramMap: SimpleParamMap) {
  const keys: (keyof ConfigurationState)[] = [
    'channel',
    'application',
    'lang',
    'currency',
    'identityProvider',
    'sparque',
  ];
  const properties: Partial<ConfigurationState> = keys
    .filter(key => paramMap.has(key) && paramMap.get(key) !== 'default')
    .map(key => ({ [key]: paramMap.get(key) }))
    .reduce((acc, val) => ({ ...acc, ...val }), {});
 
  Iif (paramMap.has('icmHost') && paramMap.get('icmHost') !== 'default') {
    const scheme = paramMap.get('icmScheme') || 'https';
    const port = paramMap.get('icmPort') || '443';
    properties.baseURL = `${scheme}://${paramMap.get('icmHost')}:${port}`;
  }
 
  if (paramMap.has('features') && paramMap.get('features') !== 'default') {
    if (paramMap.get('features') === 'none') {
      properties.features = [];
    } else {
      properties.features = paramMap.get<string>('features').split(/,/g) as FeatureToggleType[];
    }
  }
 
  Iif (paramMap.has('addFeatures')) {
    properties.addFeatures = paramMap.get<string>('addFeatures').split(/,/g) as FeatureToggleType[];
  }
 
  Iif (paramMap.has('device')) {
    properties._deviceType = paramMap.get('device');
  }
 
  Iif (paramMap.has('sparque')) {
    properties.sparque = mapSparqueConfig(paramMap.get<string>('sparque'));
  }
 
  if (Object.keys(properties).length) {
    return configurationReducer(state, applyConfiguration(properties));
  }
  return state;
}
 
function mapSparqueConfig(sparque: string): SparqueConfig {
  return sparque.split(',').reduce((acc, item) => {
    const [key, value] = item.split('=');
    acc[key] = decodeURIComponent(value);
    return acc;
  }, <SparqueConfig>{});
}
 
/**
 * meta reducer for overriding client side state if supplied by server
 */
export function configurationMeta(reducer: ActionReducer<CoreState>): ActionReducer<CoreState> {
  let first = false;
 
  return (
    state: CoreState,
    action: typeof routerNavigationAction & { payload: RouterNavigationPayload<RouterState> }
  ) => {
    let newState = state;
    if (!first && action.type === routerNavigationAction.type) {
      const payload = action.payload;
      const paramMap = new SimpleParamMap(payload.routerState.params);
      newState = mergeDeep(newState, {
        configuration: extractConfigurationParameters(newState.configuration, paramMap),
      });
      first = true;
    }
 
    return reducer(newState, action);
  };
}