@unchainedshop/core-products 4.8.29 → 4.8.31

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.
@@ -11,6 +11,7 @@ export type ProductVariation = {
11
11
  options: string[];
12
12
  productId: string;
13
13
  type: string;
14
+ sortKey?: number;
14
15
  } & TimestampFields;
15
16
  export type ProductVariationText = {
16
17
  _id: string;
@@ -6,7 +6,7 @@ export const ProductVariationType = {
6
6
  export const ProductVariationsCollection = async (db) => {
7
7
  const ProductVariations = db.collection('product_variations');
8
8
  const ProductVariationTexts = db.collection('product_variation_texts');
9
- await buildDbIndexes(ProductVariations, [{ index: { productId: 1 } }]);
9
+ await buildDbIndexes(ProductVariations, [{ index: { productId: 1, sortKey: 1 } }]);
10
10
  await buildDbIndexes(ProductVariationTexts, [
11
11
  { index: { productVariationId: 1 } },
12
12
  { index: { locale: 1, productVariationId: 1 } },
@@ -17,7 +17,7 @@ export declare const configureProductVariationsModule: ({ db }: ModuleInput<Reco
17
17
  limit?: number;
18
18
  offset?: number;
19
19
  }, options?: mongodb.FindOptions) => Promise<ProductVariation[]>;
20
- create: ({ type, ...doc }: Omit<ProductVariation, "_id" | "created"> & Pick<Partial<ProductVariation>, "_id" | "created"> & {
20
+ create: ({ type, sortKey, ...doc }: Omit<ProductVariation, "_id" | "created"> & Pick<Partial<ProductVariation>, "_id" | "created"> & {
21
21
  locale?: string;
22
22
  title?: string;
23
23
  }) => Promise<ProductVariation>;
@@ -70,16 +70,22 @@ export const configureProductVariationsModule = async ({ db }) => {
70
70
  const variations = ProductVariations.find(selector, {
71
71
  skip: offset,
72
72
  limit,
73
+ sort: { sortKey: 1 },
73
74
  ...options,
74
75
  });
75
76
  return variations.toArray();
76
77
  },
77
- create: async ({ type, ...doc }) => {
78
+ create: async ({ type, sortKey, ...doc }) => {
79
+ if (sortKey === undefined || sortKey === null) {
80
+ const lastProductVariation = (await ProductVariations.findOne({ productId: doc.productId }, { sort: { sortKey: -1 } })) || { sortKey: 0 };
81
+ sortKey = (lastProductVariation.sortKey || 0) + 1;
82
+ }
78
83
  const { insertedId: productVariationId } = await ProductVariations.insertOne({
79
84
  _id: generateDbObjectId(),
80
85
  created: new Date(),
81
86
  type: ProductVariationType[type],
82
87
  ...doc,
88
+ sortKey,
83
89
  });
84
90
  const productVariation = (await ProductVariations.findOne(generateDbFilterById(productVariationId), {}));
85
91
  await emit('PRODUCT_CREATE_VARIATION', {
@@ -222,7 +222,7 @@ export declare const configureProductsModule: (moduleInput: ModuleInput<Products
222
222
  limit?: number;
223
223
  offset?: number;
224
224
  }, options?: mongodb.FindOptions) => Promise<import("../products-index.ts").ProductVariation[]>;
225
- create: ({ type, ...doc }: Omit<import("../products-index.ts").ProductVariation, "_id" | "created"> & Pick<Partial<import("../products-index.ts").ProductVariation>, "_id" | "created"> & {
225
+ create: ({ type, sortKey, ...doc }: Omit<import("../products-index.ts").ProductVariation, "_id" | "created"> & Pick<Partial<import("../products-index.ts").ProductVariation>, "_id" | "created"> & {
226
226
  locale?: string;
227
227
  title?: string;
228
228
  }) => Promise<import("../products-index.ts").ProductVariation>;
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.29",
4
+ "version": "4.8.31",
5
5
  "main": "lib/products-index.js",
6
6
  "types": "lib/products-index.d.ts",
7
7
  "type": "module",
@@ -41,6 +41,6 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^26.2.0",
44
- "typescript": "^5.8.3"
44
+ "typescript": "^6.0.3"
45
45
  }
46
46
  }
@@ -1,2 +0,0 @@
1
- import type { MigrationRepository } from '@unchainedshop/mongodb';
2
- export default function migrateCommercePricingToMinQuantity(repository: MigrationRepository): void;
@@ -1,54 +0,0 @@
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
- }