@unchainedshop/core-products 4.8.18 → 4.8.20

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.
@@ -0,0 +1,2 @@
1
+ import type { MigrationRepository } from '@unchainedshop/mongodb';
2
+ export default function migrateCommercePricingToMinQuantity(repository: MigrationRepository): void;
@@ -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
+ }
@@ -23,6 +23,7 @@ const PRODUCT_EVENTS = [
23
23
  const InternalProductStatus = {
24
24
  DRAFT: null,
25
25
  };
26
+ const isDraftStatus = (status) => status === ProductStatus.DRAFT || status === InternalProductStatus.DRAFT;
26
27
  export const buildFindSelector = ({ slugs, tags, includeDrafts = false, includeDeleted = false, productIds, productSelector, queryString, skus, bundleItemProductIds, proxyAssignmentProductIds, }) => {
27
28
  const selector = productSelector ? { ...productSelector } : {};
28
29
  if (productIds && !selector._id) {
@@ -94,7 +95,7 @@ export const configureProductsModule = async (moduleInput) => {
94
95
  return deletedResult.deletedCount;
95
96
  };
96
97
  const publishProduct = async (product) => {
97
- if (product.status === InternalProductStatus.DRAFT) {
98
+ if (isDraftStatus(product.status)) {
98
99
  await Products.updateOne(generateDbFilterById(product._id), {
99
100
  $set: {
100
101
  status: ProductStatus.ACTIVE,
@@ -187,7 +188,7 @@ export const configureProductsModule = async (moduleInput) => {
187
188
  return product.status === ProductStatus.ACTIVE;
188
189
  },
189
190
  isDraft: (product) => {
190
- return product.status === ProductStatus.DRAFT || product.status === InternalProductStatus.DRAFT;
191
+ return isDraftStatus(product.status);
191
192
  },
192
193
  normalizedStatus: (product) => {
193
194
  return product.status === null ? ProductStatus.DRAFT : product.status;
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": "4.8.18",
4
+ "version": "4.8.20",
5
5
  "main": "lib/products-index.js",
6
6
  "types": "lib/products-index.d.ts",
7
7
  "type": "module",
@@ -40,7 +40,7 @@
40
40
  "@unchainedshop/utils": "^4.8.12"
41
41
  },
42
42
  "devDependencies": {
43
- "@types/node": "^25.0.0",
43
+ "@types/node": "^26.2.0",
44
44
  "typescript": "^5.8.3"
45
45
  }
46
46
  }