Skip to content

Commit

Permalink
fix: use productprices api for retail sets and product master (#1262)
Browse files Browse the repository at this point in the history
* cleanup of obsolete product prices related code
  • Loading branch information
Eisie96 authored Sep 5, 2022
1 parent 8138800 commit 208decc
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 146 deletions.
6 changes: 6 additions & 0 deletions src/app/core/models/price-item/price-item.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export class PriceItemHelper {
scaledPrices: priceDetails?.prices?.scaledPrices?.map(priceItem =>
PriceItemHelper.selectScaledPriceType(priceItem, type)
),
minSalePrice: PriceItemHelper.selectType(priceDetails?.prices?.minSalePrice, type),
minListPrice: PriceItemHelper.selectType(priceDetails?.prices?.minListPrice, type),
maxSalePrice: PriceItemHelper.selectType(priceDetails?.prices?.maxSalePrice, type),
maxListPrice: PriceItemHelper.selectType(priceDetails?.prices?.maxListPrice, type),
summedUpSalePrice: PriceItemHelper.selectType(priceDetails?.prices?.summedUpSalePrice, type),
summedUpListPrice: PriceItemHelper.selectType(priceDetails?.prices?.summedUpListPrice, type),
};
}
}
1 change: 1 addition & 0 deletions src/app/core/models/price/price.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class PriceHelper {
}
}

// not-dead-code
static min(p1: Price, p2: Price): Price {
PriceHelper.sanityChecks(p1, p2);
return {
Expand Down
12 changes: 9 additions & 3 deletions src/app/core/models/price/price.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ export interface ScaledPrice extends Price {
}

export interface Pricing {
listPrice: Price;
salePrice: Price;
scaledPrices: ScaledPrice[];
listPrice?: Price;
salePrice?: Price;
scaledPrices?: ScaledPrice[];
minSalePrice?: Price;
minListPrice?: Price;
maxSalePrice?: Price;
maxListPrice?: Price;
summedUpSalePrice?: Price;
summedUpListPrice?: Price;
}

export * from './price.helper';
10 changes: 8 additions & 2 deletions src/app/core/models/product-prices/product-prices.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';
export interface ProductPriceDetailsData {
sku: string;
prices: {
SalePrice: ProductPriceItemData[];
ListPrice: ProductPriceItemData[];
SalePrice?: ProductPriceItemData[];
ListPrice?: ProductPriceItemData[];
MinSalePrice?: ProductPriceItemData[];
MinListPrice?: ProductPriceItemData[];
MaxSalePrice?: ProductPriceItemData[];
MaxListPrice?: ProductPriceItemData[];
SummedUpSalePrice?: ProductPriceItemData[];
SummedUpListPrice?: ProductPriceItemData[];
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/app/core/models/product-prices/product-prices.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe('Product Prices Mapper', () => {
"net": 1,
"type": "PriceItem",
},
"maxListPrice": undefined,
"maxSalePrice": undefined,
"minListPrice": undefined,
"minSalePrice": undefined,
"salePrice": Object {
"currency": "USD",
"gross": 2,
Expand All @@ -57,6 +61,8 @@ describe('Product Prices Mapper', () => {
"type": "PriceItem",
},
],
"summedUpListPrice": undefined,
"summedUpSalePrice": undefined,
},
"sku": "abc",
}
Expand Down Expand Up @@ -96,6 +102,10 @@ describe('Product Prices Mapper', () => {
"net": 1,
"type": "PriceItem",
},
"maxListPrice": undefined,
"maxSalePrice": undefined,
"minListPrice": undefined,
"minSalePrice": undefined,
"salePrice": Object {
"currency": "USD",
"gross": 2,
Expand All @@ -111,6 +121,8 @@ describe('Product Prices Mapper', () => {
"type": "PriceItem",
},
],
"summedUpListPrice": undefined,
"summedUpSalePrice": undefined,
},
"sku": "abc",
}
Expand Down
7 changes: 7 additions & 0 deletions src/app/core/models/product-prices/product-prices.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export class ProductPricesMapper {
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),
},
};
}
Expand Down
12 changes: 9 additions & 3 deletions src/app/core/models/product-prices/product-prices.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { PriceItem, ScaledPriceItem } from 'ish-core/models/price-item/price-ite
export interface ProductPriceDetails {
sku: string;
prices: {
salePrice: PriceItem;
listPrice: PriceItem;
scaledPrices: ScaledPriceItem[];
salePrice?: PriceItem;
listPrice?: PriceItem;
scaledPrices?: ScaledPriceItem[];
minSalePrice?: PriceItem;
minListPrice?: PriceItem;
maxSalePrice?: PriceItem;
maxListPrice?: PriceItem;
summedUpSalePrice?: PriceItem;
summedUpListPrice?: PriceItem;
};
}
68 changes: 0 additions & 68 deletions src/app/core/models/product/product.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AttributeGroup } from 'ish-core/models/attribute-group/attribute-group.
import { AttributeGroupTypes } from 'ish-core/models/attribute-group/attribute-group.types';
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { ProductPriceDetails } from 'ish-core/models/product-prices/product-prices.model';

