@medusajs/product 2.10.2-snapshot-20250907131935 → 2.10.2-snapshot-20250912102418
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/dist/migrations/Migration20250910154539.d.ts +6 -0
- package/dist/migrations/Migration20250910154539.d.ts.map +1 -0
- package/dist/migrations/Migration20250910154539.js +25 -0
- package/dist/migrations/Migration20250910154539.js.map +1 -0
- package/dist/migrations/Migration20250911092221.d.ts +6 -0
- package/dist/migrations/Migration20250911092221.d.ts.map +1 -0
- package/dist/migrations/Migration20250911092221.js +14 -0
- package/dist/migrations/Migration20250911092221.js.map +1 -0
- package/dist/models/product-image.d.ts.map +1 -1
- package/dist/models/product-image.js +18 -0
- package/dist/models/product-image.js.map +1 -1
- package/dist/models/product.d.ts.map +1 -1
- package/dist/models/product.js +6 -0
- package/dist/models/product.js.map +1 -1
- package/dist/repositories/product-category.d.ts.map +1 -1
- package/dist/repositories/product-category.js +3 -6
- package/dist/repositories/product-category.js.map +1 -1
- package/dist/repositories/product.d.ts.map +1 -1
- package/dist/repositories/product.js +6 -5
- package/dist/repositories/product.js.map +1 -1
- package/dist/services/product-category.d.ts +9 -2
- package/dist/services/product-category.d.ts.map +1 -1
- package/dist/services/product-category.js +46 -9
- package/dist/services/product-category.js.map +1 -1
- package/dist/services/product-module-service.d.ts +16 -2
- package/dist/services/product-module-service.d.ts.map +1 -1
- package/dist/services/product-module-service.js +175 -143
- package/dist/services/product-module-service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/events.d.ts +0 -168
- package/dist/utils/events.d.ts.map +1 -1
- package/dist/utils/events.js +0 -126
- package/dist/utils/events.js.map +1 -1
- package/package.json +12 -12
@@ -11,13 +11,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
13
13
|
};
|
14
|
-
var _a
|
14
|
+
var _a;
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
16
16
|
const types_1 = require("@medusajs/framework/types");
|
17
17
|
const _models_1 = require("../models");
|
18
18
|
const utils_1 = require("@medusajs/framework/utils");
|
19
|
-
const utils_2 = require("../utils");
|
20
19
|
const joiner_config_1 = require("./../joiner-config");
|
20
|
+
const events_1 = require("../utils/events");
|
21
21
|
class ProductModuleService extends (0, utils_1.MedusaService)({
|
22
22
|
Product: _models_1.Product,
|
23
23
|
ProductCategory: _models_1.ProductCategory,
|
@@ -107,10 +107,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
107
107
|
const productVariantsWithOptions = ProductModuleService.assignOptionsToVariants(data, productOptions);
|
108
108
|
ProductModuleService.checkIfVariantWithOptionsAlreadyExists(productVariantsWithOptions, variants);
|
109
109
|
const createdVariants = await this.productVariantService_.create(productVariantsWithOptions, sharedContext);
|
110
|
-
utils_2.eventBuilders.createdProductVariant({
|
111
|
-
data: createdVariants,
|
112
|
-
sharedContext,
|
113
|
-
});
|
114
110
|
return createdVariants;
|
115
111
|
}
|
116
112
|
async upsertProductVariants(data, sharedContext = {}) {
|
@@ -120,7 +116,7 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
120
116
|
let created = [];
|
121
117
|
let updated = [];
|
122
118
|
if (forCreate.length) {
|
123
|
-
created = await this.
|
119
|
+
created = await this.createProductVariants(forCreate, sharedContext);
|
124
120
|
}
|
125
121
|
if (forUpdate.length) {
|
126
122
|
updated = await this.updateVariants_(forUpdate, sharedContext);
|
@@ -167,21 +163,9 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
167
163
|
if (data.some((d) => !!d.options)) {
|
168
164
|
ProductModuleService.checkIfVariantWithOptionsAlreadyExists(productVariantsWithOptions, allVariants);
|
169
165
|
}
|
170
|
-
const { entities: productVariants
|
166
|
+
const { entities: productVariants } = await this.productVariantService_.upsertWithReplace(productVariantsWithOptions, {
|
171
167
|
relations: ["options"],
|
172
168
|
}, sharedContext);
|
173
|
-
utils_2.eventBuilders.createdProductVariant({
|
174
|
-
data: performedActions.created[_models_1.ProductVariant.name] ?? [],
|
175
|
-
sharedContext,
|
176
|
-
});
|
177
|
-
utils_2.eventBuilders.updatedProductVariant({
|
178
|
-
data: performedActions.updated[_models_1.ProductVariant.name] ?? [],
|
179
|
-
sharedContext,
|
180
|
-
});
|
181
|
-
utils_2.eventBuilders.deletedProductVariant({
|
182
|
-
data: performedActions.deleted[_models_1.ProductVariant.name] ?? [],
|
183
|
-
sharedContext,
|
184
|
-
});
|
185
169
|
return productVariants;
|
186
170
|
}
|
187
171
|
// @ts-expect-error
|
@@ -189,13 +173,14 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
189
173
|
const input = Array.isArray(data) ? data : [data];
|
190
174
|
const tags = await this.productTagService_.create(input, sharedContext);
|
191
175
|
const createdTags = await this.baseRepository_.serialize(tags);
|
192
|
-
utils_2.eventBuilders.createdProductTag({
|
193
|
-
data: createdTags,
|
194
|
-
sharedContext,
|
195
|
-
});
|
196
176
|
return Array.isArray(data) ? createdTags : createdTags[0];
|
197
177
|
}
|
198
178
|
async upsertProductTags(data, sharedContext = {}) {
|
179
|
+
const tags = await this.upsertProductTags_(data, sharedContext);
|
180
|
+
const allTags = await this.baseRepository_.serialize(Array.isArray(data) ? tags : tags[0]);
|
181
|
+
return allTags;
|
182
|
+
}
|
183
|
+
async upsertProductTags_(data, sharedContext = {}) {
|
199
184
|
const input = Array.isArray(data) ? data : [data];
|
200
185
|
const forUpdate = input.filter((tag) => !!tag.id);
|
201
186
|
const forCreate = input.filter((tag) => !tag.id);
|
@@ -203,21 +188,11 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
203
188
|
let updated = [];
|
204
189
|
if (forCreate.length) {
|
205
190
|
created = await this.productTagService_.create(forCreate, sharedContext);
|
206
|
-
utils_2.eventBuilders.createdProductTag({
|
207
|
-
data: created,
|
208
|
-
sharedContext,
|
209
|
-
});
|
210
191
|
}
|
211
192
|
if (forUpdate.length) {
|
212
193
|
updated = await this.productTagService_.update(forUpdate, sharedContext);
|
213
|
-
utils_2.eventBuilders.updatedProductTag({
|
214
|
-
data: updated,
|
215
|
-
sharedContext,
|
216
|
-
});
|
217
194
|
}
|
218
|
-
|
219
|
-
const allTags = await this.baseRepository_.serialize(result);
|
220
|
-
return Array.isArray(data) ? allTags : allTags[0];
|
195
|
+
return [...created, ...updated];
|
221
196
|
}
|
222
197
|
// @ts-expect-error
|
223
198
|
async updateProductTags(idOrSelector, data, sharedContext = {}) {
|
@@ -236,10 +211,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
236
211
|
}
|
237
212
|
const tags = await this.productTagService_.update(normalizedInput, sharedContext);
|
238
213
|
const updatedTags = await this.baseRepository_.serialize(tags);
|
239
|
-
utils_2.eventBuilders.updatedProductTag({
|
240
|
-
data: updatedTags,
|
241
|
-
sharedContext,
|
242
|
-
});
|
243
214
|
return (0, utils_1.isString)(idOrSelector) ? updatedTags[0] : updatedTags;
|
244
215
|
}
|
245
216
|
// @ts-expect-error
|
@@ -250,6 +221,11 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
250
221
|
return Array.isArray(data) ? createdTypes : createdTypes[0];
|
251
222
|
}
|
252
223
|
async upsertProductTypes(data, sharedContext = {}) {
|
224
|
+
const types = await this.upsertProductTypes_(data, sharedContext);
|
225
|
+
const result = await this.baseRepository_.serialize(types);
|
226
|
+
return Array.isArray(data) ? result : result[0];
|
227
|
+
}
|
228
|
+
async upsertProductTypes_(data, sharedContext) {
|
253
229
|
const input = Array.isArray(data) ? data : [data];
|
254
230
|
const forUpdate = input.filter((type) => !!type.id);
|
255
231
|
const forCreate = input.filter((type) => !type.id);
|
@@ -261,9 +237,7 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
261
237
|
if (forUpdate.length) {
|
262
238
|
updated = await this.productTypeService_.update(forUpdate, sharedContext);
|
263
239
|
}
|
264
|
-
|
265
|
-
const allTypes = await this.baseRepository_.serialize(result);
|
266
|
-
return Array.isArray(data) ? allTypes : allTypes[0];
|
240
|
+
return [...created, ...updated];
|
267
241
|
}
|
268
242
|
// @ts-expect-error
|
269
243
|
async updateProductTypes(idOrSelector, data, sharedContext = {}) {
|
@@ -384,10 +358,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
384
358
|
const input = Array.isArray(data) ? data : [data];
|
385
359
|
const collections = await this.createCollections_(input, sharedContext);
|
386
360
|
const createdCollections = await this.baseRepository_.serialize(collections);
|
387
|
-
utils_2.eventBuilders.createdProductCollection({
|
388
|
-
data: collections,
|
389
|
-
sharedContext,
|
390
|
-
});
|
391
361
|
return Array.isArray(data) ? createdCollections : createdCollections[0];
|
392
362
|
}
|
393
363
|
async createCollections_(data, sharedContext = {}) {
|
@@ -398,6 +368,13 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
398
368
|
return productCollections;
|
399
369
|
}
|
400
370
|
async upsertProductCollections(data, sharedContext = {}) {
|
371
|
+
const collections = await this.upsertCollections_(data, sharedContext);
|
372
|
+
const serializedCollections = await this.baseRepository_.serialize(collections);
|
373
|
+
return Array.isArray(data)
|
374
|
+
? serializedCollections
|
375
|
+
: serializedCollections[0];
|
376
|
+
}
|
377
|
+
async upsertCollections_(data, sharedContext = {}) {
|
401
378
|
const input = Array.isArray(data) ? data : [data];
|
402
379
|
const forUpdate = input.filter((collection) => !!collection.id);
|
403
380
|
const forCreate = input.filter((collection) => !collection.id);
|
@@ -409,21 +386,7 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
409
386
|
if (forUpdate.length) {
|
410
387
|
updated = await this.updateCollections_(forUpdate, sharedContext);
|
411
388
|
}
|
412
|
-
|
413
|
-
const allCollections = await this.baseRepository_.serialize(result);
|
414
|
-
if (created.length) {
|
415
|
-
utils_2.eventBuilders.createdProductCollection({
|
416
|
-
data: created,
|
417
|
-
sharedContext,
|
418
|
-
});
|
419
|
-
}
|
420
|
-
if (updated.length) {
|
421
|
-
utils_2.eventBuilders.updatedProductCollection({
|
422
|
-
data: updated,
|
423
|
-
sharedContext,
|
424
|
-
});
|
425
|
-
}
|
426
|
-
return Array.isArray(data) ? allCollections : allCollections[0];
|
389
|
+
return [...created, ...updated];
|
427
390
|
}
|
428
391
|
// @ts-expect-error
|
429
392
|
async updateProductCollections(idOrSelector, data, sharedContext = {}) {
|
@@ -441,10 +404,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
441
404
|
}
|
442
405
|
const collections = await this.updateCollections_(normalizedInput, sharedContext);
|
443
406
|
const updatedCollections = await this.baseRepository_.serialize(collections);
|
444
|
-
utils_2.eventBuilders.updatedProductCollection({
|
445
|
-
data: updatedCollections,
|
446
|
-
sharedContext,
|
447
|
-
});
|
448
407
|
return (0, utils_1.isString)(idOrSelector) ? updatedCollections[0] : updatedCollections;
|
449
408
|
}
|
450
409
|
async updateCollections_(data, sharedContext = {}) {
|
@@ -500,39 +459,65 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
500
459
|
});
|
501
460
|
const categories = await this.productCategoryService_.create(input, sharedContext);
|
502
461
|
const createdCategories = await this.baseRepository_.serialize(categories);
|
503
|
-
|
462
|
+
// TODO: Same as the update categories, for some reason I cant get the tree repository update
|
463
|
+
events_1.eventBuilders.createdProductCategory({
|
504
464
|
data: createdCategories,
|
505
465
|
sharedContext,
|
506
466
|
});
|
507
467
|
return Array.isArray(data) ? createdCategories : createdCategories[0];
|
508
468
|
}
|
509
469
|
async upsertProductCategories(data, sharedContext = {}) {
|
470
|
+
const categories = await this.upsertProductCategories_(data, sharedContext);
|
471
|
+
const serializedCategories = await this.baseRepository_.serialize(categories);
|
472
|
+
return Array.isArray(data) ? serializedCategories : serializedCategories[0];
|
473
|
+
}
|
474
|
+
async upsertProductCategories_(data, sharedContext = {}) {
|
510
475
|
const input = Array.isArray(data) ? data : [data];
|
511
476
|
const forUpdate = input.filter((category) => !!category.id);
|
512
|
-
|
477
|
+
let forCreate = input.filter((category) => !category.id);
|
513
478
|
let created = [];
|
514
479
|
let updated = [];
|
515
480
|
if (forCreate.length) {
|
481
|
+
forCreate = forCreate.map((productCategory) => {
|
482
|
+
productCategory.handle ??= (0, utils_1.kebabCase)(productCategory.name);
|
483
|
+
return productCategory;
|
484
|
+
});
|
516
485
|
created = await this.productCategoryService_.create(forCreate, sharedContext);
|
517
486
|
}
|
518
487
|
if (forUpdate.length) {
|
519
488
|
updated = await this.productCategoryService_.update(forUpdate, sharedContext);
|
520
489
|
}
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
490
|
+
// TODO: Same as the update categories, for some reason I cant get the tree repository update
|
491
|
+
// event. I ll need to investigate this
|
492
|
+
if (created.length) {
|
493
|
+
events_1.eventBuilders.createdProductCategory({
|
494
|
+
data: created,
|
495
|
+
sharedContext,
|
496
|
+
});
|
497
|
+
}
|
498
|
+
if (updated.length) {
|
499
|
+
events_1.eventBuilders.updatedProductCategory({
|
500
|
+
data: updated,
|
501
|
+
sharedContext,
|
502
|
+
});
|
503
|
+
}
|
504
|
+
return [...created, ...updated];
|
533
505
|
}
|
534
506
|
// @ts-expect-error
|
535
507
|
async updateProductCategories(idOrSelector, data, sharedContext = {}) {
|
508
|
+
const categories = await this.updateProductCategories_(idOrSelector, data, sharedContext);
|
509
|
+
const serializedCategories = await this.baseRepository_.serialize(categories);
|
510
|
+
// TODO: for some reason I cant get the tree repository update
|
511
|
+
// event. I ll need to investigate this
|
512
|
+
events_1.eventBuilders.updatedProductCategory({
|
513
|
+
data: serializedCategories,
|
514
|
+
sharedContext,
|
515
|
+
});
|
516
|
+
return (0, utils_1.isString)(idOrSelector)
|
517
|
+
? serializedCategories[0]
|
518
|
+
: serializedCategories;
|
519
|
+
}
|
520
|
+
async updateProductCategories_(idOrSelector, data, sharedContext = {}) {
|
536
521
|
let normalizedInput = [];
|
537
522
|
if ((0, utils_1.isString)(idOrSelector)) {
|
538
523
|
// Check if the type exists in the first place
|
@@ -547,22 +532,13 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
547
532
|
}));
|
548
533
|
}
|
549
534
|
const categories = await this.productCategoryService_.update(normalizedInput, sharedContext);
|
550
|
-
|
551
|
-
utils_2.eventBuilders.updatedProductCategory({
|
552
|
-
data: updatedCategories,
|
553
|
-
sharedContext,
|
554
|
-
});
|
555
|
-
return (0, utils_1.isString)(idOrSelector) ? updatedCategories[0] : updatedCategories;
|
535
|
+
return categories;
|
556
536
|
}
|
557
537
|
// @ts-expect-error
|
558
538
|
async createProducts(data, sharedContext = {}) {
|
559
539
|
const input = Array.isArray(data) ? data : [data];
|
560
540
|
const products = await this.createProducts_(input, sharedContext);
|
561
541
|
const createdProducts = await this.baseRepository_.serialize(products);
|
562
|
-
utils_2.eventBuilders.createdProduct({
|
563
|
-
data: createdProducts,
|
564
|
-
sharedContext,
|
565
|
-
});
|
566
542
|
return Array.isArray(data) ? createdProducts : createdProducts[0];
|
567
543
|
}
|
568
544
|
async upsertProducts(data, sharedContext = {}) {
|
@@ -572,25 +548,13 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
572
548
|
let created = [];
|
573
549
|
let updated = [];
|
574
550
|
if (forCreate.length) {
|
575
|
-
created = await this.
|
551
|
+
created = await this.createProducts(forCreate, sharedContext);
|
576
552
|
}
|
577
553
|
if (forUpdate.length) {
|
578
554
|
updated = await this.updateProducts_(forUpdate, sharedContext);
|
579
555
|
}
|
580
556
|
const result = [...created, ...updated];
|
581
557
|
const allProducts = await this.baseRepository_.serialize(result);
|
582
|
-
if (created.length) {
|
583
|
-
utils_2.eventBuilders.createdProduct({
|
584
|
-
data: created,
|
585
|
-
sharedContext,
|
586
|
-
});
|
587
|
-
}
|
588
|
-
if (updated.length) {
|
589
|
-
utils_2.eventBuilders.updatedProduct({
|
590
|
-
data: updated,
|
591
|
-
sharedContext,
|
592
|
-
});
|
593
|
-
}
|
594
558
|
return Array.isArray(data) ? allProducts : allProducts[0];
|
595
559
|
}
|
596
560
|
// @ts-expect-error
|
@@ -610,10 +574,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
610
574
|
}
|
611
575
|
const products = await this.updateProducts_(normalizedInput, sharedContext);
|
612
576
|
const updatedProducts = await this.baseRepository_.serialize(products);
|
613
|
-
utils_2.eventBuilders.updatedProduct({
|
614
|
-
data: updatedProducts,
|
615
|
-
sharedContext,
|
616
|
-
});
|
617
577
|
return (0, utils_1.isString)(idOrSelector) ? updatedProducts[0] : updatedProducts;
|
618
578
|
}
|
619
579
|
async createProducts_(data, sharedContext = {}) {
|
@@ -669,14 +629,34 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
669
629
|
return createdProducts;
|
670
630
|
}
|
671
631
|
async updateProducts_(data, sharedContext = {}) {
|
672
|
-
|
632
|
+
// We have to do that manually because this method is bypassing the product service and goes
|
633
|
+
// directly to the custom product repository
|
634
|
+
const manager = (sharedContext.transactionManager ??
|
635
|
+
sharedContext.manager);
|
636
|
+
const subscriber = (0, utils_1.createMedusaMikroOrmEventSubscriber)(["updateProducts_"], this);
|
637
|
+
if (manager && subscriber) {
|
638
|
+
manager
|
639
|
+
.getEventManager()
|
640
|
+
.registerSubscriber(new subscriber(sharedContext));
|
641
|
+
}
|
642
|
+
const originalProducts = await this.productService_.list({
|
643
|
+
id: data.map((d) => d.id),
|
644
|
+
}, {
|
645
|
+
relations: ["options", "options.values", "variants", "images", "tags"],
|
646
|
+
}, sharedContext);
|
647
|
+
const normalizedProducts = await this.normalizeUpdateProductInput(data, originalProducts);
|
673
648
|
for (const product of normalizedProducts) {
|
674
649
|
this.validateProductUpdatePayload(product);
|
675
650
|
}
|
676
|
-
|
651
|
+
const updatedProducts = await this.productRepository_.deepUpdate(normalizedProducts, ProductModuleService.validateVariantOptions, sharedContext);
|
652
|
+
return updatedProducts;
|
677
653
|
}
|
678
654
|
// @ts-expect-error
|
679
655
|
async updateProductOptionValues(idOrSelector, data, sharedContext = {}) {
|
656
|
+
// TODO: There is a missmatch in the API which lead to function with different number of
|
657
|
+
// arguments. Therefore, applying the MedusaContext() decorator to the function will not work
|
658
|
+
// because the context arg index will differ from method to method.
|
659
|
+
sharedContext.messageAggregator ??= new utils_1.MessageAggregator();
|
680
660
|
let normalizedInput = [];
|
681
661
|
if ((0, utils_1.isString)(idOrSelector)) {
|
682
662
|
// This will throw if the product option value does not exist
|
@@ -690,16 +670,29 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
690
670
|
...data,
|
691
671
|
}));
|
692
672
|
}
|
693
|
-
const productOptionValues = await
|
673
|
+
const productOptionValues = await this.updateProductOptionValues_(normalizedInput, sharedContext);
|
694
674
|
const updatedProductOptionValues = await this.baseRepository_.serialize(productOptionValues);
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
675
|
+
// TODO: Because of the wrong method override, we have to compensate to prevent breaking
|
676
|
+
// changes right now
|
677
|
+
const groupedEvents = sharedContext.messageAggregator.getMessages();
|
678
|
+
if (Object.values(groupedEvents).flat().length > 0 &&
|
679
|
+
this.eventBusModuleService_) {
|
680
|
+
const promises = [];
|
681
|
+
for (const group of Object.keys(groupedEvents)) {
|
682
|
+
promises.push(this.eventBusModuleService_.emit(groupedEvents[group], {
|
683
|
+
internal: true,
|
684
|
+
}));
|
685
|
+
}
|
686
|
+
await Promise.all(promises);
|
687
|
+
sharedContext.messageAggregator.clearMessages();
|
688
|
+
}
|
699
689
|
return (0, utils_1.isString)(idOrSelector)
|
700
690
|
? updatedProductOptionValues[0]
|
701
691
|
: updatedProductOptionValues;
|
702
692
|
}
|
693
|
+
async updateProductOptionValues_(normalizedInput, sharedContext = {}) {
|
694
|
+
return await this.productOptionValueService_.update(normalizedInput, sharedContext);
|
695
|
+
}
|
703
696
|
/**
|
704
697
|
* Validates the manually provided handle value of the product
|
705
698
|
* to be URL-safe
|
@@ -734,7 +727,7 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
734
727
|
}
|
735
728
|
async normalizeCreateProductInput(products, sharedContext = {}) {
|
736
729
|
const products_ = Array.isArray(products) ? products : [products];
|
737
|
-
const normalizedProducts = (await this.normalizeUpdateProductInput(products_
|
730
|
+
const normalizedProducts = (await this.normalizeUpdateProductInput(products_));
|
738
731
|
for (const productData of normalizedProducts) {
|
739
732
|
if (!productData.handle && productData.title) {
|
740
733
|
productData.handle = (0, utils_1.toHandle)(productData.title);
|
@@ -745,20 +738,6 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
745
738
|
if (!productData.thumbnail && productData.images?.length) {
|
746
739
|
productData.thumbnail = productData.images[0].url;
|
747
740
|
}
|
748
|
-
// TODO: these props are typed as number, the model expect a string, the API expect number etc
|
749
|
-
// There is some inconsistency here, we should fix it
|
750
|
-
if ("weight" in productData) {
|
751
|
-
productData.weight = productData.weight?.toString();
|
752
|
-
}
|
753
|
-
if ("length" in productData) {
|
754
|
-
productData.length = productData.length?.toString();
|
755
|
-
}
|
756
|
-
if ("height" in productData) {
|
757
|
-
productData.height = productData.height?.toString();
|
758
|
-
}
|
759
|
-
if ("width" in productData) {
|
760
|
-
productData.width = productData.width?.toString();
|
761
|
-
}
|
762
741
|
if (productData.images?.length) {
|
763
742
|
productData.images = productData.images.map((image, index) => image.rank != null
|
764
743
|
? image
|
@@ -770,12 +749,23 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
770
749
|
}
|
771
750
|
return (Array.isArray(products) ? normalizedProducts : normalizedProducts[0]);
|
772
751
|
}
|
773
|
-
|
752
|
+
/**
|
753
|
+
* Normalizes the input for the update product input
|
754
|
+
* @param products - The products to normalize
|
755
|
+
* @param originalProducts - The original products to use for the normalization (must include options and option values relations)
|
756
|
+
* @returns The normalized products
|
757
|
+
*/
|
758
|
+
async normalizeUpdateProductInput(products, originalProducts) {
|
774
759
|
const products_ = Array.isArray(products) ? products : [products];
|
775
760
|
const productsIds = products_.map((p) => p.id).filter(Boolean);
|
776
761
|
let dbOptions = [];
|
777
762
|
if (productsIds.length) {
|
778
|
-
|
763
|
+
// Re map options to handle non serialized data as well
|
764
|
+
dbOptions =
|
765
|
+
originalProducts
|
766
|
+
?.map((originalProduct) => originalProduct.options.map((option) => option))
|
767
|
+
.flat()
|
768
|
+
.filter(Boolean) ?? [];
|
779
769
|
}
|
780
770
|
const normalizedProducts = [];
|
781
771
|
for (const product of products_) {
|
@@ -786,7 +776,8 @@ class ProductModuleService extends (0, utils_1.MedusaService)({
|
|
786
776
|
if (productData.options?.length) {
|
787
777
|
;
|
788
778
|
productData.options = productData.options?.map((option) => {
|
789
|
-
const dbOption = dbOptions.find((o) => o.title === option.title
|
779
|
+
const dbOption = dbOptions.find((o) => (o.title === option.title || o.id === option.id) &&
|
780
|
+
o.product_id === productData.id);
|
790
781
|
return {
|
791
782
|
title: option.title,
|
792
783
|
values: option.values?.map((value) => {
|
@@ -997,13 +988,20 @@ __decorate([
|
|
997
988
|
__metadata("design:returntype", Promise)
|
998
989
|
], ProductModuleService.prototype, "createProductTags", null);
|
999
990
|
__decorate([
|
1000
|
-
(0, utils_1.
|
991
|
+
(0, utils_1.InjectManager)(),
|
1001
992
|
(0, utils_1.EmitEvents)(),
|
1002
993
|
__param(1, (0, utils_1.MedusaContext)()),
|
1003
994
|
__metadata("design:type", Function),
|
1004
995
|
__metadata("design:paramtypes", [Object, Object]),
|
1005
996
|
__metadata("design:returntype", Promise)
|
1006
997
|
], ProductModuleService.prototype, "upsertProductTags", null);
|
998
|
+
__decorate([
|
999
|
+
(0, utils_1.InjectTransactionManager)(),
|
1000
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
1001
|
+
__metadata("design:type", Function),
|
1002
|
+
__metadata("design:paramtypes", [Object, Object]),
|
1003
|
+
__metadata("design:returntype", Promise)
|
1004
|
+
], ProductModuleService.prototype, "upsertProductTags_", null);
|
1007
1005
|
__decorate([
|
1008
1006
|
(0, utils_1.InjectManager)(),
|
1009
1007
|
(0, utils_1.EmitEvents)()
|
@@ -1015,7 +1013,8 @@ __decorate([
|
|
1015
1013
|
__metadata("design:returntype", Promise)
|
1016
1014
|
], ProductModuleService.prototype, "updateProductTags", null);
|
1017
1015
|
__decorate([
|
1018
|
-
(0, utils_1.InjectManager)()
|
1016
|
+
(0, utils_1.InjectManager)(),
|
1017
|
+
(0, utils_1.EmitEvents)()
|
1019
1018
|
// @ts-expect-error
|
1020
1019
|
,
|
1021
1020
|
__param(1, (0, utils_1.MedusaContext)()),
|
@@ -1024,14 +1023,22 @@ __decorate([
|
|
1024
1023
|
__metadata("design:returntype", Promise)
|
1025
1024
|
], ProductModuleService.prototype, "createProductTypes", null);
|
1026
1025
|
__decorate([
|
1027
|
-
(0, utils_1.
|
1026
|
+
(0, utils_1.InjectManager)(),
|
1027
|
+
(0, utils_1.EmitEvents)(),
|
1028
1028
|
__param(1, (0, utils_1.MedusaContext)()),
|
1029
1029
|
__metadata("design:type", Function),
|
1030
1030
|
__metadata("design:paramtypes", [Object, Object]),
|
1031
1031
|
__metadata("design:returntype", Promise)
|
1032
1032
|
], ProductModuleService.prototype, "upsertProductTypes", null);
|
1033
1033
|
__decorate([
|
1034
|
-
(0, utils_1.
|
1034
|
+
(0, utils_1.InjectTransactionManager)(),
|
1035
|
+
__metadata("design:type", Function),
|
1036
|
+
__metadata("design:paramtypes", [Object, Object]),
|
1037
|
+
__metadata("design:returntype", Promise)
|
1038
|
+
], ProductModuleService.prototype, "upsertProductTypes_", null);
|
1039
|
+
__decorate([
|
1040
|
+
(0, utils_1.InjectManager)(),
|
1041
|
+
(0, utils_1.EmitEvents)()
|
1035
1042
|
// @ts-expect-error
|
1036
1043
|
,
|
1037
1044
|
__param(2, (0, utils_1.MedusaContext)()),
|
@@ -1040,7 +1047,8 @@ __decorate([
|
|
1040
1047
|
__metadata("design:returntype", Promise)
|
1041
1048
|
], ProductModuleService.prototype, "updateProductTypes", null);
|
1042
1049
|
__decorate([
|
1043
|
-
(0, utils_1.InjectManager)()
|
1050
|
+
(0, utils_1.InjectManager)(),
|
1051
|
+
(0, utils_1.EmitEvents)()
|
1044
1052
|
// @ts-expect-error
|
1045
1053
|
,
|
1046
1054
|
__param(1, (0, utils_1.MedusaContext)()),
|
@@ -1057,13 +1065,15 @@ __decorate([
|
|
1057
1065
|
], ProductModuleService.prototype, "createOptions_", null);
|
1058
1066
|
__decorate([
|
1059
1067
|
(0, utils_1.InjectTransactionManager)(),
|
1068
|
+
(0, utils_1.EmitEvents)(),
|
1060
1069
|
__param(1, (0, utils_1.MedusaContext)()),
|
1061
1070
|
__metadata("design:type", Function),
|
1062
1071
|
__metadata("design:paramtypes", [Object, Object]),
|
1063
1072
|
__metadata("design:returntype", Promise)
|
1064
1073
|
], ProductModuleService.prototype, "upsertProductOptions", null);
|
1065
1074
|
__decorate([
|
1066
|
-
(0, utils_1.InjectManager)()
|
1075
|
+
(0, utils_1.InjectManager)(),
|
1076
|
+
(0, utils_1.EmitEvents)()
|
1067
1077
|
// @ts-expect-error
|
1068
1078
|
,
|
1069
1079
|
__param(2, (0, utils_1.MedusaContext)()),
|
@@ -1096,13 +1106,20 @@ __decorate([
|
|
1096
1106
|
__metadata("design:returntype", Promise)
|
1097
1107
|
], ProductModuleService.prototype, "createCollections_", null);
|
1098
1108
|
__decorate([
|
1099
|
-
(0, utils_1.
|
1109
|
+
(0, utils_1.InjectManager)(),
|
1100
1110
|
(0, utils_1.EmitEvents)(),
|
1101
1111
|
__param(1, (0, utils_1.MedusaContext)()),
|
1102
1112
|
__metadata("design:type", Function),
|
1103
1113
|
__metadata("design:paramtypes", [Object, Object]),
|
1104
1114
|
__metadata("design:returntype", Promise)
|
1105
1115
|
], ProductModuleService.prototype, "upsertProductCollections", null);
|
1116
|
+
__decorate([
|
1117
|
+
(0, utils_1.InjectTransactionManager)(),
|
1118
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
1119
|
+
__metadata("design:type", Function),
|
1120
|
+
__metadata("design:paramtypes", [Object, Object]),
|
1121
|
+
__metadata("design:returntype", Promise)
|
1122
|
+
], ProductModuleService.prototype, "upsertCollections_", null);
|
1106
1123
|
__decorate([
|
1107
1124
|
(0, utils_1.InjectManager)(),
|
1108
1125
|
(0, utils_1.EmitEvents)()
|
@@ -1131,13 +1148,20 @@ __decorate([
|
|
1131
1148
|
__metadata("design:returntype", Promise)
|
1132
1149
|
], ProductModuleService.prototype, "createProductCategories", null);
|
1133
1150
|
__decorate([
|
1134
|
-
(0, utils_1.
|
1151
|
+
(0, utils_1.InjectManager)(),
|
1135
1152
|
(0, utils_1.EmitEvents)(),
|
1136
1153
|
__param(1, (0, utils_1.MedusaContext)()),
|
1137
1154
|
__metadata("design:type", Function),
|
1138
1155
|
__metadata("design:paramtypes", [Object, Object]),
|
1139
1156
|
__metadata("design:returntype", Promise)
|
1140
1157
|
], ProductModuleService.prototype, "upsertProductCategories", null);
|
1158
|
+
__decorate([
|
1159
|
+
(0, utils_1.InjectTransactionManager)(),
|
1160
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
1161
|
+
__metadata("design:type", Function),
|
1162
|
+
__metadata("design:paramtypes", [Object, Object]),
|
1163
|
+
__metadata("design:returntype", Promise)
|
1164
|
+
], ProductModuleService.prototype, "upsertProductCategories_", null);
|
1141
1165
|
__decorate([
|
1142
1166
|
(0, utils_1.InjectManager)(),
|
1143
1167
|
(0, utils_1.EmitEvents)()
|
@@ -1148,6 +1172,13 @@ __decorate([
|
|
1148
1172
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
1149
1173
|
__metadata("design:returntype", Promise)
|
1150
1174
|
], ProductModuleService.prototype, "updateProductCategories", null);
|
1175
|
+
__decorate([
|
1176
|
+
(0, utils_1.InjectTransactionManager)(),
|
1177
|
+
__param(2, (0, utils_1.MedusaContext)()),
|
1178
|
+
__metadata("design:type", Function),
|
1179
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
1180
|
+
__metadata("design:returntype", Promise)
|
1181
|
+
], ProductModuleService.prototype, "updateProductCategories_", null);
|
1151
1182
|
__decorate([
|
1152
1183
|
(0, utils_1.InjectManager)(),
|
1153
1184
|
(0, utils_1.EmitEvents)()
|
@@ -1191,15 +1222,16 @@ __decorate([
|
|
1191
1222
|
__metadata("design:returntype", Promise)
|
1192
1223
|
], ProductModuleService.prototype, "updateProducts_", null);
|
1193
1224
|
__decorate([
|
1225
|
+
(0, utils_1.InjectTransactionManager)(),
|
1194
1226
|
__param(1, (0, utils_1.MedusaContext)()),
|
1195
1227
|
__metadata("design:type", Function),
|
1196
|
-
__metadata("design:paramtypes", [
|
1228
|
+
__metadata("design:paramtypes", [Array, Object]),
|
1197
1229
|
__metadata("design:returntype", Promise)
|
1198
|
-
], ProductModuleService.prototype, "
|
1230
|
+
], ProductModuleService.prototype, "updateProductOptionValues_", null);
|
1199
1231
|
__decorate([
|
1200
1232
|
__param(1, (0, utils_1.MedusaContext)()),
|
1201
1233
|
__metadata("design:type", Function),
|
1202
|
-
__metadata("design:paramtypes", [typeof (
|
1234
|
+
__metadata("design:paramtypes", [typeof (_a = typeof T !== "undefined" && T) === "function" ? _a : Object, Object]),
|
1203
1235
|
__metadata("design:returntype", Promise)
|
1204
|
-
], ProductModuleService.prototype, "
|
1236
|
+
], ProductModuleService.prototype, "normalizeCreateProductInput", null);
|
1205
1237
|
//# sourceMappingURL=product-module-service.js.map
|