@unchainedshop/core-assortments 4.8.29 → 5.0.0-alpha.10

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/README.md CHANGED
@@ -13,120 +13,27 @@ npm install @unchainedshop/core-assortments
13
13
 
14
14
  ## Usage
15
15
 
16
- ```typescript
17
- import { configureAssortmentsModule } from '@unchainedshop/core-assortments';
16
+ The platform initializes this module as `platform.unchainedAPI.modules.assortments`. For startup and options, see [Platform Configuration](https://docs.unchained.shop/platform-configuration/).
18
17
 
19
- const assortmentsModule = await configureAssortmentsModule({ db });
18
+ ```typescript
19
+ const { assortments } = platform.unchainedAPI.modules;
20
20
 
21
- // Create an assortment
22
- const assortmentId = await assortmentsModule.create({
23
- slugs: ['electronics'],
21
+ const assortment = await assortments.create({
24
22
  isRoot: true,
23
+ isActive: true,
24
+ sequence: 10,
25
+ tags: [],
25
26
  });
26
-
27
- // Find assortments
28
- const assortments = await assortmentsModule.findAssortments({
29
- includeInactive: false,
30
- });
31
-
32
- // Add product to assortment
33
- await assortmentsModule.products.create({
34
- assortmentId,
27
+ await assortments.products.create({
28
+ assortmentId: assortment._id,
35
29
  productId: 'product-123',
36
30
  });
31
+ const roots = await assortments.findAssortments({ includeLeaves: false, includeInactive: false });
37
32
  ```
38
33
 
39
- ## API Overview
40
-
41
- ### Module Configuration
42
-
43
- | Export | Description |
44
- |--------|-------------|
45
- | `configureAssortmentsModule` | Configure and return the assortments module |
46
-
47
- ### Queries
48
-
49
- | Method | Description |
50
- |--------|-------------|
51
- | `findAssortment` | Find assortment by ID or slug |
52
- | `findAssortments` | Find assortments with filtering and pagination |
53
- | `count` | Count assortments matching query |
54
- | `assortmentExists` | Check if assortment exists |
55
- | `findProductIds` | Get all product IDs in an assortment tree |
56
-
57
- ### Mutations
58
-
59
- | Method | Description |
60
- |--------|-------------|
61
- | `create` | Create a new assortment |
62
- | `update` | Update assortment data |
63
- | `delete` | Soft delete an assortment |
64
- | `setBase` | Set base assortment |
65
- | `invalidateCache` | Clear assortment cache |
66
-
67
- ### Submodules
68
-
69
- #### Products (`assortments.products`)
70
- | Method | Description |
71
- |--------|-------------|
72
- | `findProducts` | Find products in assortment |
73
- | `create` | Add product to assortment |
74
- | `delete` | Remove product from assortment |
75
- | `reorder` | Reorder products |
76
-
77
- #### Links (`assortments.links`)
78
- | Method | Description |
79
- |--------|-------------|
80
- | `findLinks` | Find assortment links (parent-child) |
81
- | `create` | Create link between assortments |
82
- | `delete` | Remove link |
83
- | `reorder` | Reorder child assortments |
84
-
85
- #### Media (`assortments.media`)
86
- | Method | Description |
87
- |--------|-------------|
88
- | `findMedia` | Find assortment media |
89
- | `create` | Add media to assortment |
90
- | `delete` | Remove media |
91
- | `reorder` | Reorder media |
92
-
93
- #### Texts (`assortments.texts`)
94
- | Method | Description |
95
- |--------|-------------|
96
- | `findTexts` | Find localized texts |
97
- | `updateTexts` | Update assortment texts |
98
-
99
- #### Filters (`assortments.filters`)
100
- | Method | Description |
101
- |--------|-------------|
102
- | `findFilters` | Find filters for assortment |
103
- | `create` | Add filter to assortment |
104
- | `delete` | Remove filter |
105
- | `reorder` | Reorder filters |
106
-
107
- ### Settings
108
-
109
- | Export | Description |
110
- |--------|-------------|
111
- | `assortmentsSettings` | Access assortment module settings |
112
-
113
- ### Types
114
-
115
- | Export | Description |
116
- |--------|-------------|
117
- | `Assortment` | Assortment document type |
118
- | `AssortmentProduct` | Assortment-product link type |
119
- | `AssortmentLink` | Parent-child link type |
120
- | `AssortmentMedia` | Media attachment type |
121
- | `AssortmentsModule` | Module interface type |
122
-
123
- ## Events
34
+ Product links, child links, media, texts, and filters live in the `products`, `links`, `media`, `texts`, and `filters` submodules. Creation returns the assortment document; use its `_id` when linking products.
124
35
 
125
- | Event | Description |
126
- |-------|-------------|
127
- | `ASSORTMENT_CREATE` | Assortment created |
128
- | `ASSORTMENT_UPDATE` | Assortment updated |
129
- | `ASSORTMENT_REMOVE` | Assortment deleted |
36
+ See the [module guide](https://docs.unchained.shop/platform-configuration/modules/assortments) for caching, tree ordering, options, and events. The [public exports](src/assortments-index.ts) and [module implementation](src/module/configureAssortmentsModule.ts) define the available types and methods.
130
37
 
131
38
  ## License
132
39
 
@@ -18,7 +18,7 @@ export const configureAssortmentFiltersModule = ({ AssortmentFilters, }) => {
18
18
  },
19
19
  findFilterIds: async ({ assortmentId }) => {
20
20
  const filters = AssortmentFilters.find({ assortmentId }, {
21
- sort: { sortKey: 1 },
21
+ sort: { sortKey: 1, _id: 1 },
22
22
  projection: { filterId: 1 },
23
23
  }).map((filter) => filter.filterId);
24
24
  return filters.toArray();
@@ -41,7 +41,7 @@ export const configureAssortmentFiltersModule = ({ AssortmentFilters, }) => {
41
41
  created: new Date(),
42
42
  };
43
43
  if (sortKey === undefined || sortKey === null) {
44
- const lastAssortmentFilter = (await AssortmentFilters.findOne({ assortmentId }, { sort: { sortKey: -1 } })) || { sortKey: 0 };
44
+ const lastAssortmentFilter = (await AssortmentFilters.findOne({ assortmentId }, { sort: { sortKey: -1, _id: -1 } })) || { sortKey: 0 };
45
45
  $setOnInsert.sortKey = lastAssortmentFilter.sortKey + 1;
46
46
  }
47
47
  else {
@@ -31,7 +31,7 @@ export const configureAssortmentLinksModule = ({ AssortmentLinks, invalidateCach
31
31
  return [];
32
32
  const selector = selectors.length > 1 ? { $or: selectors } : selectors[0];
33
33
  const links = AssortmentLinks.find(selector, options || {
34
- sort: { sortKey: 1 },
34
+ sort: { sortKey: 1, _id: 1 },
35
35
  });
36
36
  return links.toArray();
37
37
  },
@@ -41,7 +41,7 @@ export const configureAssortmentLinksModule = ({ AssortmentLinks, invalidateCach
41
41
  resolveAssortmentLinks: async (id) => {
42
42
  return AssortmentLinks.find({ childAssortmentId: id }, {
43
43
  projection: { _id: 1, childAssortmentId: 1, parentAssortmentId: 1 },
44
- sort: { sortKey: 1, parentAssortmentId: 1 },
44
+ sort: { sortKey: 1, parentAssortmentId: 1, _id: 1 },
45
45
  }).toArray();
46
46
  },
47
47
  assortmentId: parentAssortmentId,
@@ -60,7 +60,7 @@ export const configureAssortmentLinksModule = ({ AssortmentLinks, invalidateCach
60
60
  created: new Date(),
61
61
  };
62
62
  if (sortKey === undefined || sortKey === null) {
63
- const lastAssortmentLink = (await AssortmentLinks.findOne({ parentAssortmentId }, { sort: { sortKey: -1 } })) || { sortKey: 0 };
63
+ const lastAssortmentLink = (await AssortmentLinks.findOne({ parentAssortmentId }, { sort: { sortKey: -1, _id: -1 } })) || { sortKey: 0 };
64
64
  $setOnInsert.sortKey = lastAssortmentLink.sortKey + 1;
65
65
  }
66
66
  else {
@@ -54,7 +54,7 @@ export const configureAssortmentMediaModule = async ({ db }) => {
54
54
  const mediaList = AssortmentMedia.find(selector, {
55
55
  skip: offset,
56
56
  limit,
57
- sort: { sortKey: 1 },
57
+ sort: { sortKey: 1, _id: 1 },
58
58
  ...options,
59
59
  });
60
60
  return mediaList.toArray();
@@ -64,7 +64,7 @@ export const configureAssortmentMediaModule = async ({ db }) => {
64
64
  const lastAssortmentMedia = (await AssortmentMedia.findOne({
65
65
  assortmentId: doc.assortmentId,
66
66
  }, {
67
- sort: { sortKey: -1 },
67
+ sort: { sortKey: -1, _id: -1 },
68
68
  })) || { sortKey: 0 };
69
69
  sortKey = lastAssortmentMedia.sortKey + 1;
70
70
  }
@@ -25,7 +25,7 @@ export declare const configureAssortmentProductsModule: ({ AssortmentProducts, i
25
25
  productId: string;
26
26
  assortmentIds: string[];
27
27
  }) => Promise<string[]>;
28
- create: (doc: Omit<AssortmentProduct, "_id" | "created" | "sortKey"> & Partial<Pick<AssortmentProduct, "_id" | "created" | "sortKey">>, options?: {
28
+ create: (doc: Omit<AssortmentProduct, "_id" | "created" | "sortKey" | "tags"> & Partial<Pick<AssortmentProduct, "_id" | "created" | "sortKey" | "tags">>, options?: {
29
29
  skipInvalidation?: boolean;
30
30
  }) => Promise<mongodb.WithId<AssortmentProduct> | null>;
31
31
  delete: (assortmentProductId: string, options?: {
@@ -46,7 +46,7 @@ export const configureAssortmentProductsModule = ({ AssortmentProducts, invalida
46
46
  assortmentId: { $in: assortmentIds },
47
47
  productId: { $ne: productId },
48
48
  }, {
49
- sort: { sortKey: 1 },
49
+ sort: { sortKey: 1, _id: 1 },
50
50
  projection: { productId: 1 },
51
51
  }).toArray();
52
52
  return assortmentProducts.map((product) => product.productId);
@@ -69,7 +69,7 @@ export const configureAssortmentProductsModule = ({ AssortmentProducts, invalida
69
69
  created: new Date(),
70
70
  };
71
71
  if (sortKey === undefined || sortKey === null) {
72
- const lastAssortmentProduct = (await AssortmentProducts.findOne({ assortmentId }, { sort: { sortKey: -1 } })) || { sortKey: 0 };
72
+ const lastAssortmentProduct = (await AssortmentProducts.findOne({ assortmentId }, { sort: { sortKey: -1, _id: -1 } })) || { sortKey: 0 };
73
73
  $setOnInsert.sortKey = lastAssortmentProduct.sortKey + 1;
74
74
  }
75
75
  else {
@@ -52,6 +52,21 @@ export declare const configureAssortmentsModule: (moduleInput: ModuleInput<Assor
52
52
  skipInvalidation?: boolean;
53
53
  }) => Promise<mongodb.WithId<Assortment> | null>;
54
54
  invalidateCache: InvalidateCacheFn;
55
+ bulkDelete: (assortmentIds: string[]) => Promise<{
56
+ successIds: string[];
57
+ failedIds: string[];
58
+ }>;
59
+ bulkSetActive: (assortmentIds: string[], isActive: boolean) => Promise<{
60
+ successIds: string[];
61
+ failedIds: string[];
62
+ }>;
63
+ bulkUpdateTags: (assortmentIds: string[], { add, remove }: {
64
+ add?: string[];
65
+ remove?: string[];
66
+ }) => Promise<{
67
+ successIds: string[];
68
+ failedIds: string[];
69
+ }>;
55
70
  search: {
56
71
  findFilteredAssortments: ({ limit, offset, assortmentIds, assortmentSelector, sort, }: {
57
72
  assortmentIds: string[];
@@ -184,7 +199,7 @@ export declare const configureAssortmentsModule: (moduleInput: ModuleInput<Assor
184
199
  productId: string;
185
200
  assortmentIds: string[];
186
201
  }) => Promise<string[]>;
187
- create: (doc: Omit<AssortmentProduct, "_id" | "created" | "sortKey"> & Partial<Pick<AssortmentProduct, "_id" | "created" | "sortKey">>, options?: {
202
+ create: (doc: Omit<AssortmentProduct, "_id" | "created" | "sortKey" | "tags"> & Partial<Pick<AssortmentProduct, "_id" | "created" | "sortKey" | "tags">>, options?: {
188
203
  skipInvalidation?: boolean;
189
204
  }) => Promise<mongodb.WithId<AssortmentProduct> | null>;
190
205
  delete: (assortmentProductId: string, options?: {
@@ -1,4 +1,4 @@
1
- import { SortDirection } from '@unchainedshop/utils';
1
+ import { executeBulkOperation, SortDirection } from '@unchainedshop/utils';
2
2
  import { emit, registerEvents } from '@unchainedshop/events';
3
3
  import { generateDbFilterById, findPreservingIds, buildSortOptions, mongodb, generateDbObjectId, } from '@unchainedshop/mongodb';
4
4
  import { createLogger } from '@unchainedshop/logger';
@@ -10,7 +10,7 @@ import { configureAssortmentProductsModule } from "./configureAssortmentProducts
10
10
  import { configureAssortmentTextsModule } from "./configureAssortmentTextsModule.js";
11
11
  import { configureAssortmentMediaModule } from "./configureAssortmentMediaModule.js";
12
12
  import { makeAssortmentBreadcrumbsBuilder } from "../utils/breadcrumbs/makeAssortmentBreadcrumbsBuilder.js";
13
- const logger = createLogger('unchained:core');
13
+ const logger = createLogger('unchained:core-assortments');
14
14
  const ASSORTMENT_EVENTS = ['ASSORTMENT_CREATE', 'ASSORTMENT_REMOVE', 'ASSORTMENT_UPDATE'];
15
15
  export const buildFindSelector = ({ assortmentIds, assortmentSelector, slugs, tags, includeLeaves = false, includeInactive = false, queryString, }) => {
16
16
  const selector = assortmentSelector || {};
@@ -49,12 +49,12 @@ export const configureAssortmentsModule = async (moduleInput) => {
49
49
  return AssortmentLinks.find({
50
50
  $or: [{ parentAssortmentId: assortment._id }, { childAssortmentId: assortment._id }],
51
51
  }, {
52
- sort: { sortKey: 1 },
52
+ sort: { sortKey: 1, _id: 1 },
53
53
  }).toArray();
54
54
  };
55
55
  const findProductAssignments = async (assortmentId) => {
56
56
  return AssortmentProducts.find({ assortmentId }, {
57
- sort: { sortKey: 1 },
57
+ sort: { sortKey: 1, _id: 1 },
58
58
  }).toArray();
59
59
  };
60
60
  const collectProductIdCacheTree = async (assortment) => {
@@ -123,6 +123,42 @@ export const configureAssortmentsModule = async (moduleInput) => {
123
123
  AssortmentTexts,
124
124
  });
125
125
  const assortmentMedia = await configureAssortmentMediaModule(moduleInput);
126
+ const updateAssortment = async (assortmentId, doc, options) => {
127
+ const assortment = await Assortments.findOneAndUpdate(generateDbFilterById(assortmentId), {
128
+ $set: {
129
+ updated: new Date(),
130
+ ...doc,
131
+ },
132
+ }, { returnDocument: 'after' });
133
+ if (!assortment)
134
+ return null;
135
+ await emit('ASSORTMENT_UPDATE', { assortmentId });
136
+ if (!options?.skipInvalidation) {
137
+ await invalidateCache({ assortmentIds: [assortmentId] });
138
+ }
139
+ return assortment;
140
+ };
141
+ const deleteAssortment = async (assortmentId, options) => {
142
+ await assortmentLinks.deleteMany({
143
+ $or: [{ parentAssortmentId: assortmentId }, { childAssortmentId: assortmentId }],
144
+ }, { skipInvalidation: true });
145
+ await assortmentProducts.deleteMany({ assortmentId }, { skipInvalidation: true });
146
+ await assortmentFilters.deleteMany({ assortmentId });
147
+ await assortmentTexts.deleteMany({ assortmentId });
148
+ await assortmentMedia.deleteMediaFiles({ assortmentId });
149
+ const deletedAssortment = await Assortments.findOneAndUpdate(generateDbFilterById(assortmentId), {
150
+ $set: {
151
+ deleted: new Date(),
152
+ },
153
+ }, { returnDocument: 'after' });
154
+ if (!deletedAssortment)
155
+ return null;
156
+ if (!options?.skipInvalidation) {
157
+ await invalidateCache({}, { skipUpstreamTraversal: true });
158
+ }
159
+ await emit('ASSORTMENT_REMOVE', { assortmentId });
160
+ return deletedAssortment;
161
+ };
126
162
  return {
127
163
  findAssortment: async ({ assortmentId, slug }) => {
128
164
  let selector = {};
@@ -166,7 +202,7 @@ export const configureAssortmentsModule = async (moduleInput) => {
166
202
  children: async ({ assortmentId, includeInactive, }) => {
167
203
  const links = await AssortmentLinks.find({ parentAssortmentId: assortmentId }, {
168
204
  projection: { childAssortmentId: 1 },
169
- sort: { sortKey: 1 },
205
+ sort: { sortKey: 1, _id: 1 },
170
206
  }).toArray();
171
207
  const assortmentIds = links.map(({ childAssortmentId }) => childAssortmentId);
172
208
  const selector = !includeInactive ? { isActive: true } : {};
@@ -182,12 +218,12 @@ export const configureAssortmentsModule = async (moduleInput) => {
182
218
  breadcrumbs: async (params, { resolveAssortmentLinks = async (id) => {
183
219
  return AssortmentLinks.find({ childAssortmentId: id }, {
184
220
  projection: { _id: 1, childAssortmentId: 1, parentAssortmentId: 1 },
185
- sort: { sortKey: 1, parentAssortmentId: 1 },
221
+ sort: { sortKey: 1, parentAssortmentId: 1, _id: 1 },
186
222
  }).toArray();
187
223
  }, resolveAssortmentProducts = async (id) => {
188
224
  return AssortmentProducts.find({ productId: id }, {
189
225
  projection: { _id: true, assortmentId: true, productId: true },
190
- sort: { sortKey: 1, productId: 1 },
226
+ sort: { sortKey: 1, productId: 1, _id: 1 },
191
227
  }).toArray();
192
228
  }, }) => {
193
229
  const buildBreadcrumbs = makeAssortmentBreadcrumbsBuilder({
@@ -212,43 +248,39 @@ export const configureAssortmentsModule = async (moduleInput) => {
212
248
  await emit('ASSORTMENT_CREATE', { assortment });
213
249
  return assortment;
214
250
  },
215
- update: async (assortmentId, doc, options) => {
216
- const assortment = await Assortments.findOneAndUpdate(generateDbFilterById(assortmentId), {
217
- $set: {
218
- updated: new Date(),
219
- ...doc,
220
- },
221
- }, { returnDocument: 'after' });
222
- if (!assortment)
223
- return null;
224
- await emit('ASSORTMENT_UPDATE', { assortmentId });
225
- if (!options?.skipInvalidation) {
226
- await invalidateCache({ assortmentIds: [assortmentId] });
227
- }
228
- return assortment;
251
+ update: updateAssortment,
252
+ delete: deleteAssortment,
253
+ invalidateCache,
254
+ bulkDelete: async (assortmentIds) => {
255
+ return executeBulkOperation(assortmentIds, async (assortmentId) => {
256
+ const assortment = await Assortments.findOne({ _id: assortmentId, deleted: null }, {});
257
+ if (!assortment)
258
+ throw new Error('not-found');
259
+ if (!(await deleteAssortment(assortmentId))) {
260
+ throw new Error('delete-failed');
261
+ }
262
+ });
229
263
  },
230
- delete: async (assortmentId, options) => {
231
- await assortmentLinks.deleteMany({
232
- $or: [{ parentAssortmentId: assortmentId }, { childAssortmentId: assortmentId }],
233
- }, { skipInvalidation: true });
234
- await assortmentProducts.deleteMany({ assortmentId }, { skipInvalidation: true });
235
- await assortmentFilters.deleteMany({ assortmentId });
236
- await assortmentTexts.deleteMany({ assortmentId });
237
- await assortmentMedia.deleteMediaFiles({ assortmentId });
238
- const deletedAssortment = await Assortments.findOneAndUpdate(generateDbFilterById(assortmentId), {
239
- $set: {
240
- deleted: new Date(),
241
- },
242
- }, { returnDocument: 'after' });
243
- if (!deletedAssortment)
244
- return null;
245
- if (!options?.skipInvalidation) {
246
- await invalidateCache({}, { skipUpstreamTraversal: true });
247
- }
248
- await emit('ASSORTMENT_REMOVE', { assortmentId });
249
- return deletedAssortment;
264
+ bulkSetActive: async (assortmentIds, isActive) => executeBulkOperation(assortmentIds, async (assortmentId) => {
265
+ const assortment = await Assortments.findOne({ _id: assortmentId, deleted: null }, {});
266
+ if (!assortment)
267
+ throw new Error('not-found');
268
+ await updateAssortment(assortmentId, { isActive });
269
+ }),
270
+ bulkUpdateTags: async (assortmentIds, { add = [], remove = [] }) => {
271
+ const tagsToRemove = new Set(remove);
272
+ return executeBulkOperation(assortmentIds, async (assortmentId) => {
273
+ const assortment = await Assortments.findOne({ _id: assortmentId, deleted: null }, {});
274
+ if (!assortment)
275
+ throw new Error('not-found');
276
+ const tags = (assortment.tags || []).filter((tag) => !tagsToRemove.has(tag));
277
+ for (const tag of add) {
278
+ if (!tags.includes(tag))
279
+ tags.push(tag);
280
+ }
281
+ await updateAssortment(assortmentId, { tags });
282
+ });
250
283
  },
251
- invalidateCache,
252
284
  search: {
253
285
  findFilteredAssortments: async ({ limit, offset, assortmentIds, assortmentSelector, sort, }) => {
254
286
  const assortments = await findPreservingIds(Assortments)(assortmentSelector, assortmentIds, {
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-assortments",
3
3
  "description": "Assortment and category management module for the Unchained Engine",
4
- "version": "4.8.29",
4
+ "version": "5.0.0-alpha.10",
5
+ "files": [
6
+ "lib"
7
+ ],
5
8
  "main": "lib/assortments-index.js",
6
9
  "types": "lib/assortments-index.d.ts",
7
10
  "type": "module",
@@ -35,13 +38,13 @@
35
38
  },
36
39
  "homepage": "https://github.com/unchainedshop/unchained#readme",
37
40
  "dependencies": {
38
- "@unchainedshop/events": "^4.8.27",
39
- "@unchainedshop/logger": "^4.8.27",
40
- "@unchainedshop/mongodb": "^4.8.27",
41
- "@unchainedshop/utils": "^4.8.27"
41
+ "@unchainedshop/events": "^5.0.0-alpha.10",
42
+ "@unchainedshop/logger": "^5.0.0-alpha.10",
43
+ "@unchainedshop/mongodb": "^5.0.0-alpha.10",
44
+ "@unchainedshop/utils": "^5.0.0-alpha.10"
42
45
  },
43
46
  "devDependencies": {
44
47
  "@types/node": "^26.2.0",
45
- "typescript": "^5.8.3"
48
+ "typescript": "^6.0.3"
46
49
  }
47
50
  }