All files / src/app/extensions/punchout/facades punchout.facade.ts

28.26% Statements 13/46
30% Branches 3/10
0% Functions 0/18
28.88% Lines 13/45

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 1298x 8x 8x 8x     8x 8x 8x         8x           8x               8x 8x 8x                       8x                                                                                                                                                                              
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, combineLatest } from 'rxjs';
import { distinctUntilChanged, filter, map, switchMap } from 'rxjs/operators';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { selectQueryParam } from 'ish-core/store/core/router';
import { decamelizeString } from 'ish-core/utils/functions';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { CxmlConfiguration } from '../models/cxml-configuration/cxml-configuration.model';
import { OciConfigurationItem } from '../models/oci-configuration-item/oci-configuration-item.model';
import { PunchoutType, PunchoutUser } from '../models/punchout-user/punchout-user.model';
import {
  cxmlConfigurationActions,
  getCxmlConfiguration,
  getCxmlConfigurationError,
  getCxmlConfigurationLoading,
} from '../store/cxml-configuration';
import {
  getOciConfiguration,
  getOciConfigurationError,
  getOciConfigurationLoading,
  getOciFormatters,
  getOciPlaceholders,
  ociConfigurationActions,
} from '../store/oci-configuration';
import { transferPunchoutBasket } from '../store/punchout-functions';
import { getPunchoutTypes, getPunchoutTypesError, getPunchoutTypesLoading } from '../store/punchout-types';
import {
  addPunchoutUser,
  deletePunchoutUser,
  getPunchoutError,
  getPunchoutLoading,
  getPunchoutUsers,
  getSelectedPunchoutUser,
  updatePunchoutUser,
} from '../store/punchout-users';
 
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class PunchoutFacade {
  constructor(private store: Store) {}
 
  punchoutLoading$ = combineLatest([
    this.store.pipe(select(getPunchoutLoading)),
    this.store.pipe(select(getPunchoutTypesLoading)),
  ]).pipe(map(([loading, typesLoading]) => loading || (typesLoading as boolean)));
 
  punchoutError$ = this.store.pipe(select(getPunchoutError));
  punchoutTypesError$: Observable<HttpError> = this.store.pipe(select(getPunchoutTypesError));
  supportedPunchoutTypes$: Observable<PunchoutType[]> = this.store.pipe(select(getPunchoutTypes));
 
  selectedPunchoutType$ = combineLatest([
    this.store.pipe(select(selectQueryParam('format'))),
    this.store.pipe(select(getPunchoutTypes)),
  ]).pipe(
    filter(([format, types]) => !!format || types?.length > 0),
    map(([format, types]) => (format as PunchoutType) || types[0]),
    distinctUntilChanged()
  );
 
  punchoutUsersByRoute$() {
    return this.selectedPunchoutType$.pipe(
      whenTruthy(),
      switchMap(type => this.store.pipe(select(getPunchoutUsers(type))))
    );
  }
 
  selectedPunchoutUser$ = this.store.pipe(select(getSelectedPunchoutUser));
 
  addPunchoutUser(user: PunchoutUser) {
    this.store.dispatch(addPunchoutUser({ user }));
  }
 
  updatePunchoutUser(user: PunchoutUser) {
    this.store.dispatch(updatePunchoutUser({ user }));
  }
 
  deletePunchoutUser(user: PunchoutUser) {
    this.store.dispatch(deletePunchoutUser({ user }));
  }
 
  transferBasket() {
    this.store.dispatch(transferPunchoutBasket());
  }
 
  ociConfiguration$() {
    this.store.dispatch(ociConfigurationActions.loadOCIOptionsAndConfiguration());
    return this.store.pipe(select(getOciConfiguration));
  }
  ociConfigurationLoading$ = this.store.pipe(select(getOciConfigurationLoading));
  ociConfigurationError$ = this.store.pipe(select(getOciConfigurationError));
  ociFormatterSelectOptions$ = this.store.pipe(
    select(getOciFormatters),
    whenTruthy(),
    map(formatters => formatters.map(f => ({ label: decamelizeString(f), value: f }))),
    map(options => {
      options.push({
        value: '',
        label: 'account.punchout.configuration.option.none.label',
      });
      return options;
    })
  );
  ociPlaceholders$ = this.store.pipe(select(getOciPlaceholders));
 
  updateOciConfiguration(configuration: OciConfigurationItem[]) {
    this.store.dispatch(ociConfigurationActions.updateOCIConfiguration({ configuration }));
  }
 
  cxmlConfiguration$() {
    this.store.dispatch(cxmlConfigurationActions.loadCXMLConfiguration());
    return this.store.pipe(select(getCxmlConfiguration));
  }
 
  cxmlConfigurationLoading$ = this.store.pipe(select(getCxmlConfigurationLoading));
 
  cxmlConfigurationError$ = this.store.pipe(select(getCxmlConfigurationError));
 
  updateCxmlConfiguration(configuration: CxmlConfiguration[]) {
    this.store.dispatch(cxmlConfigurationActions.updateCXMLConfiguration({ configuration }));
  }
 
  resetCxmlConfiguration() {
    this.store.dispatch(cxmlConfigurationActions.resetCXMLConfiguration());
  }
}