@labdigital/commercetools-mock 2.2.0 → 2.4.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 CHANGED
@@ -2760,6 +2760,22 @@ var CategoryRepository = class extends AbstractResourceRepository {
2760
2760
  changeSlug: (context, resource, { slug }) => {
2761
2761
  resource.slug = slug;
2762
2762
  },
2763
+ changeName: (context, resource, { name }) => {
2764
+ resource.name = name;
2765
+ },
2766
+ changeParent: (context, resource, { parent }) => {
2767
+ const category = this._storage.getByResourceIdentifier(
2768
+ context.projectKey,
2769
+ parent
2770
+ );
2771
+ if (!category) {
2772
+ throw new Error("No category found for reference");
2773
+ }
2774
+ resource.parent = {
2775
+ typeId: "category",
2776
+ id: category.id
2777
+ };
2778
+ },
2763
2779
  setKey: (context, resource, { key }) => {
2764
2780
  resource.key = key;
2765
2781
  },
@@ -3807,20 +3823,63 @@ var ProductRepository = class extends AbstractResourceRepository {
3807
3823
  id: draft.productType.id || ""
3808
3824
  };
3809
3825
  }
3826
+ const categoryReferences = [];
3827
+ draft.categories?.forEach((category) => {
3828
+ if (category) {
3829
+ categoryReferences.push(
3830
+ getReferenceFromResourceIdentifier(
3831
+ category,
3832
+ context.projectKey,
3833
+ this._storage
3834
+ )
3835
+ );
3836
+ } else {
3837
+ throw new CommercetoolsError(
3838
+ {
3839
+ code: "InvalidJsonInput",
3840
+ message: "Request body does not contain valid JSON.",
3841
+ detailedErrorMessage: "categories: JSON object expected."
3842
+ },
3843
+ 400
3844
+ );
3845
+ }
3846
+ });
3847
+ let taxCategoryReference = void 0;
3848
+ if (draft.taxCategory) {
3849
+ taxCategoryReference = getReferenceFromResourceIdentifier(
3850
+ draft.taxCategory,
3851
+ context.projectKey,
3852
+ this._storage
3853
+ );
3854
+ }
3855
+ let productStateReference = void 0;
3856
+ if (draft.state) {
3857
+ productStateReference = getReferenceFromResourceIdentifier(
3858
+ draft.state,
3859
+ context.projectKey,
3860
+ this._storage
3861
+ );
3862
+ }
3810
3863
  const productData = {
3811
3864
  name: draft.name,
3812
3865
  slug: draft.slug,
3813
- categories: [],
3866
+ description: draft.description,
3867
+ categories: categoryReferences,
3814
3868
  masterVariant: variantFromDraft(1, draft.masterVariant),
3815
3869
  variants: draft.variants?.map(
3816
3870
  (variant, index) => variantFromDraft(index + 2, variant)
3817
3871
  ) ?? [],
3872
+ metaTitle: draft.metaTitle,
3873
+ metaDescription: draft.metaDescription,
3874
+ metaKeywords: draft.metaKeywords,
3818
3875
  searchKeywords: draft.searchKeywords ?? {}
3819
3876
  };
