Skip to content

Commit f39d65b

Browse files
committed
Adjust catalogPrice calculation and fix integration tests
1 parent 483c91a commit f39d65b

5 files changed

Lines changed: 26 additions & 25 deletions

File tree

packages/core-products/src/module/configureProductPrices.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,21 @@ export const configureProductPricesModule = ({
6262
quantity = 1,
6363
}: { countryCode: string; currencyCode?: string; quantity?: number },
6464
): Promise<ProductPrice | null> => {
65-
const pricing = getPriceLevels({
66-
product,
67-
currencyCode,
68-
countryCode,
69-
});
65+
const pricing = getPriceLevels({ product, currencyCode, countryCode });
66+
if (!pricing.length) return null;
7067

71-
const foundPrice = pricing.find((level) => !level.minQuantity || level.minQuantity >= quantity);
72-
if (!foundPrice) return null;
68+
// Filter tiers that match the quantity, take the last because pricing is sorted
69+
const matched = pricing.filter((level) => (level.minQuantity ?? 0) <= quantity).pop();
7370

74-
const normalizedPrice = {
75-
isTaxable: false,
76-
isNetPrice: false,
77-
...foundPrice,
78-
};
71+
if (!matched) return null;
7972

80-
if (normalizedPrice.amount !== null) {
81-
return normalizedPrice;
82-
}
83-
return null;
73+
return {
74+
isTaxable: !!matched.isTaxable,
75+
isNetPrice: !!matched.isNetPrice,
76+
amount: matched.amount,
77+
currencyCode: matched.currencyCode,
78+
countryCode: matched.countryCode,
79+
};
8480
};
8581

8682
return {

tests/cart-products.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ test.describe('Cart: Product Items', () => {
323323
product {
324324
_id
325325
... on SimpleProduct {
326-
simulatedPrice(quantity: 4) {
326+
simulatedPrice {
327327
isTaxable
328328
isNetPrice
329329
amount
@@ -352,12 +352,11 @@ test.describe('Cart: Product Items', () => {
352352
`,
353353
variables: {
354354
productId: LeveledPricingProduct._id,
355-
quantity: 1,
356355
},
357356
});
358357
assert.partialDeepStrictEqual(defaultTierPrice?.order.items[0], {
359358
quantity: 4,
360-
total: { amount: 40000, currencyCode: 'CHF' },
359+
total: { amount: 8000, currencyCode: 'CHF' },
361360
product: {
362361
_id: 'leveled-pricing-product',
363362
simulatedPrice: {

tests/product-commerce.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ test.describe('Product: Commerce', async () => {
6969
productId: SimpleProduct._id,
7070
commerce: {
7171
pricing: [
72+
{
73+
amount: 1000,
74+
isTaxable: true,
75+
isNetPrice: false,
76+
currencyCode: 'CHF',
77+
countryCode: 'CH',
78+
},
7279
{
7380
amount: 100,
7481
minQuantity: 50,
@@ -85,7 +92,7 @@ test.describe('Product: Commerce', async () => {
8592
assert.partialDeepStrictEqual(updateProductCommerce, {
8693
_id: SimpleProduct._id,
8794
catalogPrice: {
88-
amount: 100,
95+
amount: 1000,
8996
isTaxable: true,
9097
isNetPrice: false,
9198
currencyCode: 'CHF',

tests/products.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ test.describe('Products', () => {
18631863
maxPrice: {
18641864
isTaxable: true,
18651865
isNetPrice: false,
1866-
amount: 2000000,
1866+
amount: 10000000,
18671867
currencyCode: 'CHF',
18681868
},
18691869
});
@@ -1956,13 +1956,13 @@ test.describe('Products', () => {
19561956
minPrice: {
19571957
isTaxable: true,
19581958
isNetPrice: false,
1959-
amount: 500000,
1959+
amount: 2000000,
19601960
currencyCode: 'CHF',
19611961
},
19621962
maxPrice: {
19631963
isTaxable: true,
19641964
isNetPrice: false,
1965-
amount: 30000000,
1965+
amount: 2000000,
19661966
currencyCode: 'CHF',
19671967
},
19681968
});
@@ -2010,7 +2010,7 @@ test.describe('Products', () => {
20102010
maxPrice: {
20112011
isTaxable: true,
20122012
isNetPrice: false,
2013-
amount: 2000000,
2013+
amount: 10000000,
20142014
currencyCode: 'CHF',
20152015
},
20162016
});

tests/seeds/products.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export const LeveledPricingProduct = {
6666
isNetPrice: false,
6767
currencyCode: 'CHF',
6868
countryCode: 'CH',
69-
minQuantity: 0,
7069
},
7170
],
7271
},

0 commit comments

Comments
 (0)