import { ProductDataStub } from './product.interface';
import { Product, ProductCompletenessLevel, ProductHelper } from './product.model';
Expand Down Expand Up @@ -186,73 +185,6 @@ describe('Product Helper', () => {
});
});

describe('calculatePriceRange()', () => {
it('should return empty object when no products are supplied', () => {
expect(ProductHelper.calculatePriceRange(undefined, undefined, undefined)).toBeEmpty();
expect(ProductHelper.calculatePriceRange([], [], undefined)).toBeEmpty();
});

it('should return the single object if only one element is supplied', () => {
const product = { sku: '1' } as Product;
const productPrice = {
sku: '1',
prices: { salePrice: { gross: 1, currency: 'EUR' } },
} as ProductPriceDetails;
expect(ProductHelper.calculatePriceRange([product], [productPrice], 'gross')).toEqual(productPrice);
});

it('should calculate a range when multiple elements are supplied', () => {
const product1 = { sku: '1' } as Product;
const productPrice1 = {
sku: '1',
prices: { salePrice: { gross: 1, currency: 'EUR' }, listPrice: { gross: 2, currency: 'EUR' } },
} as ProductPriceDetails;

const product2 = { sku: '2' } as Product;
const productPrice2 = {
sku: '2',
prices: { salePrice: { gross: 3, currency: 'EUR' }, listPrice: { gross: 4, currency: 'EUR' } },
} as ProductPriceDetails;

const product3 = { sku: '3' } as Product;
const productPrice3 = {
sku: '3',
prices: { salePrice: { gross: 5, currency: 'EUR' }, listPrice: { gross: 6, currency: 'EUR' } },
} as ProductPriceDetails;

expect(
ProductHelper.calculatePriceRange(
[product1, product2, product3],
[productPrice1, productPrice2, productPrice3],
'gross'
)
).toMatchInlineSnapshot(`
Object {
"minListPrice": Object {
"currency": "EUR",
"type": "Money",
"value": 2,
},
"minSalePrice": Object {
"currency": "EUR",
"type": "Money",
"value": 1,
},
"summedUpListPrice": Object {
"currency": "EUR",
"type": "Money",
"value": 12,
},
"summedUpSalePrice": Object {
"currency": "EUR",
"type": "Money",
"value": 9,
},
}
`);
});
});