3820
3877
  const resource = {
3821
3878
  ...getBaseResourceProperties(),
3822
3879
  key: draft.key,
3823
3880
  productType,
3881
+ taxCategory: taxCategoryReference,
3882
+ state: productStateReference,
3824
3883
  masterData: {
3825
3884
  current: productData,
3826
3885
  staged: productData,
@@ -3881,6 +3940,47 @@ var ProductRepository = class extends AbstractResourceRepository {
3881
3940
  checkForStagedChanges(resource);
3882
3941
  return resource;
3883
3942
  },
3943
+ setAttributeInAllVariants: (context, resource, { name, value, staged }) => {
3944
+ const setAttrInAllVariants = (data) => {
3945
+ if (!data.masterVariant.attributes) {
3946
+ data.masterVariant.attributes = [];
3947
+ }
3948
+ const existingAttr = data.masterVariant.attributes?.find(
3949
+ (attr) => attr.name === name
3950
+ );
3951
+ if (existingAttr) {
3952
+ existingAttr.value = value;
3953
+ } else {
3954
+ data.masterVariant.attributes.push({
3955
+ name,
3956
+ value
3957
+ });
3958
+ }
3959
+ data.variants.forEach((variant) => {
3960
+ if (!variant.attributes) {
3961
+ variant.attributes = [];
3962
+ }
3963
+ const existingAttr2 = variant.attributes.find(
3964
+ (attr) => attr.name === name
3965
+ );
3966
+ if (existingAttr2) {
3967
+ existingAttr2.value = value;
3968
+ } else {
3969
+ variant.attributes.push({
3970
+ name,
3971
+ value
3972
+ });
3973
+ }
3974
+ });
3975
+ };
3976
+ const onlyStaged = staged !== void 0 ? staged : true;
3977
+ setAttrInAllVariants(resource.masterData.staged);
3978
+ if (!onlyStaged) {
3979
+ setAttrInAllVariants(resource.masterData.current);
3980
+ }
3981
+ checkForStagedChanges(resource);
3982
+ return resource;
3983
+ },
3884
3984
  setDescription: (context, resource, { description, staged }) => {
3885
3985
  const onlyStaged = staged !== void 0 ? staged : true;
3886
3986
  resource.masterData.staged.description = description;
@@ -4012,7 +4112,7 @@ var ProductRepository = class extends AbstractResourceRepository {
4012
4112
  return resource;
4013
4113
  },
4014
4114
  addPrice: (context, resource, { variantId, sku, price, staged }) => {
4015
- const addVariantPrice = (data) => {
4115
+ const addVariantPrice = (data, priceToAdd2) => {
4016
4116
  const { variant, isMasterVariant, variantIndex } = getVariant(
4017
4117
  data,
4018
4118
  variantId,
@@ -4024,9 +4124,9 @@ var ProductRepository = class extends AbstractResourceRepository {
4024
4124
  );
4025
4125
  }
4026
4126
  if (variant.prices === void 0) {
4027
- variant.prices = [priceFromDraft(price)];
4127
+ variant.prices = [priceToAdd2];
4028
4128
  } else {
4029
- variant.prices.push(priceFromDraft(price));
4129
+ variant.prices.push(priceToAdd2);
4030
4130
  }
4031
4131
  if (isMasterVariant) {
4032
4132
  data.masterVariant = variant;
@@ -4034,10 +4134,11 @@ var ProductRepository = class extends AbstractResourceRepository {
4034
4134
  data.variants[variantIndex] = variant;
4035
4135
  }
4036
4136
  };
4137
+ const priceToAdd = priceFromDraft(price);
4037
4138
  const onlyStaged = staged !== void 0 ? staged : true;
4038
- addVariantPrice(resource.masterData.staged);
4139
+ addVariantPrice(resource.masterData.staged, priceToAdd);
4039
4140
  if (!onlyStaged) {
4040
- addVariantPrice(resource.masterData.current);
4141
+ addVariantPrice(resource.masterData.current, priceToAdd);
4041
4142
  }
4042
4143
  checkForStagedChanges(resource);
4043
4144
  return resource;
@@ -4118,21 +4219,270 @@ var ProductRepository = class extends AbstractResourceRepository {
4118
4219
  }
4119
4220
  checkForStagedChanges(resource);
4120
4221
  return resource;
4222
+ },
4223
+ changeName: (context, resource, { name, staged }) => {
4224
+ const onlyStaged = staged !== void 0 ? staged : true;
4225
+ resource.masterData.staged.name = name;
4226
+ if (!onlyStaged) {
4227
+ resource.masterData.current.name = name;
4228
+ }
4229
+ checkForStagedChanges(resource);
4230
+ return resource;
4231
+ },
4232
+ changeSlug: (context, resource, { slug, staged }) => {
4233
+ const onlyStaged = staged !== void 0 ? staged : true;
4234
+ resource.masterData.staged.slug = slug;
4235
+ if (!onlyStaged) {
4236
+ resource.masterData.current.slug = slug;
4237
+ }
4238
+ checkForStagedChanges(resource);
4239
+ return resource;
4240
+ },
4241
+ setMetaTitle: (context, resource, { metaTitle, staged }) => {
4242
+ const onlyStaged = staged !== void 0 ? staged : true;
4243
+ resource.masterData.staged.metaTitle = metaTitle;
4244
+ if (!onlyStaged) {
4245
+ resource.masterData.current.metaTitle = metaTitle;
4246
+ }
4247
+ checkForStagedChanges(resource);
4248
+ return resource;
4249
+ },
4250
+ setMetaDescription: (context, resource, { metaDescription, staged }) => {
4251
+ const onlyStaged = staged !== void 0 ? staged : true;
4252
+ resource.masterData.staged.metaDescription = metaDescription;
4253
+ if (!onlyStaged) {
4254
+ resource.masterData.current.metaDescription = metaDescription;
4255
+ }
4256
+ checkForStagedChanges(resource);
4257
+ return resource;
4258
+ },
4259
+ setMetaKeywords: (context, resource, { metaKeywords, staged }) => {
4260
+ const onlyStaged = staged !== void 0 ? staged : true;
4261
+ resource.masterData.staged.metaKeywords = metaKeywords;
4262
+ if (!onlyStaged) {
4263
+ resource.masterData.current.metaKeywords = metaKeywords;
4264
+ }
4265
+ checkForStagedChanges(resource);
4266
+ return resource;
4267
+ },
4268
+ addVariant: (context, resource, {
4269
+ sku,
4270
+ key,
4271
+ prices,
4272
+ images,
4273
+ attributes,
4274
+ staged,
4275
+ assets
4276
+ }) => {
4277
+ const variantDraft = {
4278
+ sku,
4279
+ key,
4280
+ prices,
4281
+ images,
4282
+ attributes,
4283
+ assets
4284
+ };
4285
+ const dataStaged = resource.masterData.staged;
4286
+ const allVariants = [
4287
+ dataStaged.masterVariant,
4288
+ ...dataStaged.variants ?? []
4289
+ ];
4290
+ const maxId = allVariants.reduce(
4291
+ (max, element) => element.id > max ? element.id : max,
4292
+ 0
4293
+ );
4294
+ const variant = variantFromDraft(maxId + 1, variantDraft);
4295
+ dataStaged.variants.push(variant);
4296
+ const onlyStaged = staged !== void 0 ? staged : true;
4297
+ if (!onlyStaged) {
4298
+ resource.masterData.current.variants.push(variant);
4299
+ }
4300
+ checkForStagedChanges(resource);
4301
+ return resource;
4302
+ },
4303
+ removeVariant: (context, resource, { id, sku, staged }) => {
4304
+ const removeVariant = (data) => {
4305
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4306
+ data,
4307
+ id,
4308
+ sku
4309
+ );
4310
+ if (!variant) {
4311
+ throw new Error(
4312
+ `Variant with id ${id} or sku ${sku} not found on product ${resource.id}`
4313
+ );
4314
+ }
4315
+ if (isMasterVariant) {
4316
+ throw new Error(
4317
+ `Can not remove the variant [ID:${id}] for [Product:${resource.id}] since it's the master variant`
4318
+ );
4319
+ }
4320
+ data.variants.splice(variantIndex, 1);
4321
+ };
4322
+ const onlyStaged = staged !== void 0 ? staged : true;
4323
+ removeVariant(resource.masterData.staged);
4324
+ if (!onlyStaged) {
4325
+ removeVariant(resource.masterData.current);
4326
+ }
4327
+ checkForStagedChanges(resource);
4328
+ return resource;
4329
+ },
4330
+ changeMasterVariant: (context, resource, { variantId, sku, staged }) => {
4331
+ const setMaster = (data) => {
4332
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4333
+ data,
4334
+ variantId,
4335
+ sku
4336
+ );
4337
+ if (!variant) {
4338
+ throw new Error(
4339
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
4340
+ );
4341
+ }
4342
+ if (!isMasterVariant) {
4343
+ const masterVariantPrev = data.masterVariant;
4344
+ data.masterVariant = variant;
4345
+ data.variants.splice(variantIndex, 1);
4346
+ data.variants.push(masterVariantPrev);
4347
+ }
4348
+ };
4349
+ const onlyStaged = staged !== void 0 ? staged : true;
4350
+ setMaster(resource.masterData.staged);
4351
+ if (!onlyStaged) {
4352
+ setMaster(resource.masterData.current);
4353
+ }
4354
+ checkForStagedChanges(resource);
4355
+ return resource;
4356
+ },
4357
+ setTaxCategory: (context, resource, { taxCategory }) => {
4358
+ let taxCategoryReference = void 0;
4359
+ if (taxCategory) {
4360
+ taxCategoryReference = getReferenceFromResourceIdentifier(
4361
+ taxCategory,
4362
+ context.projectKey,
4363
+ this._storage
4364
+ );
4365
+ } else {
4366
+ throw new CommercetoolsError(
4367
+ {
4368
+ code: "InvalidJsonInput",
4369
+ message: "Request body does not contain valid JSON.",
4370
+ detailedErrorMessage: "actions -> taxCategory: Missing required value"
4371
+ },
4372
+ 400
4373
+ );
4374
+ }
4375
+ resource.taxCategory = taxCategoryReference;
4376
+ return resource;
4377
+ },
4378
+ addToCategory: (context, resource, { category, staged, orderHint }) => {
4379
+ const addCategory = (data) => {
4380
+ if (category) {
4381
+ data.categories.push(
4382
+ getReferenceFromResourceIdentifier(
4383
+ category,
4384
+ context.projectKey,
4385
+ this._storage
4386
+ )
4387
+ );
4388
+ } else {
4389
+ throw new CommercetoolsError(
4390
+ {
4391
+ code: "InvalidJsonInput",
4392
+ message: "Request body does not contain valid JSON.",
4393
+ detailedErrorMessage: "actions -> category: Missing required value"
4394
+ },
4395
+ 400
4396
+ );
4397
+ }
4398
+ };
4399
+ const onlyStaged = staged !== void 0 ? staged : true;
4400
+ addCategory(resource.masterData.staged);
4401
+ if (!onlyStaged) {
4402
+ addCategory(resource.masterData.current);
4403
+ }
4404
+ checkForStagedChanges(resource);
4405
+ return resource;
4406
+ },
4407
+ removeFromCategory: (context, resource, { category, staged }) => {
4408
+ const removeCategory = (data) => {
4409
+ if (category) {
4410
+ const resolvedCategory = getReferenceFromResourceIdentifier(
4411
+ category,
4412
+ context.projectKey,
4413
+ this._storage
4414
+ );
4415
+ const foundCategory = data.categories.find(
4416
+ (productCategory) => {
4417
+ if (productCategory.id == resolvedCategory.id) {
4418
+ return productCategory;
4419
+ }
4420
+ return false;
4421
+ }
4422
+ );
4423
+ if (!foundCategory) {
4424
+ throw new CommercetoolsError(
4425
+ {
4426
+ code: "InvalidOperation",
4427
+ message: `Cannot remove from category '${resolvedCategory.id}' because product '${resource.masterData.current.name}' is not in that category.`
4428
+ },
4429
+ 400
4430
+ );
4431
+ }
4432
+ data.categories = data.categories.filter(
4433
+ (productCategory) => {
4434
+ if (productCategory.id == resolvedCategory.id) {
4435
+ return false;
4436
+ }
4437
+ return true;
4438
+ }
4439
+ );
4440
+ } else {
4441
+ throw new CommercetoolsError(
4442
+ {
4443
+ code: "InvalidJsonInput",
4444
+ message: "Request body does not contain valid JSON.",
4445
+ detailedErrorMessage: "actions -> category: Missing required value"
4446
+ },
4447
+ 400
4448
+ );
4449
+ }
4450
+ };
4451
+ const onlyStaged = staged !== void 0 ? staged : true;
4452
+ removeCategory(resource.masterData.staged);
4453
+ if (!onlyStaged) {
4454
+ removeCategory(resource.masterData.current);
4455
+ }
4456
+ checkForStagedChanges(resource);
4457
+ return resource;
4458
+ },
4459
+ transitionState: (context, resource, { state, force }) => {
4460
+ let productStateReference = void 0;
4461
+ if (state) {
4462
+ productStateReference = getReferenceFromResourceIdentifier(
4463
+ state,
4464
+ context.projectKey,
4465
+ this._storage
4466
+ );
4467
+ resource.state = productStateReference;
4468
+ } else {
4469
+ throw new CommercetoolsError(
4470
+ {
4471
+ code: "InvalidJsonInput",
4472
+ message: "Request body does not contain valid JSON.",
4473
+ detailedErrorMessage: "actions -> state: Missing required value"
4474
+ },
4475
+ 400
4476
+ );
4477
+ }
4478
+ return resource;
4121
4479
  }
4122
- // 'changeName': () => {},
4123
- // 'changeSlug': () => {},
4124
- // 'addVariant': () => {},
4125
- // 'removeVariant': () => {},
4126
- // 'changeMasterVariant': () => {},
4127
4480
  // 'setPrices': () => {},
4128
4481
  // 'setProductPriceCustomType': () => {},
4129
4482
  // 'setProductPriceCustomField': () => {},
4130
4483
  // 'setDiscountedPrice': () => {},
4131
4484
  // 'setAttributeInAllVariants': () => {},
4132
- // 'addToCategory': () => {},
4133
4485
  // 'setCategoryOrderHint': () => {},
4134
- // 'removeFromCategory': () => {},
4135
- // 'setTaxCategory': () => {},
4136
4486
  // 'setSku': () => {},
4137
4487
  // 'setProductVariantKey': () => {},
4138
4488
  // 'setImageLabel': () => {},
@@ -4147,12 +4497,8 @@ var ProductRepository = class extends AbstractResourceRepository {
4147
4497
  // 'setAssetCustomType': () => {},
4148
4498
  // 'setAssetCustomField': () => {},
4149
4499
  // 'setSearchKeywords': () => {},
4150
- // 'setMetaTitle': () => {},
4151
- // 'setMetaDescription': () => {},
4152
- // 'setMetaKeywords': () => {},
4153
4500
  // 'revertStagedChanges': () => {},
4154
4501
  // 'revertStagedVariantChanges': () => {},
4155
- // 'transitionState': () => {},
4156
4502
  };
4157
4503
  };
4158
4504
  var checkForStagedChanges = (product) => {
@@ -4186,6 +4532,7 @@ var getVariant = (productData, variantId, sku) => {
4186
4532
  var variantFromDraft = (variantId, variant) => ({
4187
4533
  id: variantId,
4188
4534
  sku: variant?.sku,
4535
+ key: variant?.key,
4189
4536
  attributes: variant?.attributes ?? [],
4190
4537
  prices: variant?.prices?.map(priceFromDraft),
4191
4538
  assets: [],
@@ -4193,6 +4540,7 @@ var variantFromDraft = (variantId, variant) => ({
4193
4540
  });
4194
4541
  var priceFromDraft = (draft) => ({
4195
4542
  id: (0, import_uuid6.v4)(),
4543
+ key: draft.key,
4196
4544
  country: draft.country,
4197
4545
  value: createTypedMoney(draft.value)
4198
4546
  });