@labdigital/commercetools-mock 2.2.0 → 2.3.0
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/index.cjs +309 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +309 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/product.ts +424 -20
- package/src/services/product.test.ts +798 -7
package/dist/index.cjs
CHANGED
|
@@ -3807,20 +3807,63 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
3807
3807
|
id: draft.productType.id || ""
|
|
3808
3808
|
};
|
|
3809
3809
|
}
|
|
3810
|
+
const categoryReferences = [];
|
|
3811
|
+
draft.categories?.forEach((category) => {
|
|
3812
|
+
if (category) {
|
|
3813
|
+
categoryReferences.push(
|
|
3814
|
+
getReferenceFromResourceIdentifier(
|
|
3815
|
+
category,
|
|
3816
|
+
context.projectKey,
|
|
3817
|
+
this._storage
|
|
3818
|
+
)
|
|
3819
|
+
);
|
|
3820
|
+
} else {
|
|
3821
|
+
throw new CommercetoolsError(
|
|
3822
|
+
{
|
|
3823
|
+
code: "InvalidJsonInput",
|
|
3824
|
+
message: "Request body does not contain valid JSON.",
|
|
3825
|
+
detailedErrorMessage: "categories: JSON object expected."
|
|
3826
|
+
},
|
|
3827
|
+
400
|
|
3828
|
+
);
|
|
3829
|
+
}
|
|
3830
|
+
});
|
|
3831
|
+
let taxCategoryReference = void 0;
|
|
3832
|
+
if (draft.taxCategory) {
|
|
3833
|
+
taxCategoryReference = getReferenceFromResourceIdentifier(
|
|
3834
|
+
draft.taxCategory,
|
|
3835
|
+
context.projectKey,
|
|
3836
|
+
this._storage
|
|
3837
|
+
);
|
|
3838
|
+
}
|
|
3839
|
+
let productStateReference = void 0;
|
|
3840
|
+
if (draft.state) {
|
|
3841
|
+
productStateReference = getReferenceFromResourceIdentifier(
|
|
3842
|
+
draft.state,
|
|
3843
|
+
context.projectKey,
|
|
3844
|
+
this._storage
|
|
3845
|
+
);
|
|
3846
|
+
}
|
|
3810
3847
|
const productData = {
|
|
3811
3848
|
name: draft.name,
|
|
3812
3849
|
slug: draft.slug,
|
|
3813
|
-
|
|
3850
|
+
description: draft.description,
|
|
3851
|
+
categories: categoryReferences,
|
|
3814
3852
|
masterVariant: variantFromDraft(1, draft.masterVariant),
|
|
3815
3853
|
variants: draft.variants?.map(
|
|
3816
3854
|
(variant, index) => variantFromDraft(index + 2, variant)
|
|
3817
3855
|
) ?? [],
|
|
3856
|
+
metaTitle: draft.metaTitle,
|
|
3857
|
+
metaDescription: draft.metaDescription,
|
|
3858
|
+
metaKeywords: draft.metaKeywords,
|
|
3818
3859
|
searchKeywords: draft.searchKeywords ?? {}
|
|
3819
3860
|
};
|
|
3820
3861
|
const resource = {
|
|
3821
3862
|
...getBaseResourceProperties(),
|
|
3822
3863
|
key: draft.key,
|
|
3823
3864
|
productType,
|
|
3865
|
+
taxCategory: taxCategoryReference,
|
|
3866
|
+
state: productStateReference,
|
|
3824
3867
|
masterData: {
|
|
3825
3868
|
current: productData,
|
|
3826
3869
|
staged: productData,
|
|
@@ -4012,7 +4055,7 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4012
4055
|
return resource;
|
|
4013
4056
|
},
|
|
4014
4057
|
addPrice: (context, resource, { variantId, sku, price, staged }) => {
|
|
4015
|
-
const addVariantPrice = (data) => {
|
|
4058
|
+
const addVariantPrice = (data, priceToAdd2) => {
|
|
4016
4059
|
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
4017
4060
|
data,
|
|
4018
4061
|
variantId,
|
|
@@ -4024,9 +4067,9 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4024
4067
|
);
|
|
4025
4068
|
}
|
|
4026
4069
|
if (variant.prices === void 0) {
|
|
4027
|
-
variant.prices = [
|
|
4070
|
+
variant.prices = [priceToAdd2];
|
|
4028
4071
|
} else {
|
|
4029
|
-
variant.prices.push(
|
|
4072
|
+
variant.prices.push(priceToAdd2);
|
|
4030
4073
|
}
|
|
4031
4074
|
if (isMasterVariant) {
|
|
4032
4075
|
data.masterVariant = variant;
|
|
@@ -4034,10 +4077,11 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4034
4077
|
data.variants[variantIndex] = variant;
|
|
4035
4078
|
}
|
|
4036
4079
|
};
|
|
4080
|
+
const priceToAdd = priceFromDraft(price);
|
|
4037
4081
|
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4038
|
-
addVariantPrice(resource.masterData.staged);
|
|
4082
|
+
addVariantPrice(resource.masterData.staged, priceToAdd);
|
|
4039
4083
|
if (!onlyStaged) {
|
|
4040
|
-
addVariantPrice(resource.masterData.current);
|
|
4084
|
+
addVariantPrice(resource.masterData.current, priceToAdd);
|
|
4041
4085
|
}
|
|
4042
4086
|
checkForStagedChanges(resource);
|
|
4043
4087
|
return resource;
|
|
@@ -4118,21 +4162,270 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4118
4162
|
}
|
|
4119
4163
|
checkForStagedChanges(resource);
|
|
4120
4164
|
return resource;
|
|
4165
|
+
},
|
|
4166
|
+
changeName: (context, resource, { name, staged }) => {
|
|
4167
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4168
|
+
resource.masterData.staged.name = name;
|
|
4169
|
+
if (!onlyStaged) {
|
|
4170
|
+
resource.masterData.current.name = name;
|
|
4171
|
+
}
|
|
4172
|
+
checkForStagedChanges(resource);
|
|
4173
|
+
return resource;
|
|
4174
|
+
},
|
|
4175
|
+
changeSlug: (context, resource, { slug, staged }) => {
|
|
4176
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4177
|
+
resource.masterData.staged.slug = slug;
|
|
4178
|
+
if (!onlyStaged) {
|
|
4179
|
+
resource.masterData.current.slug = slug;
|
|
4180
|
+
}
|
|
4181
|
+
checkForStagedChanges(resource);
|
|
4182
|
+
return resource;
|
|
4183
|
+
},
|
|
4184
|
+
setMetaTitle: (context, resource, { metaTitle, staged }) => {
|
|
4185
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4186
|
+
resource.masterData.staged.metaTitle = metaTitle;
|
|
4187
|
+
if (!onlyStaged) {
|
|
4188
|
+
resource.masterData.current.metaTitle = metaTitle;
|
|
4189
|
+
}
|
|
4190
|
+
checkForStagedChanges(resource);
|
|
4191
|
+
return resource;
|
|
4192
|
+
},
|
|
4193
|
+
setMetaDescription: (context, resource, { metaDescription, staged }) => {
|
|
4194
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4195
|
+
resource.masterData.staged.metaDescription = metaDescription;
|
|
4196
|
+
if (!onlyStaged) {
|
|
4197
|
+
resource.masterData.current.metaDescription = metaDescription;
|
|
4198
|
+
}
|
|
4199
|
+
checkForStagedChanges(resource);
|
|
4200
|
+
return resource;
|
|
4201
|
+
},
|
|
4202
|
+
setMetaKeywords: (context, resource, { metaKeywords, staged }) => {
|
|
4203
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4204
|
+
resource.masterData.staged.metaKeywords = metaKeywords;
|
|
4205
|
+
if (!onlyStaged) {
|
|
4206
|
+
resource.masterData.current.metaKeywords = metaKeywords;
|
|
4207
|
+
}
|
|
4208
|
+
checkForStagedChanges(resource);
|
|
4209
|
+
return resource;
|
|
4210
|
+
},
|
|
4211
|
+
addVariant: (context, resource, {
|
|
4212
|
+
sku,
|
|
4213
|
+
key,
|
|
4214
|
+
prices,
|
|
4215
|
+
images,
|
|
4216
|
+
attributes,
|
|
4217
|
+
staged,
|
|
4218
|
+
assets
|
|
4219
|
+
}) => {
|
|
4220
|
+
const variantDraft = {
|
|
4221
|
+
sku,
|
|
4222
|
+
key,
|
|
4223
|
+
prices,
|
|
4224
|
+
images,
|
|
4225
|
+
attributes,
|
|
4226
|
+
assets
|
|
4227
|
+
};
|
|
4228
|
+
const dataStaged = resource.masterData.staged;
|
|
4229
|
+
const allVariants = [
|
|
4230
|
+
dataStaged.masterVariant,
|
|
4231
|
+
...dataStaged.variants ?? []
|
|
4232
|
+
];
|
|
4233
|
+
const maxId = allVariants.reduce(
|
|
4234
|
+
(max, element) => element.id > max ? element.id : max,
|
|
4235
|
+
0
|
|
4236
|
+
);
|
|
4237
|
+
const variant = variantFromDraft(maxId + 1, variantDraft);
|
|
4238
|
+
dataStaged.variants.push(variant);
|
|
4239
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4240
|
+
if (!onlyStaged) {
|
|
4241
|
+
resource.masterData.current.variants.push(variant);
|
|
4242
|
+
}
|
|
4243
|
+
checkForStagedChanges(resource);
|
|
4244
|
+
return resource;
|
|
4245
|
+
},
|
|
4246
|
+
removeVariant: (context, resource, { id, sku, staged }) => {
|
|
4247
|
+
const removeVariant = (data) => {
|
|
4248
|
+
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
4249
|
+
data,
|
|
4250
|
+
id,
|
|
4251
|
+
sku
|
|
4252
|
+
);
|
|
4253
|
+
if (!variant) {
|
|
4254
|
+
throw new Error(
|
|
4255
|
+
`Variant with id ${id} or sku ${sku} not found on product ${resource.id}`
|
|
4256
|
+
);
|
|
4257
|
+
}
|
|
4258
|
+
if (isMasterVariant) {
|
|
4259
|
+
throw new Error(
|
|
4260
|
+
`Can not remove the variant [ID:${id}] for [Product:${resource.id}] since it's the master variant`
|
|
4261
|
+
);
|
|
4262
|
+
}
|
|
4263
|
+
data.variants.splice(variantIndex, 1);
|
|
4264
|
+
};
|
|
4265
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4266
|
+
removeVariant(resource.masterData.staged);
|
|
4267
|
+
if (!onlyStaged) {
|
|
4268
|
+
removeVariant(resource.masterData.current);
|
|
4269
|
+
}
|
|
4270
|
+
checkForStagedChanges(resource);
|
|
4271
|
+
return resource;
|
|
4272
|
+
},
|
|
4273
|
+
changeMasterVariant: (context, resource, { variantId, sku, staged }) => {
|
|
4274
|
+
const setMaster = (data) => {
|
|
4275
|
+
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
4276
|
+
data,
|
|
4277
|
+
variantId,
|
|
4278
|
+
sku
|
|
4279
|
+
);
|
|
4280
|
+
if (!variant) {
|
|
4281
|
+
throw new Error(
|
|
4282
|
+
`Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
|
|
4283
|
+
);
|
|
4284
|
+
}
|
|
4285
|
+
if (!isMasterVariant) {
|
|
4286
|
+
const masterVariantPrev = data.masterVariant;
|
|
4287
|
+
data.masterVariant = variant;
|
|
4288
|
+
data.variants.splice(variantIndex, 1);
|
|
4289
|
+
data.variants.push(masterVariantPrev);
|
|
4290
|
+
}
|
|
4291
|
+
};
|
|
4292
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4293
|
+
setMaster(resource.masterData.staged);
|
|
4294
|
+
if (!onlyStaged) {
|
|
4295
|
+
setMaster(resource.masterData.current);
|
|
4296
|
+
}
|
|
4297
|
+
checkForStagedChanges(resource);
|
|
4298
|
+
return resource;
|
|
4299
|
+
},
|
|
4300
|
+
setTaxCategory: (context, resource, { taxCategory }) => {
|
|
4301
|
+
let taxCategoryReference = void 0;
|
|
4302
|
+
if (taxCategory) {
|
|
4303
|
+
taxCategoryReference = getReferenceFromResourceIdentifier(
|
|
4304
|
+
taxCategory,
|
|
4305
|
+
context.projectKey,
|
|
4306
|
+
this._storage
|
|
4307
|
+
);
|
|
4308
|
+
} else {
|
|
4309
|
+
throw new CommercetoolsError(
|
|
4310
|
+
{
|
|
4311
|
+
code: "InvalidJsonInput",
|
|
4312
|
+
message: "Request body does not contain valid JSON.",
|
|
4313
|
+
detailedErrorMessage: "actions -> taxCategory: Missing required value"
|
|
4314
|
+
},
|
|
4315
|
+
400
|
|
4316
|
+
);
|
|
4317
|
+
}
|
|
4318
|
+
resource.taxCategory = taxCategoryReference;
|
|
4319
|
+
return resource;
|
|
4320
|
+
},
|
|
4321
|
+
addToCategory: (context, resource, { category, staged, orderHint }) => {
|
|
4322
|
+
const addCategory = (data) => {
|
|
4323
|
+
if (category) {
|
|
4324
|
+
data.categories.push(
|
|
4325
|
+
getReferenceFromResourceIdentifier(
|
|
4326
|
+
category,
|
|
4327
|
+
context.projectKey,
|
|
4328
|
+
this._storage
|
|
4329
|
+
)
|
|
4330
|
+
);
|
|
4331
|
+
} else {
|
|
4332
|
+
throw new CommercetoolsError(
|
|
4333
|
+
{
|
|
4334
|
+
code: "InvalidJsonInput",
|
|
4335
|
+
message: "Request body does not contain valid JSON.",
|
|
4336
|
+
detailedErrorMessage: "actions -> category: Missing required value"
|
|
4337
|
+
},
|
|
4338
|
+
400
|
|
4339
|
+
);
|
|
4340
|
+
}
|
|
4341
|
+
};
|
|
4342
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4343
|
+
addCategory(resource.masterData.staged);
|
|
4344
|
+
if (!onlyStaged) {
|
|
4345
|
+
addCategory(resource.masterData.current);
|
|
4346
|
+
}
|
|
4347
|
+
checkForStagedChanges(resource);
|
|
4348
|
+
return resource;
|
|
4349
|
+
},
|
|
4350
|
+
removeFromCategory: (context, resource, { category, staged }) => {
|
|
4351
|
+
const removeCategory = (data) => {
|
|
4352
|
+
if (category) {
|
|
4353
|
+
const resolvedCategory = getReferenceFromResourceIdentifier(
|
|
4354
|
+
category,
|
|
4355
|
+
context.projectKey,
|
|
4356
|
+
this._storage
|
|
4357
|
+
);
|
|
4358
|
+
const foundCategory = data.categories.find(
|
|
4359
|
+
(productCategory) => {
|
|
4360
|
+
if (productCategory.id == resolvedCategory.id) {
|
|
4361
|
+
return productCategory;
|
|
4362
|
+
}
|
|
4363
|
+
return false;
|
|
4364
|
+
}
|
|
4365
|
+
);
|
|
4366
|
+
if (!foundCategory) {
|
|
4367
|
+
throw new CommercetoolsError(
|
|
4368
|
+
{
|
|
4369
|
+
code: "InvalidOperation",
|
|
4370
|
+
message: `Cannot remove from category '${resolvedCategory.id}' because product '${resource.masterData.current.name}' is not in that category.`
|
|
4371
|
+
},
|
|
4372
|
+
400
|
|
4373
|
+
);
|
|
4374
|
+
}
|
|
4375
|
+
data.categories = data.categories.filter(
|
|
4376
|
+
(productCategory) => {
|
|
4377
|
+
if (productCategory.id == resolvedCategory.id) {
|
|
4378
|
+
return false;
|
|
4379
|
+
}
|
|
4380
|
+
return true;
|
|
4381
|
+
}
|
|
4382
|
+
);
|
|
4383
|
+
} else {
|
|
4384
|
+
throw new CommercetoolsError(
|
|
4385
|
+
{
|
|
4386
|
+
code: "InvalidJsonInput",
|
|
4387
|
+
message: "Request body does not contain valid JSON.",
|
|
4388
|
+
detailedErrorMessage: "actions -> category: Missing required value"
|
|
4389
|
+
},
|
|
4390
|
+
400
|
|
4391
|
+
);
|
|
4392
|
+
}
|
|
4393
|
+
};
|
|
4394
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
4395
|
+
removeCategory(resource.masterData.staged);
|
|
4396
|
+
if (!onlyStaged) {
|
|
4397
|
+
removeCategory(resource.masterData.current);
|
|
4398
|
+
}
|
|
4399
|
+
checkForStagedChanges(resource);
|
|
4400
|
+
return resource;
|
|
4401
|
+
},
|
|
4402
|
+
transitionState: (context, resource, { state, force }) => {
|
|
4403
|
+
let productStateReference = void 0;
|
|
4404
|
+
if (state) {
|
|
4405
|
+
productStateReference = getReferenceFromResourceIdentifier(
|
|
4406
|
+
state,
|
|
4407
|
+
context.projectKey,
|
|
4408
|
+
this._storage
|
|
4409
|
+
);
|
|
4410
|
+
resource.state = productStateReference;
|
|
4411
|
+
} else {
|
|
4412
|
+
throw new CommercetoolsError(
|
|
4413
|
+
{
|
|
4414
|
+
code: "InvalidJsonInput",
|
|
4415
|
+
message: "Request body does not contain valid JSON.",
|
|
4416
|
+
detailedErrorMessage: "actions -> state: Missing required value"
|
|
4417
|
+
},
|
|
4418
|
+
400
|
|
4419
|
+
);
|
|
4420
|
+
}
|
|
4421
|
+
return resource;
|
|
4121
4422
|
}
|
|
4122
|
-
// 'changeName': () => {},
|
|
4123
|
-
// 'changeSlug': () => {},
|
|
4124
|
-
// 'addVariant': () => {},
|
|
4125
|
-
// 'removeVariant': () => {},
|
|
4126
|
-
// 'changeMasterVariant': () => {},
|
|
4127
4423
|
// 'setPrices': () => {},
|
|
4128
4424
|
// 'setProductPriceCustomType': () => {},
|
|
4129
4425
|
// 'setProductPriceCustomField': () => {},
|
|
4130
4426
|
// 'setDiscountedPrice': () => {},
|
|
4131
4427
|
// 'setAttributeInAllVariants': () => {},
|
|
4132
|
-
// 'addToCategory': () => {},
|
|
4133
4428
|
// 'setCategoryOrderHint': () => {},
|
|
4134
|
-
// 'removeFromCategory': () => {},
|
|
4135
|
-
// 'setTaxCategory': () => {},
|
|
4136
4429
|
// 'setSku': () => {},
|
|
4137
4430
|
// 'setProductVariantKey': () => {},
|
|
4138
4431
|
// 'setImageLabel': () => {},
|
|
@@ -4147,12 +4440,8 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
4147
4440
|
// 'setAssetCustomType': () => {},
|
|
4148
4441
|
// 'setAssetCustomField': () => {},
|
|
4149
4442
|
// 'setSearchKeywords': () => {},
|
|
4150
|
-
// 'setMetaTitle': () => {},
|
|
4151
|
-
// 'setMetaDescription': () => {},
|
|
4152
|
-
// 'setMetaKeywords': () => {},
|
|
4153
4443
|
// 'revertStagedChanges': () => {},
|
|
4154
4444
|
// 'revertStagedVariantChanges': () => {},
|
|
4155
|
-
// 'transitionState': () => {},
|
|
4156
4445
|
};
|
|
4157
4446
|
};
|
|
4158
4447
|
var checkForStagedChanges = (product) => {
|
|
@@ -4186,6 +4475,7 @@ var getVariant = (productData, variantId, sku) => {
|
|
|
4186
4475
|
var variantFromDraft = (variantId, variant) => ({
|
|
4187
4476
|
id: variantId,
|
|
4188
4477
|
sku: variant?.sku,
|
|
4478
|
+
key: variant?.key,
|
|
4189
4479
|
attributes: variant?.attributes ?? [],
|
|
4190
4480
|
prices: variant?.prices?.map(priceFromDraft),
|
|
4191
4481
|
assets: [],
|
|
@@ -4193,6 +4483,7 @@ var variantFromDraft = (variantId, variant) => ({
|
|
|
4193
4483
|
});
|
|
4194
4484
|
var priceFromDraft = (draft) => ({
|
|
4195
4485
|
id: (0, import_uuid6.v4)(),
|
|
4486
|
+
key: draft.key,
|
|
4196
4487
|
country: draft.country,
|
|
4197
4488
|
value: createTypedMoney(draft.value)
|
|
4198
4489
|
});
|