@medusajs/product 0.3.10 → 0.3.11
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.
|
@@ -2,7 +2,7 @@ import { Context, DAL, IEventBusModuleService, InternalModuleDeclaration, Module
|
|
|
2
2
|
import { Image, Product, ProductCategory, ProductCollection, ProductOption, ProductOptionValue, ProductTag, ProductType, ProductVariant, ProductVariantOption } from "../models";
|
|
3
3
|
import { ProductCategoryService, ProductCollectionService, ProductOptionService, ProductService, ProductTagService, ProductTypeService } from ".";
|
|
4
4
|
import { ModulesSdkUtils } from "@medusajs/utils";
|
|
5
|
-
import { UpdateCollectionInput, UpdateProductInput, UpdateProductVariantInput } from "../types";
|
|
5
|
+
import { UpdateCollectionInput, UpdateProductInput, UpdateProductVariantInput, UpdateProductOptionInput } from "../types";
|
|
6
6
|
type InjectedDependencies = {
|
|
7
7
|
baseRepository: DAL.RepositoryService;
|
|
8
8
|
productService: ProductService<any>;
|
|
@@ -71,16 +71,27 @@ export default class ProductModuleService<TProduct extends Product = Product, TP
|
|
|
71
71
|
constructor({ baseRepository, productService, productVariantService, productTagService, productCategoryService, productCollectionService, productImageService, productTypeService, productOptionService, productOptionValueService, productVariantOptionService, eventBusModuleService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
|
|
72
72
|
__joinerConfig(): ModuleJoinerConfig;
|
|
73
73
|
createVariants(data: ProductTypes.CreateProductVariantDTO[], sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO[]>;
|
|
74
|
-
|
|
75
|
-
protected
|
|
76
|
-
|
|
74
|
+
createVariants(data: ProductTypes.CreateProductVariantDTO, sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO>;
|
|
75
|
+
protected createVariants_(data: ProductTypes.CreateProductVariantDTO[], sharedContext?: Context): Promise<ProductVariant[]>;
|
|
76
|
+
upsertVariants(data: ProductTypes.UpsertProductVariantDTO[], sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO[]>;
|
|
77
|
+
upsertVariants(data: ProductTypes.UpsertProductVariantDTO, sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO>;
|
|
78
|
+
updateVariants(id: string, data: ProductTypes.UpdateProductVariantDTO, sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO>;
|
|
79
|
+
updateVariants(selector: ProductTypes.FilterableProductVariantProps, data: ProductTypes.UpdateProductVariantDTO, sharedContext?: Context): Promise<ProductTypes.ProductVariantDTO[]>;
|
|
80
|
+
protected updateVariants_(data: UpdateProductVariantInput[], sharedContext?: Context): Promise<TProductVariant[]>;
|
|
81
|
+
protected diffVariants_(data: UpdateProductVariantInput[], productOptions: ProductOption[], sharedContext?: Context): Promise<TProductVariant[]>;
|
|
77
82
|
createTags(data: ProductTypes.CreateProductTagDTO[], sharedContext?: Context): Promise<ProductTypes.ProductTagDTO[]>;
|
|
78
83
|
updateTags(data: ProductTypes.UpdateProductTagDTO[], sharedContext?: Context): Promise<ProductTypes.ProductTagDTO[]>;
|
|
79
84
|
createTypes(data: ProductTypes.CreateProductTypeDTO[], sharedContext?: Context): Promise<ProductTypes.ProductTypeDTO[]>;
|
|
80
85
|
updateTypes(data: ProductTypes.UpdateProductTypeDTO[], sharedContext?: Context): Promise<ProductTypes.ProductTypeDTO[]>;
|
|
81
86
|
createOptions(data: ProductTypes.CreateProductOptionDTO[], sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO[]>;
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
createOptions(data: ProductTypes.CreateProductOptionDTO, sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO>;
|
|
88
|
+
protected createOptions_(data: ProductTypes.CreateProductOptionDTO[], sharedContext?: Context): Promise<ProductOption[]>;
|
|
89
|
+
upsertOptions(data: ProductTypes.UpsertProductOptionDTO[], sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO[]>;
|
|
90
|
+
upsertOptions(data: ProductTypes.UpsertProductOptionDTO, sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO>;
|
|
91
|
+
updateOptions(id: string, data: ProductTypes.UpdateProductOptionDTO, sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO>;
|
|
92
|
+
updateOptions(selector: ProductTypes.FilterableProductOptionProps, data: ProductTypes.UpdateProductOptionDTO, sharedContext?: Context): Promise<ProductTypes.ProductOptionDTO[]>;
|
|
93
|
+
protected updateOptions_(data: UpdateProductOptionInput[], sharedContext?: Context): Promise<ProductOption[]>;
|
|
94
|
+
protected diffOptions_(data: UpdateProductOptionInput[], sharedContext?: Context): Promise<ProductOption[]>;
|
|
84
95
|
createCollections(data: ProductTypes.CreateProductCollectionDTO[], sharedContext?: Context): Promise<ProductTypes.ProductCollectionDTO[]>;
|
|
85
96
|
createCollections(data: ProductTypes.CreateProductCollectionDTO, sharedContext?: Context): Promise<ProductTypes.ProductCollectionDTO>;
|
|
86
97
|
createCollections_(data: ProductTypes.CreateProductCollectionDTO[], sharedContext?: Context): Promise<TProductCollection[]>;
|
|
@@ -88,7 +99,7 @@ export default class ProductModuleService<TProduct extends Product = Product, TP
|
|
|
88
99
|
upsertCollections(data: ProductTypes.UpsertProductCollectionDTO, sharedContext?: Context): Promise<ProductTypes.ProductCollectionDTO>;
|
|
89
100
|
updateCollections(id: string, data: ProductTypes.UpdateProductCollectionDTO, sharedContext?: Context): Promise<ProductTypes.ProductCollectionDTO>;
|
|
90
101
|
updateCollections(selector: ProductTypes.FilterableProductCollectionProps, data: ProductTypes.UpdateProductCollectionDTO, sharedContext?: Context): Promise<ProductTypes.ProductCollectionDTO[]>;
|
|
91
|
-
updateCollections_(data: UpdateCollectionInput[], sharedContext?: Context): Promise<TProductCollection[]>;
|
|
102
|
+
protected updateCollections_(data: UpdateCollectionInput[], sharedContext?: Context): Promise<TProductCollection[]>;
|
|
92
103
|
createCategory(data: ProductTypes.CreateProductCategoryDTO, sharedContext?: Context): Promise<ProductTypes.ProductCategoryDTO>;
|
|
93
104
|
updateCategory(categoryId: string, data: ProductTypes.UpdateProductCategoryDTO, sharedContext?: Context): Promise<ProductTypes.ProductCategoryDTO>;
|
|
94
105
|
deleteCategory(categoryId: string, sharedContext?: Context): Promise<void>;
|
|
@@ -54,22 +54,54 @@ class ProductModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
54
54
|
return joiner_config_1.joinerConfig;
|
|
55
55
|
}
|
|
56
56
|
async createVariants(data, sharedContext = {}) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
const
|
|
67
|
-
|
|
57
|
+
const input = Array.isArray(data) ? data : [data];
|
|
58
|
+
const variants = await this.createVariants_(input, sharedContext);
|
|
59
|
+
const createdVariants = await this.baseRepository_.serialize(variants, { populate: true });
|
|
60
|
+
return Array.isArray(data) ? createdVariants : createdVariants[0];
|
|
61
|
+
}
|
|
62
|
+
async createVariants_(data, sharedContext = {}) {
|
|
63
|
+
if (data.some((v) => !v.product_id)) {
|
|
64
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, "Tried to create variants without specifying a product_id");
|
|
65
|
+
}
|
|
66
|
+
const productOptions = await this.productOptionService_.list({
|
|
67
|
+
product_id: [...new Set(data.map((v) => v.product_id))],
|
|
68
|
+
}, {
|
|
69
|
+
take: null,
|
|
70
|
+
}, sharedContext);
|
|
71
|
+
const productVariantsWithOptions = ProductModuleService.assignOptionsToVariants(data, productOptions);
|
|
72
|
+
return await this.productVariantService_.create(productVariantsWithOptions, sharedContext);
|
|
68
73
|
}
|
|
69
|
-
async
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
74
|
+
async upsertVariants(data, sharedContext = {}) {
|
|
75
|
+
const input = Array.isArray(data) ? data : [data];
|
|
76
|
+
const forUpdate = input.filter((variant) => !!variant.id);
|
|
77
|
+
const forCreate = input.filter((variant) => !variant.id);
|
|
78
|
+
let created = [];
|
|
79
|
+
let updated = [];
|
|
80
|
+
if (forCreate.length) {
|
|
81
|
+
created = await this.createVariants_(forCreate, sharedContext);
|
|
82
|
+
}
|
|
83
|
+
if (forUpdate.length) {
|
|
84
|
+
updated = await this.updateVariants_(forUpdate, sharedContext);
|
|
85
|
+
}
|
|
86
|
+
const result = [...created, ...updated];
|
|
87
|
+
const allVariants = await this.baseRepository_.serialize(result);
|
|
88
|
+
return Array.isArray(data) ? allVariants : allVariants[0];
|
|
89
|
+
}
|
|
90
|
+
async updateVariants(idOrSelector, data, sharedContext = {}) {
|
|
91
|
+
let normalizedInput = [];
|
|
92
|
+
if ((0, utils_1.isString)(idOrSelector)) {
|
|
93
|
+
normalizedInput = [{ id: idOrSelector, ...data }];
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const variants = await this.productVariantService_.list(idOrSelector, {}, sharedContext);
|
|
97
|
+
normalizedInput = variants.map((variant) => ({
|
|
98
|
+
id: variant.id,
|
|
99
|
+
...data,
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
const variants = await this.updateVariants_(normalizedInput, sharedContext);
|
|
103
|
+
const updatedVariants = await this.baseRepository_.serialize(variants);
|
|
104
|
+
return (0, utils_1.isString)(idOrSelector) ? updatedVariants[0] : updatedVariants;
|
|
73
105
|
}
|
|
74
106
|
async updateVariants_(data, sharedContext = {}) {
|
|
75
107
|
// Validation step
|
|
@@ -144,6 +176,15 @@ class ProductModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
144
176
|
});
|
|
145
177
|
}
|
|
146
178
|
async createOptions(data, sharedContext = {}) {
|
|
179
|
+
const input = Array.isArray(data) ? data : [data];
|
|
180
|
+
const options = await this.createOptions_(input, sharedContext);
|
|
181
|
+
const createdOptions = await this.baseRepository_.serialize(options, { populate: true });
|
|
182
|
+
return Array.isArray(data) ? createdOptions : createdOptions[0];
|
|
183
|
+
}
|
|
184
|
+
async createOptions_(data, sharedContext = {}) {
|
|
185
|
+
if (data.some((v) => !v.product_id)) {
|
|
186
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, "Tried to create options without specifying a product_id");
|
|
187
|
+
}
|
|
147
188
|
const normalizedInput = data.map((opt) => {
|
|
148
189
|
return {
|
|
149
190
|
...opt,
|
|
@@ -152,10 +193,42 @@ class ProductModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
152
193
|
}),
|
|
153
194
|
};
|
|
154
195
|
});
|
|
155
|
-
|
|
156
|
-
|
|
196
|
+
return await this.productOptionService_.create(normalizedInput, sharedContext);
|
|
197
|
+
}
|
|
198
|
+
async upsertOptions(data, sharedContext = {}) {
|
|
199
|
+
const input = Array.isArray(data) ? data : [data];
|
|
200
|
+
const forUpdate = input.filter((option) => !!option.id);
|
|
201
|
+
const forCreate = input.filter((option) => !option.id);
|
|
202
|
+
let created = [];
|
|
203
|
+
let updated = [];
|
|
204
|
+
if (forCreate.length) {
|
|
205
|
+
created = await this.createOptions_(forCreate, sharedContext);
|
|
206
|
+
}
|
|
207
|
+
if (forUpdate.length) {
|
|
208
|
+
updated = await this.updateOptions_(forUpdate, sharedContext);
|
|
209
|
+
}
|
|
210
|
+
const result = [...created, ...updated];
|
|
211
|
+
const allOptions = await this.baseRepository_.serialize(result);
|
|
212
|
+
return Array.isArray(data) ? allOptions : allOptions[0];
|
|
213
|
+
}
|
|
214
|
+
async updateOptions(idOrSelector, data, sharedContext = {}) {
|
|
215
|
+
let normalizedInput = [];
|
|
216
|
+
if ((0, utils_1.isString)(idOrSelector)) {
|
|
217
|
+
normalizedInput = [{ id: idOrSelector, ...data }];
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
const options = await this.productOptionService_.list(idOrSelector, {}, sharedContext);
|
|
221
|
+
normalizedInput = options.map((option) => ({
|
|
222
|
+
id: option.id,
|
|
223
|
+
...data,
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
const options = await this.updateOptions_(normalizedInput, sharedContext);
|
|
227
|
+
const updatedOptions = await this.baseRepository_.serialize(options);
|
|
228
|
+
return (0, utils_1.isString)(idOrSelector) ? updatedOptions[0] : updatedOptions;
|
|
157
229
|
}
|
|
158
|
-
async
|
|
230
|
+
async updateOptions_(data, sharedContext = {}) {
|
|
231
|
+
// Validation step
|
|
159
232
|
const normalizedInput = data.map((opt) => {
|
|
160
233
|
return {
|
|
161
234
|
...opt,
|
|
@@ -172,7 +245,7 @@ class ProductModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
172
245
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, "Tried to update options without specifying an ID");
|
|
173
246
|
}
|
|
174
247
|
const productOptions = await this.diffOptions_(normalizedInput, sharedContext);
|
|
175
|
-
return
|
|
248
|
+
return productOptions;
|
|
176
249
|
}
|
|
177
250
|
// TODO: Do validation
|
|
178
251
|
async diffOptions_(data, sharedContext = {}) {
|
|
@@ -507,17 +580,32 @@ class ProductModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
507
580
|
}
|
|
508
581
|
exports.default = ProductModuleService;
|
|
509
582
|
__decorate([
|
|
583
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
|
510
584
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
511
585
|
__metadata("design:type", Function),
|
|
512
|
-
__metadata("design:paramtypes", [
|
|
586
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
513
587
|
__metadata("design:returntype", Promise)
|
|
514
588
|
], ProductModuleService.prototype, "createVariants", null);
|
|
515
589
|
__decorate([
|
|
516
|
-
(0, utils_1.
|
|
590
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
517
591
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
518
592
|
__metadata("design:type", Function),
|
|
519
593
|
__metadata("design:paramtypes", [Array, Object]),
|
|
520
594
|
__metadata("design:returntype", Promise)
|
|
595
|
+
], ProductModuleService.prototype, "createVariants_", null);
|
|
596
|
+
__decorate([
|
|
597
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
598
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
|
599
|
+
__metadata("design:type", Function),
|
|
600
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
601
|
+
__metadata("design:returntype", Promise)
|
|
602
|
+
], ProductModuleService.prototype, "upsertVariants", null);
|
|
603
|
+
__decorate([
|
|
604
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
|
605
|
+
__param(2, (0, utils_1.MedusaContext)()),
|
|
606
|
+
__metadata("design:type", Function),
|
|
607
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
608
|
+
__metadata("design:returntype", Promise)
|
|
521
609
|
], ProductModuleService.prototype, "updateVariants", null);
|
|
522
610
|
__decorate([
|
|
523
611
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
@@ -527,7 +615,7 @@ __decorate([
|
|
|
527
615
|
__metadata("design:returntype", Promise)
|
|
528
616
|
], ProductModuleService.prototype, "updateVariants_", null);
|
|
529
617
|
__decorate([
|
|
530
|
-
(0, utils_1.
|
|
618
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
531
619
|
__param(2, (0, utils_1.MedusaContext)()),
|
|
532
620
|
__metadata("design:type", Function),
|
|
533
621
|
__metadata("design:paramtypes", [Array, Array, Object]),
|
|
@@ -562,10 +650,10 @@ __decorate([
|
|
|
562
650
|
__metadata("design:returntype", Promise)
|
|
563
651
|
], ProductModuleService.prototype, "updateTypes", null);
|
|
564
652
|
__decorate([
|
|
565
|
-
(0, utils_1.
|
|
653
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
|
566
654
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
567
655
|
__metadata("design:type", Function),
|
|
568
|
-
__metadata("design:paramtypes", [
|
|
656
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
569
657
|
__metadata("design:returntype", Promise)
|
|
570
658
|
], ProductModuleService.prototype, "createOptions", null);
|
|
571
659
|
__decorate([
|
|
@@ -574,7 +662,28 @@ __decorate([
|
|
|
574
662
|
__metadata("design:type", Function),
|
|
575
663
|
__metadata("design:paramtypes", [Array, Object]),
|
|
576
664
|
__metadata("design:returntype", Promise)
|
|
665
|
+
], ProductModuleService.prototype, "createOptions_", null);
|
|
666
|
+
__decorate([
|
|
667
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
668
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
|
669
|
+
__metadata("design:type", Function),
|
|
670
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
671
|
+
__metadata("design:returntype", Promise)
|
|
672
|
+
], ProductModuleService.prototype, "upsertOptions", null);
|
|
673
|
+
__decorate([
|
|
674
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
|
675
|
+
__param(2, (0, utils_1.MedusaContext)()),
|
|
676
|
+
__metadata("design:type", Function),
|
|
677
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
678
|
+
__metadata("design:returntype", Promise)
|
|
577
679
|
], ProductModuleService.prototype, "updateOptions", null);
|
|
680
|
+
__decorate([
|
|
681
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
682
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
|
683
|
+
__metadata("design:type", Function),
|
|
684
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
685
|
+
__metadata("design:returntype", Promise)
|
|
686
|
+
], ProductModuleService.prototype, "updateOptions_", null);
|
|
578
687
|
__decorate([
|
|
579
688
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
580
689
|
__param(1, (0, utils_1.MedusaContext)()),
|
package/dist/types/index.d.ts
CHANGED
|
@@ -40,5 +40,9 @@ export type UpdateCollectionInput = ProductTypes.UpdateProductCollectionDTO & {
|
|
|
40
40
|
id: string;
|
|
41
41
|
};
|
|
42
42
|
export type UpdateProductVariantInput = ProductTypes.UpdateProductVariantDTO & {
|
|
43
|
-
|
|
43
|
+
id: string;
|
|
44
|
+
product_id?: string;
|
|
45
|
+
};
|
|
46
|
+
export type UpdateProductOptionInput = ProductTypes.UpdateProductOptionDTO & {
|
|
47
|
+
id: string;
|
|
44
48
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/product",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.11",
|
|
4
4
|
"description": "Medusa Product module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"cross-env": "^5.2.1",
|
|
42
42
|
"faker": "^6.6.6",
|
|
43
43
|
"jest": "^29.6.3",
|
|
44
|
-
"medusa-test-utils": "^1.1.
|
|
44
|
+
"medusa-test-utils": "^1.1.43",
|
|
45
45
|
"pg-god": "^1.0.12",
|
|
46
46
|
"rimraf": "^3.0.2",
|
|
47
47
|
"ts-jest": "^29.1.1",
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"typescript": "^5.1.6"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@medusajs/modules-sdk": "^1.12.
|
|
54
|
-
"@medusajs/types": "^1.11.
|
|
55
|
-
"@medusajs/utils": "^1.11.
|
|
53
|
+
"@medusajs/modules-sdk": "^1.12.10",
|
|
54
|
+
"@medusajs/types": "^1.11.15",
|
|
55
|
+
"@medusajs/utils": "^1.11.8",
|
|
56
56
|
"@mikro-orm/core": "5.9.7",
|
|
57
57
|
"@mikro-orm/migrations": "5.9.7",
|
|
58
58
|
"@mikro-orm/postgresql": "5.9.7",
|