libs/pfe-connector/src/lib/runtime-utils/dynamic-building-block-meta-service/dynamic-building-block-meta-service.ts

Description

This is a dynamic version of the BuildingBlockMetaService. It enables the Runtime Mode to trigger events out of Dynamic Forms.

See the original BuildingBlockMetaService implementation for more details on the functionality.

Metadata

Relationships

Depends on

No results matching.

Index

Methods

Methods

getBusinessEvents
getBusinessEvents(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : any
getData
getData(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : any
getNavigationData
getNavigationData(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : {}
getResources
getResources(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : undefined
hasData
hasData(buildingBlockId: string, key: string | number | symbol)
Parameters :
Name Type Optional
buildingBlockId string No
key string | number | symbol No
Returns : boolean
hasMetaData
hasMetaData(id: string)
Parameters :
Name Type Optional
id string No
Returns : boolean
hasResources
hasResources(id: string)
Parameters :
Name Type Optional
id string No
Returns : boolean
import { PfeNavigationService } from '@allianz/ngx-pfe';
import {
  type BuildingBlockMetaData,
  type BuildingBlockMetaServiceInterface
} from '@allianz/taly-core/building-blocks';
import { type BusinessEventConfig } from '@allianz/taly-core';
import { isDynamicFormBbConfig } from '@allianz/taly-core/schemas';
import { inject, Injectable } from '@angular/core';
import { PfeRuntimeConfigService } from '../pfe-runtime-config/pfe-runtime-config.service';

/**
 * This is a dynamic version of the BuildingBlockMetaService.
 * It enables the Runtime Mode to trigger events out of Dynamic Forms.
 *
 * See the original BuildingBlockMetaService implementation for more details on the functionality.
 */
@Injectable({
  providedIn: 'any'
})
export class DynamicBuildingBlockMetaService implements BuildingBlockMetaServiceInterface {
  private pfeNavigationService = inject(PfeNavigationService);
  private runtimeConfigService = inject(PfeRuntimeConfigService);

  getData(buildingBlockId: string) {
    const currentPageConfig = this.runtimeConfigService
      .pagesConfig()
      .pages?.find((page) => page.id === this.pfeNavigationService.currentPageId$.value);

    const blockConfig = currentPageConfig?.blocks?.find((block) => block.id === buildingBlockId);

    if (blockConfig) {
      return blockConfig;
    } else {
      throw new Error(
        `Could not find block with id ${buildingBlockId} in current page configuration`
      );
    }
  }

  hasData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
    return false;
  }

  getResources(buildingBlockId: string) {
    return undefined;
  }

  getBusinessEvents(buildingBlockId: string) {
    const block = this.getData(buildingBlockId);

    if (isDynamicFormBbConfig(block)) {
      const businessEvents = block.form.fields.reduce(
        (acc: Record<string, BusinessEventConfig>, fieldConfig) => {
          /**
           * The implementation must be synced with the libs/sdk/src/lib/journey/transform/transform.ts
           */
          let configWithMaybeEventConfig;
          if (fieldConfig.type === 'CUSTOM_COMPONENT') {
            configWithMaybeEventConfig = fieldConfig.config;
            if (!configWithMaybeEventConfig) return acc;
          } else {
            configWithMaybeEventConfig = fieldConfig;
          }

          Object.entries(configWithMaybeEventConfig).forEach(
            ([maybeEventName, maybeEventConfig]) => {
              if (!isBusinessEventConfigType(maybeEventConfig)) return;

              const businessEventId = `${fieldConfig.id}.${maybeEventName}`;
              acc[businessEventId] = maybeEventConfig;
            }
          );

          return acc;
        },
        {}
      );

      return businessEvents;
    }
    return {};
  }

  getNavigationData(buildingBlockId: string) {
    return {};
  }

  hasResources(id: string) {
    return false;
  }

  hasMetaData(id: string) {
    return false;
  }
}

function isBusinessEventConfigType(config: unknown): config is BusinessEventConfig {
  return Boolean(config && typeof config === 'object' && 'handlerType' in config);
}

results matching ""

    No results matching ""