File

feature-libs/product-configurator/common/shared/utils/common-configurator-utils.service.ts

Description

Utilities for generic configuration

Index

Methods

Constructor

constructor(userIdService: UserIdService)
Parameters :
Name Type Optional
userIdService UserIdService No

Methods

decomposeOwnerId
decomposeOwnerId(ownerId: string)

Decomposes an owner ID into documentId and entryNumber

Parameters :
Name Type Optional Description
ownerId string No
  • ID of owner
Returns : any

object containing documentId and entryNumber

getCartId
getCartId(cart?: Cart)

Gets cart ID (which can be either its guid or its code)

Parameters :
Name Type Optional Description
cart Cart Yes
  • Cart
Returns : string
  • Cart identifier
getComposedOwnerId
getComposedOwnerId(documentId: string, entryNumber: number)

Composes owner ID from document ID and entry number

Parameters :
Name Type Optional Description
documentId string No
  • ID of document the entry is part of, like the order or quote code
entryNumber number No
  • Entry number
Returns : string
  • owner ID
getNumberOfIssues
getNumberOfIssues(cartItem: OrderEntry)

Retrieves the number of issues at the cart item.

Parameters :
Name Type Optional Description
cartItem OrderEntry No
  • Cart item
Returns : number
  • the number of issues at the cart item
hasIssues
hasIssues(cartItem: OrderEntry)

Verifies whether the item has any issues.

Parameters :
Name Type Optional Description
cartItem OrderEntry No
  • Cart item
Returns : boolean
  • whether there are any issues
isActiveCartContext
isActiveCartContext(cartItemContext: CartItemContext | undefined)

Determines whether we are in the context of an active cart

Parameters :
Name Type Optional Description
cartItemContext CartItemContext | undefined No

Cart item context

Returns : Observable<boolean>

Item part of an active cart?

isAttributeBasedConfigurator
isAttributeBasedConfigurator(configuratorType: string | undefined)

Verifies whether the configurator type is an attribute based one.

Parameters :
Name Type Optional Description
configuratorType string | undefined No
  • Configurator type
Returns : boolean
  • 'True' if the expected configurator type, otherwise 'false'
isBundleBasedConfigurator
isBundleBasedConfigurator(configuratorType: string | undefined)

Verifies whether the configurator type is a bundle based one.

Parameters :
Name Type Optional Description
configuratorType string | undefined No
  • Configurator type
Returns : boolean
  • 'True' if the expected configurator type, otherwise 'fasle'
setOwnerKey
setOwnerKey(owner: CommonConfigurator.Owner)

Compiles a unique key for a configuration owner and sets it into the 'key' attribute

Parameters :
Name Type Optional Description
owner CommonConfigurator.Owner No
  • Specifies the owner of a product configuration
Returns : void
import { Injectable } from '@angular/core';
import {
  Cart,
  OCC_USER_ID_ANONYMOUS,
  OrderEntry,
  PromotionLocation,
  UserIdService,
} from '@spartacus/core';
import { CartItemContext } from '@spartacus/storefront';
import { EMPTY, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
  CommonConfigurator,
  ConfiguratorType,
  OrderEntryStatus,
} from '../../core/model/common-configurator.model';
import { ConfiguratorModelUtils } from './configurator-model-utils';

/**
 * Utilities for generic configuration
 */
@Injectable({ providedIn: 'root' })
export class CommonConfiguratorUtilsService {
  constructor(protected userIdService: UserIdService) {}
  /**
   * Compiles a unique key for a configuration owner and sets it into the 'key'
   * attribute
   * @param {CommonConfigurator.Owner }owner - Specifies the owner of a product configuration
   */
  setOwnerKey(owner: CommonConfigurator.Owner) {
    owner.key = ConfiguratorModelUtils.getOwnerKey(owner.type, owner.id);
  }

  /**
   * Composes owner ID from document ID and entry number
   * @param {string} documentId - ID of document the entry is part of, like the order or quote code
   * @param {string} entryNumber - Entry number
   * @returns {string} - owner ID
   */
  getComposedOwnerId(documentId: string, entryNumber: number): string {
    return documentId + '+' + entryNumber;
  }

  /**
   * Decomposes an owner ID into documentId and entryNumber
   * @param {string} ownerId - ID of owner
   * @returns {any} object containing documentId and entryNumber
   */
  decomposeOwnerId(ownerId: string): any {
    const parts: string[] = ownerId.split('+');
    if (parts.length !== 2) {
      throw new Error(
        'We only expect 2 parts in ownerId, separated by +, but was: ' + ownerId
      );
    }
    const result = { documentId: parts[0], entryNumber: parts[1] };
    return result;
  }
  /**
   * Gets cart ID (which can be either its guid or its code)
   * @param {Cart} cart - Cart
   * @returns {string} - Cart identifier
   */
  getCartId(cart?: Cart): string {
    const cartId =
      cart?.user?.uid === OCC_USER_ID_ANONYMOUS ? cart.guid : cart?.code;
    if (!cartId) {
      throw new Error('Cart ID is not defined');
    }
    return cartId;
  }

  /**
   * Verifies whether the item has any issues.
   *
   * @param {OrderEntry} cartItem - Cart item
   * @returns {boolean} - whether there are any issues
   */
  hasIssues(cartItem: OrderEntry): boolean {
    return this.getNumberOfIssues(cartItem) > 0;
  }

  /**
   * Retrieves the number of issues at the cart item.
   *
   * @param {OrderEntry} cartItem - Cart item
   * @returns {number} - the number of issues at the cart item
   */
  getNumberOfIssues(cartItem: OrderEntry): number {
    let numberOfIssues = 0;
    cartItem?.statusSummaryList?.forEach((statusSummary) => {
      if (statusSummary.status === OrderEntryStatus.Error) {
        const numberOfIssuesFromStatus = statusSummary.numberOfIssues;
        numberOfIssues = numberOfIssuesFromStatus
          ? numberOfIssuesFromStatus
          : 0;
      }
    });
    return numberOfIssues;
  }

  /**
   * Verifies whether the configurator type is an attribute based one.
   *
   * @param {string} configuratorType - Configurator type
   * @returns {boolean} - 'True' if the expected configurator type, otherwise 'false'
   */
  isAttributeBasedConfigurator(configuratorType: string | undefined): boolean {
    if (configuratorType) {
      return (
        configuratorType === ConfiguratorType.VARIANT ||
        configuratorType === ConfiguratorType.TEXTFIELD
      );
    }
    return false;
  }

  /**
   * Verifies whether the configurator type is a bundle based one.
   *
   * @param {string} configuratorType - Configurator type
   * @returns {boolean} - 'True' if the expected configurator type, otherwise 'fasle'
   */
  isBundleBasedConfigurator(configuratorType: string | undefined): boolean {
    if (configuratorType) {
      return configuratorType === ConfiguratorType.CPQ;
    }
    return false;
  }

  /**
   * Determines whether we are in the context of an active cart
   * @param cartItemContext Cart item context
   * @returns Item part of an active cart?
   */
  isActiveCartContext(
    cartItemContext: CartItemContext | undefined
  ): Observable<boolean> {
    return (cartItemContext?.location$ ?? EMPTY).pipe(
      map(
        (location) =>
          location !== PromotionLocation.SaveForLater &&
          location !== PromotionLocation.SavedCart
      )
    );
  }
}

result-matching ""

    No results matching ""