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

86.48% Statements 32/37
60% Branches 12/20
100% Functions 9/9
86.48% Lines 32/37

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  98x       98x 98x     98x     7x   63x     16x           7x 7x 35x 5x 5x   7x           7x 2x 1x   1x       7x       7x       7x 6x   1x           98x 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 { 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'];
  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');
  }
 
  if (Object.keys(properties).length) {
    return configurationReducer(state, applyConfiguration(properties));
  }
  return state;
}
 
/**
 * 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);
  };
}