All files / src/app/core/models/product-prices product-prices.mapper.ts

88.88% Statements 16/18
100% Branches 11/11
85.71% Functions 6/7
86.66% Lines 13/15

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 5320x   20x               58x 58x 58x 6x     52x       4x 4x                 20x   5x 4x   5x                                  
import { Injectable } from '@angular/core';
 
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
import { PriceItem, ScaledPriceItem } from 'ish-core/models/price-item/price-item.model';
 
import { ProductPriceDetailsData, ProductPriceItemData } from './product-prices.interface';
import { ProductPriceDetails } from './product-prices.model';
 
// get ProductPriceItem for a single product from a set of possible prices
function getSinglePrice(prices: ProductPriceItemData[]): PriceItem {
  if (prices?.length !== 0) {
    const singlePrice = prices?.find(price => price?.minQuantity?.value === 1);
    if (singlePrice) {
      return PriceItemMapper.fromPriceItem(singlePrice);
    }
  }
  return;
}
 
function getScaledPriceItem(priceItem: ProductPriceItemData): ScaledPriceItem {
  if (priceItem) {
    return {
      ...PriceItemMapper.fromPriceItem(priceItem),
      minQuantity: priceItem?.minQuantity?.value,
    };
  }
  return;
}
 
@Injectable({ providedIn: 'root' })
export class ProductPricesMapper {
  static fromData(data: ProductPriceDetailsData): ProductPriceDetails {
    const scaledPrices = data?.prices?.SalePrice?.map(priceItem => getScaledPriceItem(priceItem))
      .filter(priceItem => priceItem.minQuantity !== 1)
      .sort((a, b) => a.minQuantity - b.minQuantity);
    return {
      sku: data?.sku,
      prices: {
        salePrice: getSinglePrice(data?.prices?.SalePrice) ?? getSinglePrice(data?.prices?.ListPrice),
        listPrice: getSinglePrice(data?.prices?.ListPrice),
        scaledPrices,
        minSalePrice: getSinglePrice(data?.prices?.MinSalePrice) ?? getSinglePrice(data?.prices?.MinListPrice),
        minListPrice: getSinglePrice(data?.prices?.MinListPrice),
        maxSalePrice: getSinglePrice(data?.prices?.MaxSalePrice) ?? getSinglePrice(data?.prices?.MaxListPrice),
        maxListPrice: getSinglePrice(data?.prices?.MaxListPrice),
        summedUpSalePrice:
          getSinglePrice(data?.prices?.SummedUpSalePrice) ?? getSinglePrice(data?.prices?.SummedUpListPrice),
        summedUpListPrice: getSinglePrice(data?.prices?.SummedUpListPrice),
      },
    };
  }
}