describe('compare', () => {
let product: Product;
let compareProduct1: Product;
Expand Down
39 changes: 0 additions & 39 deletions src/app/core/models/product/product.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { intersection } from 'lodash-es';

import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { Image } from 'ish-core/models/image/image.model';
import { PriceItemHelper } from 'ish-core/models/price-item/price-item.helper';
import { PriceHelper } from 'ish-core/models/price/price.model';
import { ProductPriceDetails } from 'ish-core/models/product-prices/product-prices.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';

import {
Expand All @@ -27,13 +24,6 @@ export enum ProductCompletenessLevel {
List = 2,
}

// not-dead-code
export type ProductPrices = Partial<
Pick<ProductRetailSet, 'minListPrice' | 'minSalePrice' | 'summedUpListPrice' | 'summedUpSalePrice'>
> &
Partial<Pick<VariationProductMaster, 'minListPrice' | 'minSalePrice' | 'maxListPrice' | 'maxSalePrice'>> &
Partial<Pick<ProductPriceDetails, 'prices'>>;

export class ProductHelper {
/**
* Get primary product image based on image type
Expand Down Expand Up @@ -138,35 +128,6 @@ export class ProductHelper {
return;
}

// not-dead-code
static calculatePriceRange(
products: Product[],
productPrices: ProductPriceDetails[],
priceType: 'gross' | 'net'
): ProductPrices {
if ((!products || !products.length) && (!productPrices || !productPrices.length)) {
return {};
} else if (products.length === 1) {
return productPrices.find(price => price.sku === products[0].sku);
} else {
const prices = products.map(p => productPrices.find(productPrice => productPrice.sku === p.sku));
return {
minListPrice: prices
.map(p => PriceItemHelper.selectType(p?.prices?.listPrice, priceType))
.reduce(PriceHelper.min),
minSalePrice: prices
.map(p => PriceItemHelper.selectType(p?.prices?.salePrice, priceType))
.reduce(PriceHelper.min),
summedUpListPrice: prices
.map(p => PriceItemHelper.selectType(p?.prices?.listPrice, priceType))
.reduce(PriceHelper.sum),
summedUpSalePrice: prices
.map(p => PriceItemHelper.selectType(p?.prices?.salePrice, priceType))
.reduce(PriceHelper.sum),
};
}
}

/**
* Determines the set of common attribute names for the compare products.
*
Expand Down
10 changes: 0 additions & 10 deletions src/app/core/models/product/product.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,9 @@ export interface ProductData {
stepOrderQuantity?: number;
packingUnit: string;

// If Variation Master and Retail Set {
minListPrice?: PriceData;
maxListPrice?: PriceData;
minSalePrice?: PriceData;
maxSalePrice?: PriceData;
// }
variationAttributeValues?: VariationAttribute[];
variableVariationAttributes?: VariationAttribute[];
partOfRetailSet: boolean;
// If Retail Set {
summedUpListPrice?: PriceData;
summedUpSalePrice?: PriceData;
// }

attachments?: AttachmentData[];
variations?: unknown;
Expand Down
9 changes: 0 additions & 9 deletions src/app/core/models/product/product.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { CategoryData } from 'ish-core/models/category/category.interface';
import { CategoryMapper } from 'ish-core/models/category/category.mapper';
import { ImageMapper } from 'ish-core/models/image/image.mapper';
import { Link } from 'ish-core/models/link/link.model';
import { PriceMapper } from 'ish-core/models/price/price.mapper';
import { SeoAttributesMapper } from 'ish-core/models/seo-attributes/seo-attributes.mapper';

import { SkuQuantityType } from './product.helper';
Expand Down Expand Up @@ -249,10 +248,6 @@ export class ProductMapper {
if (data.productMaster) {
return {
...product,
minListPrice: PriceMapper.fromData(data.minListPrice),
minSalePrice: PriceMapper.fromData(data.minSalePrice),
maxListPrice: PriceMapper.fromData(data.maxListPrice),
maxSalePrice: PriceMapper.fromData(data.maxSalePrice),
variationAttributeValues: data.variationAttributeValues,
type: 'VariationProductMaster',
};
Expand All @@ -272,10 +267,6 @@ export class ProductMapper {
return {
...product,
type: 'RetailSet',
minListPrice: PriceMapper.fromData(data.minListPrice),
minSalePrice: PriceMapper.fromData(data.minSalePrice),
summedUpListPrice: PriceMapper.fromData(data.summedUpListPrice),
summedUpSalePrice: PriceMapper.fromData(data.summedUpSalePrice),
};
} else {
return product;
Expand Down
9 changes: 0 additions & 9 deletions src/app/core/models/product/product.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Attachment } from 'ish-core/models/attachment/attachment.model';
import { AttributeGroup } from 'ish-core/models/attribute-group/attribute-group.model';
import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { Image } from 'ish-core/models/image/image.model';
import { Price } from 'ish-core/models/price/price.model';
import { VariationAttribute } from 'ish-core/models/product-variation/variation-attribute.model';
import { SeoAttributes } from 'ish-core/models/seo-attributes/seo-attributes.model';

Expand Down Expand Up @@ -45,18 +44,10 @@ export interface VariationProduct extends Product {
export interface VariationProductMaster extends Product {
type: 'VariationProductMaster';
variationAttributeValues?: VariationAttribute[];
minListPrice?: Price;
minSalePrice?: Price;
maxListPrice?: Price;
maxSalePrice?: Price;
}

export interface ProductRetailSet extends Product {
type: 'RetailSet';
minListPrice: Price;
minSalePrice: Price;
summedUpListPrice: Price;
summedUpSalePrice: Price;
}

export interface ProductBundle extends Product {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export class ProductPriceComponent implements OnInit {
isListPriceLessThanSalePrice: prices.listPrice?.value < prices.salePrice?.value,
priceSavings: prices.listPrice && prices.salePrice && PriceHelper.diff(prices.listPrice, prices.salePrice),
lowerPrice:
(ProductHelper.isMasterProduct(product) || ProductHelper.isRetailSet(product)) && product.minSalePrice,
(ProductHelper.isMasterProduct(product) || ProductHelper.isRetailSet(product)) && prices.minSalePrice,
upperPrice: ProductHelper.isMasterProduct(product)
? product.maxSalePrice
? prices.maxSalePrice
: ProductHelper.isRetailSet(product)
? product.summedUpSalePrice
? prices.summedUpSalePrice
: undefined,
}))
);
Expand Down

0 comments on commit 208decc

Please sign in to comment.