File

libs/pfe-connector/src/lib/pfe-state.service.ts

Extends

TalyStateService

Index

Methods

Methods

Public evaluateCondition$
evaluateCondition$(condition: string)
Inherited from TalyStateService
Parameters :
Name Type Optional
condition string No
Returns : Observable<boolean>
Public interpolateFromStore$
interpolateFromStore$(message: string | undefined)
Inherited from TalyStateService
Parameters :
Name Type Optional
message string | undefined No
Returns : Observable<string>
import {
  ExpressionCondition,
  JSON_PATH_REGEX,
  PfeBusinessService,
  PfeConditionsService
} from '@allianz/ngx-pfe';
import { TalyStateService } from '@allianz/taly-core';
import { inject, Injectable } from '@angular/core';
import { combineLatest, map, merge, Observable, of } from 'rxjs';

@Injectable()
export class PfeStateService extends TalyStateService {
  private pfeBusinessService = inject(PfeBusinessService);
  private pfeConditionsService = inject(PfeConditionsService);

  public interpolateFromStore$(message: string | undefined): Observable<string> {
    if (!message) {
      return of('');
    }

    const keys = message.match(JSON_PATH_REGEX) || [];

    if (keys.length === 0) {
      return of(message);
    }

    const values$ = keys
      .map(function removeCurlyBraces(key) {
        return key.slice(1, -1);
      })
      .map((key) => {
        return this.pfeBusinessService.getObservableForExpressionKey(key, true);
      });

    return combineLatest(values$).pipe(
      map((values: (string | undefined)[]) => {
        let newMessage = message;
        values.forEach((value, index) => {
          const toReplace = keys[index];
          newMessage = newMessage.replace(toReplace, value ?? '');
        });
        return newMessage;
      })
    );
  }

  public evaluateCondition$(condition: string): Observable<boolean> {
    if (JSON_PATH_REGEX.test(condition)) {
      return this.createObservableForPfeExpressionCondition(condition);
    } else {
      return this.createObservableForJsonPathExpression(condition);
    }
  }

  private createObservableForPfeExpressionCondition(condition: string): Observable<boolean> {
    const expressionCondition: ExpressionCondition[] = [{ expression: condition }];
    // get all property keys which are in curly brackets (e.g. "{key1} == {key2}" => ["key1", "key2"])
    const propertyKeys = condition.match(JSON_PATH_REGEX)?.map((match) => match.slice(1, -1)) || [];

    return merge(
      ...propertyKeys.map((key) => this.pfeBusinessService.getObservableForExpressionKey(key, true))
    ).pipe(
      map(() =>
        this.pfeConditionsService.evaluateConditions(
          expressionCondition,
          this.pfeBusinessService.getFullState()
        )
      )
    );
  }

  private createObservableForJsonPathExpression(condition: string): Observable<boolean> {
    return this.pfeBusinessService
      .getObservableForExpressionKey(condition, true)
      .pipe(map((stateValue) => Boolean(stateValue)));
  }
}

results matching ""

    No results matching ""