@unchainedshop/core-products 5.0.0-alpha.2 → 5.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/db/ProductsCollection.d.ts +1 -1
- package/lib/migrations/20260611120000-pricing-maxquantity-to-minquantity.d.ts +2 -0
- package/lib/migrations/20260611120000-pricing-maxquantity-to-minquantity.js +54 -0
- package/lib/mock/product.d.ts +3 -10
- package/lib/mock/product.js +4 -4
- package/lib/module/configureProductPrices.d.ts +1 -1
- package/lib/module/configureProductPrices.js +30 -28
- package/lib/module/configureProductsModule.d.ts +1 -1
- package/lib/module/configureProductsModule.js +6 -3
- package/lib/module/utils/getPriceLevels.js +5 -12
- package/package.json +4 -2
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
2
|
+
import { ProductsCollection } from "../db/ProductsCollection.js";
|
|
3
|
+
const logger = createLogger('unchained:core-products:migrations');
|
|
4
|
+
const UNMIGRATED_PRICING_SELECTOR = {
|
|
5
|
+
'commerce.pricing': { $elemMatch: { minQuantity: { $exists: false } } },
|
|
6
|
+
};
|
|
7
|
+
const convertPricingToMinQuantity = (pricing = []) => {
|
|
8
|
+
const groups = new Map();
|
|
9
|
+
for (const level of pricing) {
|
|
10
|
+
const key = `${level.countryCode}:${level.currencyCode}`;
|
|
11
|
+
const group = groups.get(key);
|
|
12
|
+
if (group)
|
|
13
|
+
group.push(level);
|
|
14
|
+
else
|
|
15
|
+
groups.set(key, [level]);
|
|
16
|
+
}
|
|
17
|
+
const nextPricing = [];
|
|
18
|
+
for (const levels of groups.values()) {
|
|
19
|
+
const sorted = [...levels].sort((a, b) => (a.maxQuantity || Number.POSITIVE_INFINITY) - (b.maxQuantity || Number.POSITIVE_INFINITY));
|
|
20
|
+
const seenMax = new Set();
|
|
21
|
+
let previousMax = 0;
|
|
22
|
+
sorted.forEach((level, index) => {
|
|
23
|
+
if (level.maxQuantity) {
|
|
24
|
+
if (seenMax.has(level.maxQuantity)) {
|
|
25
|
+
logger.warn(`Duplicate maxQuantity ${level.maxQuantity} for ${level.countryCode}/${level.currencyCode} — order resolved by stable sort`);
|
|
26
|
+
}
|
|
27
|
+
seenMax.add(level.maxQuantity);
|
|
28
|
+
}
|
|
29
|
+
const { maxQuantity, ...rest } = level;
|
|
30
|
+
nextPricing.push({ ...rest, minQuantity: index === 0 ? 0 : previousMax + 1 });
|
|
31
|
+
previousMax = maxQuantity || previousMax;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return nextPricing;
|
|
35
|
+
};
|
|
36
|
+
export default function migrateCommercePricingToMinQuantity(repository) {
|
|
37
|
+
repository?.register({
|
|
38
|
+
id: 20260611120000,
|
|
39
|
+
name: 'Convert product commerce pricing tiers from maxQuantity to minQuantity',
|
|
40
|
+
up: async () => {
|
|
41
|
+
const { Products } = await ProductsCollection(repository.db);
|
|
42
|
+
let migrated = 0;
|
|
43
|
+
const cursor = Products.find(UNMIGRATED_PRICING_SELECTOR);
|
|
44
|
+
for await (const product of cursor) {
|
|
45
|
+
const nextPricing = convertPricingToMinQuantity((product.commerce?.pricing ?? []));
|
|
46
|
+
await Products.updateOne({ _id: product._id }, { $set: { 'commerce.pricing': nextPricing } });
|
|
47
|
+
migrated += 1;
|
|
48
|
+
}
|
|
49
|
+
if (migrated > 0) {
|
|
50
|
+
logger.info(`Converted commerce pricing tiers to minQuantity on ${migrated} product(s)`);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
package/lib/mock/product.d.ts
CHANGED
|
@@ -8,21 +8,14 @@ declare const _default: {
|
|
|
8
8
|
slugs: string[];
|
|
9
9
|
updated: Date;
|
|
10
10
|
commerce: {
|
|
11
|
-
pricing:
|
|
11
|
+
pricing: {
|
|
12
12
|
amount: number;
|
|
13
|
-
|
|
13
|
+
minQuantity: number;
|
|
14
14
|
isTaxable: boolean;
|
|
15
15
|
isNetPrice: boolean;
|
|
16
16
|
currencyCode: string;
|
|
17
17
|
countryCode: string;
|
|
18
|
-
}
|
|
19
|
-
amount: number;
|
|
20
|
-
maxQuantity: number;
|
|
21
|
-
isTaxable: boolean;
|
|
22
|
-
isNetPrice: boolean;
|
|
23
|
-
currencyCode: string;
|
|
24
|
-
countryCode: string;
|
|
25
|
-
})[];
|
|
18
|
+
}[];
|
|
26
19
|
};
|
|
27
20
|
published: Date;
|
|
28
21
|
};
|
package/lib/mock/product.js
CHANGED
|
@@ -11,7 +11,7 @@ export default {
|
|
|
11
11
|
pricing: [
|
|
12
12
|
{
|
|
13
13
|
amount: 1000,
|
|
14
|
-
|
|
14
|
+
minQuantity: 3,
|
|
15
15
|
isTaxable: false,
|
|
16
16
|
isNetPrice: false,
|
|
17
17
|
currencyCode: 'CHF',
|
|
@@ -19,7 +19,7 @@ export default {
|
|
|
19
19
|
},
|
|
20
20
|
{
|
|
21
21
|
amount: 20000,
|
|
22
|
-
|
|
22
|
+
minQuantity: 0,
|
|
23
23
|
isTaxable: true,
|
|
24
24
|
isNetPrice: true,
|
|
25
25
|
currencyCode: 'ETB',
|
|
@@ -27,7 +27,7 @@ export default {
|
|
|
27
27
|
},
|
|
28
28
|
{
|
|
29
29
|
amount: 1,
|
|
30
|
-
|
|
30
|
+
minQuantity: 0,
|
|
31
31
|
isTaxable: false,
|
|
32
32
|
isNetPrice: false,
|
|
33
33
|
currencyCode: 'ETH',
|
|
@@ -35,7 +35,7 @@ export default {
|
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
37
|
amount: 750,
|
|
38
|
-
|
|
38
|
+
minQuantity: 0,
|
|
39
39
|
isTaxable: false,
|
|
40
40
|
isNetPrice: false,
|
|
41
41
|
currencyCode: 'CHF',
|
|
@@ -18,6 +18,18 @@ export const normalizeRate = (baseCurrency, quoteCurrency, rateRecord) => {
|
|
|
18
18
|
rate = fromDecimals !== targetDecimals ? rate / 10 ** (fromDecimals - targetDecimals) : rate;
|
|
19
19
|
return rate;
|
|
20
20
|
};
|
|
21
|
+
const normalizeProductPrice = (priceLevel) => {
|
|
22
|
+
if (priceLevel.amount === null)
|
|
23
|
+
return null;
|
|
24
|
+
return {
|
|
25
|
+
amount: priceLevel.amount,
|
|
26
|
+
currencyCode: priceLevel.currencyCode,
|
|
27
|
+
countryCode: priceLevel.countryCode,
|
|
28
|
+
minQuantity: priceLevel.minQuantity ?? 0,
|
|
29
|
+
isTaxable: !!priceLevel.isTaxable,
|
|
30
|
+
isNetPrice: !!priceLevel.isNetPrice,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
21
33
|
export const configureProductPricesModule = ({ proxyProducts, db, }) => {
|
|
22
34
|
const catalogPrice = async (product, { countryCode, currencyCode, quantity = 1, }) => {
|
|
23
35
|
const pricing = getPriceLevels({
|
|
@@ -25,24 +37,18 @@ export const configureProductPricesModule = ({ proxyProducts, db, }) => {
|
|
|
25
37
|
currencyCode,
|
|
26
38
|
countryCode,
|
|
27
39
|
});
|
|
28
|
-
const foundPrice = pricing.
|
|
40
|
+
const foundPrice = pricing.filter((level) => (level.minQuantity ?? 0) <= quantity).pop();
|
|
29
41
|
if (!foundPrice)
|
|
30
42
|
return null;
|
|
31
|
-
|
|
32
|
-
isTaxable: false,
|
|
33
|
-
isNetPrice: false,
|
|
34
|
-
...foundPrice,
|
|
35
|
-
};
|
|
36
|
-
if (normalizedPrice.amount !== null) {
|
|
37
|
-
return normalizedPrice;
|
|
38
|
-
}
|
|
39
|
-
return null;
|
|
43
|
+
return normalizeProductPrice(foundPrice);
|
|
40
44
|
};
|
|
41
45
|
return {
|
|
42
46
|
price: catalogPrice,
|
|
43
47
|
priceRange: getPriceRange,
|
|
44
48
|
async catalogPrices(product) {
|
|
45
|
-
return (product.commerce && product.commerce.pricing) || []
|
|
49
|
+
return ((product.commerce && product.commerce.pricing) || [])
|
|
50
|
+
.map(normalizeProductPrice)
|
|
51
|
+
.filter((priceLevel) => Boolean(priceLevel));
|
|
46
52
|
},
|
|
47
53
|
catalogPriceRange: async (product, { quantity = 0, vectors = [], includeInactive = false, countryCode, currencyCode, }) => {
|
|
48
54
|
const products = await proxyProducts(product, vectors, {
|
|
@@ -65,28 +71,24 @@ export const configureProductPricesModule = ({ proxyProducts, db, }) => {
|
|
|
65
71
|
};
|
|
66
72
|
},
|
|
67
73
|
catalogPricesLeveled: async (product, { currencyCode, countryCode }) => {
|
|
68
|
-
|
|
69
|
-
const filteredAndSortedPriceLevels = getPriceLevels({
|
|
74
|
+
const sortedPriceLevels = getPriceLevels({
|
|
70
75
|
product,
|
|
71
76
|
currencyCode,
|
|
72
77
|
countryCode,
|
|
73
78
|
});
|
|
74
|
-
return
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
|
|
79
|
+
return sortedPriceLevels.flatMap((priceLevel, i) => {
|
|
80
|
+
const nextLevel = sortedPriceLevels[i + 1];
|
|
81
|
+
const price = normalizeProductPrice({
|
|
82
|
+
...priceLevel,
|
|
83
|
+
currencyCode,
|
|
84
|
+
countryCode,
|
|
85
|
+
});
|
|
86
|
+
if (!price)
|
|
87
|
+
return [];
|
|
78
88
|
return {
|
|
79
|
-
minQuantity:
|
|
80
|
-
maxQuantity:
|
|
81
|
-
|
|
82
|
-
: max,
|
|
83
|
-
price: {
|
|
84
|
-
isTaxable: !!priceLevel.isTaxable,
|
|
85
|
-
isNetPrice: !!priceLevel.isNetPrice,
|
|
86
|
-
amount: priceLevel.amount,
|
|
87
|
-
currencyCode,
|
|
88
|
-
countryCode,
|
|
89
|
-
},
|
|
89
|
+
minQuantity: priceLevel.minQuantity ?? 0,
|
|
90
|
+
maxQuantity: nextLevel?.minQuantity != null ? nextLevel.minQuantity - 1 : null,
|
|
91
|
+
price,
|
|
90
92
|
};
|
|
91
93
|
});
|
|
92
94
|
},
|
|
@@ -80,7 +80,7 @@ export declare const configureProductsModule: (moduleInput: ModuleInput<Products
|
|
|
80
80
|
countryCode: string;
|
|
81
81
|
}) => Promise<{
|
|
82
82
|
minQuantity: number;
|
|
83
|
-
maxQuantity: number;
|
|
83
|
+
maxQuantity: number | null;
|
|
84
84
|
price: import("../db/ProductsCollection.ts").ProductPrice;
|
|
85
85
|
}[]>;
|
|
86
86
|
rates: {
|
|
@@ -8,6 +8,7 @@ import { configureProductReviewsModule } from "./configureProductReviewsModule.j
|
|
|
8
8
|
import { configureProductTextsModule } from "./configureProductTextsModule.js";
|
|
9
9
|
import { configureProductVariationsModule } from "./configureProductVariationsModule.js";
|
|
10
10
|
import { productsSettings } from "../products-settings.js";
|
|
11
|
+
import migrateCommercePricingToMinQuantity from "../migrations/20260611120000-pricing-maxquantity-to-minquantity.js";
|
|
11
12
|
const PRODUCT_EVENTS = [
|
|
12
13
|
'PRODUCT_CREATE',
|
|
13
14
|
'PRODUCT_REMOVE',
|
|
@@ -23,6 +24,7 @@ const PRODUCT_EVENTS = [
|
|
|
23
24
|
const InternalProductStatus = {
|
|
24
25
|
DRAFT: null,
|
|
25
26
|
};
|
|
27
|
+
const isDraftStatus = (status) => status === ProductStatus.DRAFT || status === InternalProductStatus.DRAFT;
|
|
26
28
|
export const buildFindSelector = ({ slugs, tags, includeDrafts = false, includeDeleted = false, productIds, productSelector, queryString, skus, bundleItemProductIds, proxyAssignmentProductIds, }) => {
|
|
27
29
|
const selector = productSelector ? { ...productSelector } : {};
|
|
28
30
|
if (productIds && !selector._id) {
|
|
@@ -69,10 +71,11 @@ export const buildFindSelector = ({ slugs, tags, includeDrafts = false, includeD
|
|
|
69
71
|
return selector;
|
|
70
72
|
};
|
|
71
73
|
export const configureProductsModule = async (moduleInput) => {
|
|
72
|
-
const { db, options: productsOptions = {} } = moduleInput;
|
|
74
|
+
const { db, migrationRepository, options: productsOptions = {} } = moduleInput;
|
|
73
75
|
registerEvents(PRODUCT_EVENTS);
|
|
74
76
|
await productsSettings.configureSettings(productsOptions);
|
|
75
77
|
const { Products, ProductTexts } = await ProductsCollection(db);
|
|
78
|
+
migrateCommercePricingToMinQuantity(migrationRepository);
|
|
76
79
|
const productTexts = configureProductTextsModule({
|
|
77
80
|
Products,
|
|
78
81
|
ProductTexts,
|
|
@@ -94,7 +97,7 @@ export const configureProductsModule = async (moduleInput) => {
|
|
|
94
97
|
return deletedResult.deletedCount;
|
|
95
98
|
};
|
|
96
99
|
const publishProduct = async (product) => {
|
|
97
|
-
if (product.status
|
|
100
|
+
if (isDraftStatus(product.status)) {
|
|
98
101
|
await Products.updateOne(generateDbFilterById(product._id), {
|
|
99
102
|
$set: {
|
|
100
103
|
status: ProductStatus.ACTIVE,
|
|
@@ -187,7 +190,7 @@ export const configureProductsModule = async (moduleInput) => {
|
|
|
187
190
|
return product.status === ProductStatus.ACTIVE;
|
|
188
191
|
},
|
|
189
192
|
isDraft: (product) => {
|
|
190
|
-
return product.status
|
|
193
|
+
return isDraftStatus(product.status);
|
|
191
194
|
},
|
|
192
195
|
normalizedStatus: (product) => {
|
|
193
196
|
return product.status === null ? ProductStatus.DRAFT : product.status;
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
export const getPriceLevels = (params) => {
|
|
2
|
-
return (params.product?.commerce?.pricing || [])
|
|
3
|
-
.toSorted((
|
|
4
|
-
if (leftMaxQuantity === rightMaxQuantity || (!leftMaxQuantity && !rightMaxQuantity))
|
|
5
|
-
return 0;
|
|
6
|
-
if (!leftMaxQuantity)
|
|
7
|
-
return 1;
|
|
8
|
-
if (!rightMaxQuantity)
|
|
9
|
-
return -1;
|
|
10
|
-
return leftMaxQuantity - rightMaxQuantity;
|
|
11
|
-
})
|
|
2
|
+
return ((params.product?.commerce?.pricing || [])
|
|
3
|
+
.toSorted((left, right) => (left.minQuantity ?? 0) - (right.minQuantity ?? 0))
|
|
12
4
|
.filter((priceLevel) => {
|
|
13
5
|
if (!params.currencyCode)
|
|
14
6
|
return priceLevel.countryCode === params.countryCode;
|
|
15
|
-
return (priceLevel.currencyCode === params.currencyCode &&
|
|
16
|
-
|
|
7
|
+
return (priceLevel.currencyCode === params.currencyCode &&
|
|
8
|
+
priceLevel.countryCode === params.countryCode);
|
|
9
|
+
}));
|
|
17
10
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-products",
|
|
3
3
|
"description": "Product catalog management module for the Unchained Engine",
|
|
4
|
-
"version": "5.0.0-alpha.
|
|
4
|
+
"version": "5.0.0-alpha.3",
|
|
5
5
|
"main": "lib/products-index.js",
|
|
6
6
|
"types": "lib/products-index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -36,10 +36,12 @@
|
|
|
36
36
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@unchainedshop/events": "^5.0.0-alpha.1",
|
|
39
|
+
"@unchainedshop/logger": "^5.0.0-alpha.1",
|
|
40
|
+
"@unchainedshop/mongodb": "^5.0.0-alpha.1",
|
|
39
41
|
"@unchainedshop/utils": "^5.0.0-alpha.1"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"@types/node": "^
|
|
44
|
+
"@types/node": "^26.2.0",
|
|
43
45
|
"typescript": "^5.8.3"
|
|
44
46
|
}
|
|
45
47
|
}
|