@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.js CHANGED
@@ -3770,20 +3770,63 @@ var ProductRepository = class extends AbstractResourceRepository {
3770
3770
  id: draft.productType.id || ""
3771
3771
  };
3772
3772
  }
3773
+ const categoryReferences = [];
3774
+ draft.categories?.forEach((category) => {
3775
+ if (category) {
3776
+ categoryReferences.push(
3777
+ getReferenceFromResourceIdentifier(
3778
+ category,
3779
+ context.projectKey,
3780
+ this._storage
3781
+ )
3782
+ );
3783
+ } else {
3784
+ throw new CommercetoolsError(
3785
+ {
3786
+ code: "InvalidJsonInput",
3787
+ message: "Request body does not contain valid JSON.",
3788
+ detailedErrorMessage: "categories: JSON object expected."
3789
+ },
3790
+ 400
3791
+ );
3792
+ }
3793
+ });
3794
+ let taxCategoryReference = void 0;
3795
+ if (draft.taxCategory) {
3796
+ taxCategoryReference = getReferenceFromResourceIdentifier(
3797
+ draft.taxCategory,
3798
+ context.projectKey,
3799
+ this._storage
3800
+ );
3801
+ }
3802
+ let productStateReference = void 0;
3803
+ if (draft.state) {
3804
+ productStateReference = getReferenceFromResourceIdentifier(
3805
+ draft.state,
3806
+ context.projectKey,
3807
+ this._storage
3808
+ );
3809
+ }
3773
3810
  const productData = {
3774
3811
  name: draft.name,
3775
3812
  slug: draft.slug,
3776
- categories: [],
3813
+ description: draft.description,
3814
+ categories: categoryReferences,
3777
3815
  masterVariant: variantFromDraft(1, draft.masterVariant),
3778
3816
  variants: draft.variants?.map(
3779
3817
  (variant, index) => variantFromDraft(index + 2, variant)
3780
3818
  ) ?? [],
3819
+ metaTitle: draft.metaTitle,
3820
+ metaDescription: draft.metaDescription,
3821
+ metaKeywords: draft.metaKeywords,
3781
3822
  searchKeywords: draft.searchKeywords ?? {}
3782
3823
  };
3783
3824
  const resource = {
3784
3825
  ...getBaseResourceProperties(),
3785
3826
  key: draft.key,
3786
3827
  productType,
3828
+ taxCategory: taxCategoryReference,
3829
+ state: productStateReference,
3787
3830
  masterData: {
3788
3831
  current: productData,
3789
3832
  staged: productData,
@@ -3975,7 +4018,7 @@ var ProductRepository = class extends AbstractResourceRepository {
3975
4018
  return resource;
3976
4019
  },
3977
4020
  addPrice: (context, resource, { variantId, sku, price, staged }) => {
3978
- const addVariantPrice = (data) => {
4021
+ const addVariantPrice = (data, priceToAdd2) => {
3979
4022
  const { variant, isMasterVariant, variantIndex } = getVariant(
3980
4023
  data,
3981
4024
  variantId,
@@ -3987,9 +4030,9 @@ var ProductRepository = class extends AbstractResourceRepository {
3987
4030
  );
3988
4031
  }
3989
4032
  if (variant.prices === void 0) {
3990
- variant.prices = [priceFromDraft(price)];
4033
+ variant.prices = [priceToAdd2];
3991
4034
  } else {
3992
- variant.prices.push(priceFromDraft(price));
4035
+ variant.prices.push(priceToAdd2);
3993
4036
  }
3994
4037
  if (isMasterVariant) {
3995
4038
  data.masterVariant = variant;
@@ -3997,10 +4040,11 @@ var ProductRepository = class extends AbstractResourceRepository {
3997
4040
  data.variants[variantIndex] = variant;
3998
4041
  }
3999
4042
  };
4043
+ const priceToAdd = priceFromDraft(price);
4000
4044
  const onlyStaged = staged !== void 0 ? staged : true;
4001
- addVariantPrice(resource.masterData.staged);
4045
+ addVariantPrice(resource.masterData.staged, priceToAdd);
4002
4046
  if (!onlyStaged) {
4003
- addVariantPrice(resource.masterData.current);
4047
+ addVariantPrice(resource.masterData.current, priceToAdd);
4004
4048
  }
4005
4049
  checkForStagedChanges(resource);
4006
4050
  return resource;
@@ -4081,21 +4125,270 @@ var ProductRepository = class extends AbstractResourceRepository {
4081
4125
  }
4082
4126
  checkForStagedChanges(resource);
4083
4127
  return resource;
4128
+ },
4129
+ changeName: (context, resource, { name, staged }) => {
4130
+ const onlyStaged = staged !== void 0 ? staged : true;
4131
+ resource.masterData.staged.name = name;
4132
+ if (!onlyStaged) {
4133
+ resource.masterData.current.name = name;
4134
+ }
4135
+ checkForStagedChanges(resource);
4136
+ return resource;
4137
+ },
4138
+ changeSlug: (context, resource, { slug, staged }) => {
4139
+ const onlyStaged = staged !== void 0 ? staged : true;
4140
+ resource.masterData.staged.slug = slug;
4141
+ if (!onlyStaged) {
4142
+ resource.masterData.current.slug = slug;
4143
+ }
4144
+ checkForStagedChanges(resource);
4145
+ return resource;
4146
+ },
4147
+ setMetaTitle: (context, resource, { metaTitle, staged }) => {
4148
+ const onlyStaged = staged !== void 0 ? staged : true;
4149
+ resource.masterData.staged.metaTitle = metaTitle;
4150
+ if (!onlyStaged) {
4151
+ resource.masterData.current.metaTitle = metaTitle;
4152
+ }
4153
+ checkForStagedChanges(resource);
4154
+ return resource;
4155
+ },
4156
+ setMetaDescription: (context, resource, { metaDescription, staged }) => {
4157
+ const onlyStaged = staged !== void 0 ? staged : true;
4158
+ resource.masterData.staged.metaDescription = metaDescription;
4159
+ if (!onlyStaged) {
4160
+ resource.masterData.current.metaDescription = metaDescription;
4161
+ }
4162
+ checkForStagedChanges(resource);
4163
+ return resource;
4164
+ },
4165
+ setMetaKeywords: (context, resource, { metaKeywords, staged }) => {
4166
+ const onlyStaged = staged !== void 0 ? staged : true;
4167
+ resource.masterData.staged.metaKeywords = metaKeywords;
4168
+ if (!onlyStaged) {
4169
+ resource.masterData.current.metaKeywords = metaKeywords;
4170
+ }
4171
+ checkForStagedChanges(resource);
4172
+ return resource;
4173
+ },
4174
+ addVariant: (context, resource, {
4175
+ sku,
4176
+ key,
4177
+ prices,
4178
+ images,
4179
+ attributes,
4180
+ staged,
4181
+ assets
4182
+ }) => {
4183
+ const variantDraft = {
4184
+ sku,
4185
+ key,
4186
+ prices,
4187
+ images,
4188
+ attributes,
4189
+ assets
4190
+ };
4191
+ const dataStaged = resource.masterData.staged;
4192
+ const allVariants = [
4193
+ dataStaged.masterVariant,
4194
+ ...dataStaged.variants ?? []
4195
+ ];
4196
+ const maxId = allVariants.reduce(
4197
+ (max, element) => element.id > max ? element.id : max,
4198
+ 0
4199
+ );
4200
+ const variant = variantFromDraft(maxId + 1, variantDraft);
4201
+ dataStaged.variants.push(variant);
4202
+ const onlyStaged = staged !== void 0 ? staged : true;
4203
+ if (!onlyStaged) {
4204
+ resource.masterData.current.variants.push(variant);
4205
+ }
4206
+ checkForStagedChanges(resource);
4207
+ return resource;
4208
+ },
4209
+ removeVariant: (context, resource, { id, sku, staged }) => {
4210
+ const removeVariant = (data) => {
4211
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4212
+ data,
4213
+ id,
4214
+ sku
4215
+ );
4216
+ if (!variant) {
4217
+ throw new Error(
4218
+ `Variant with id ${id} or sku ${sku} not found on product ${resource.id}`
4219
+ );
4220
+ }
4221
+ if (isMasterVariant) {
4222
+ throw new Error(
4223
+ `Can not remove the variant [ID:${id}] for [Product:${resource.id}] since it's the master variant`
4224
+ );
4225
+ }
4226
+ data.variants.splice(variantIndex, 1);
4227
+ };
4228
+ const onlyStaged = staged !== void 0 ? staged : true;
4229
+ removeVariant(resource.masterData.staged);
4230
+ if (!onlyStaged) {
4231
+ removeVariant(resource.masterData.current);
4232
+ }
4233
+ checkForStagedChanges(resource);
4234
+ return resource;
4235
+ },
4236
+ changeMasterVariant: (context, resource, { variantId, sku, staged }) => {
4237
+ const setMaster = (data) => {
4238
+ const { variant, isMasterVariant, variantIndex } = getVariant(
4239
+ data,
4240
+ variantId,
4241
+ sku
4242
+ );
4243
+ if (!variant) {
4244
+ throw new Error(
4245
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
4246
+ );
4247
+ }
4248
+ if (!isMasterVariant) {
4249
+ const masterVariantPrev = data.masterVariant;
4250
+ data.masterVariant = variant;
4251
+ data.variants.splice(variantIndex, 1);
4252
+ data.variants.push(masterVariantPrev);
4253
+ }
4254
+ };
4255
+ const onlyStaged = staged !== void 0 ? staged : true;
4256
+ setMaster(resource.masterData.staged);
4257
+ if (!onlyStaged) {
4258
+ setMaster(resource.masterData.current);
4259
+ }
4260
+ checkForStagedChanges(resource);
4261
+ return resource;
4262
+ },
4263
+ setTaxCategory: (context, resource, { taxCategory }) => {
4264
+ let taxCategoryReference = void 0;
4265
+ if (taxCategory) {
4266
+ taxCategoryReference = getReferenceFromResourceIdentifier(
4267
+ taxCategory,
4268
+ context.projectKey,
4269
+ this._storage
4270
+ );
4271
+ } else {
4272
+ throw new CommercetoolsError(
4273
+ {
4274
+ code: "InvalidJsonInput",
4275
+ message: "Request body does not contain valid JSON.",
4276
+ detailedErrorMessage: "actions -> taxCategory: Missing required value"
4277
+ },
4278
+ 400
4279
+ );
4280
+ }
4281
+ resource.taxCategory = taxCategoryReference;
4282
+ return resource;
4283
+ },
4284
+ addToCategory: (context, resource, { category, staged, orderHint }) => {
4285
+ const addCategory = (data) => {
4286
+ if (category) {
4287
+ data.categories.push(
4288
+ getReferenceFromResourceIdentifier(
4289
+ category,
4290
+ context.projectKey,
4291
+ this._storage
4292
+ )
4293
+ );
4294
+ } else {
4295
+ throw new CommercetoolsError(
4296
+ {
4297
+ code: "InvalidJsonInput",
4298
+ message: "Request body does not contain valid JSON.",
4299
+ detailedErrorMessage: "actions -> category: Missing required value"
4300
+ },
4301
+ 400
4302
+ );
4303
+ }
4304
+ };
4305
+ const onlyStaged = staged !== void 0 ? staged : true;
4306
+ addCategory(resource.masterData.staged);
4307
+ if (!onlyStaged) {
4308
+ addCategory(resource.masterData.current);
4309
+ }
4310
+ checkForStagedChanges(resource);
4311
+ return resource;
4312
+ },
4313
+ removeFromCategory: (context, resource, { category, staged }) => {
4314
+ const removeCategory = (data) => {
4315
+ if (category) {
4316
+ const resolvedCategory = getReferenceFromResourceIdentifier(
4317
+ category,
4318
+ context.projectKey,
4319
+ this._storage
4320
+ );
4321
+ const foundCategory = data.categories.find(
4322
+ (productCategory) => {
4323
+ if (productCategory.id == resolvedCategory.id) {
4324
+ return productCategory;
4325
+ }
4326
+ return false;
4327
+ }
4328
+ );
4329
+ if (!foundCategory) {
4330
+ throw new CommercetoolsError(
4331
+ {
4332
+ code: "InvalidOperation",
4333
+ message: `Cannot remove from category '${resolvedCategory.id}' because product '${resource.masterData.current.name}' is not in that category.`
4334
+ },
4335
+ 400
4336
+ );
4337
+ }
4338
+ data.categories = data.categories.filter(
4339
+ (productCategory) => {
4340
+ if (productCategory.id == resolvedCategory.id) {
4341
+ return false;
4342
+ }
4343
+ return true;
4344
+ }
4345
+ );
4346
+ } else {
4347
+ throw new CommercetoolsError(
4348
+ {
4349
+ code: "InvalidJsonInput",
4350
+ message: "Request body does not contain valid JSON.",
4351
+ detailedErrorMessage: "actions -> category: Missing required value"
4352
+ },
4353
+ 400
4354
+ );
4355
+ }
4356
+ };
4357
+ const onlyStaged = staged !== void 0 ? staged : true;
4358
+ removeCategory(resource.masterData.staged);
4359
+ if (!onlyStaged) {
4360
+ removeCategory(resource.masterData.current);
4361
+ }
4362
+ checkForStagedChanges(resource);
4363
+ return resource;
4364
+ },
4365
+ transitionState: (context, resource, { state, force }) => {
4366
+ let productStateReference = void 0;
4367
+ if (state) {
4368
+ productStateReference = getReferenceFromResourceIdentifier(
4369
+ state,
4370
+ context.projectKey,
4371
+ this._storage
4372
+ );
4373
+ resource.state = productStateReference;
4374
+ } else {
4375
+ throw new CommercetoolsError(
4376
+ {
4377
+ code: "InvalidJsonInput",
4378
+ message: "Request body does not contain valid JSON.",
4379
+ detailedErrorMessage: "actions -> state: Missing required value"
4380
+ },
4381
+ 400
4382
+ );
4383
+ }
4384
+ return resource;
4084
4385
  }
4085
- // 'changeName': () => {},
4086
- // 'changeSlug': () => {},
4087
- // 'addVariant': () => {},
4088
- // 'removeVariant': () => {},
4089
- // 'changeMasterVariant': () => {},
4090
4386
  // 'setPrices': () => {},
4091
4387
  // 'setProductPriceCustomType': () => {},
4092
4388
  // 'setProductPriceCustomField': () => {},
4093
4389
  // 'setDiscountedPrice': () => {},
4094
4390
  // 'setAttributeInAllVariants': () => {},
4095
- // 'addToCategory': () => {},
4096
4391
  // 'setCategoryOrderHint': () => {},
4097
- // 'removeFromCategory': () => {},
4098
- // 'setTaxCategory': () => {},
4099
4392
  // 'setSku': () => {},
4100
4393
  // 'setProductVariantKey': () => {},
4101
4394
  // 'setImageLabel': () => {},
@@ -4110,12 +4403,8 @@ var ProductRepository = class extends AbstractResourceRepository {
4110
4403
  // 'setAssetCustomType': () => {},
4111
4404
  // 'setAssetCustomField': () => {},
4112
4405
  // 'setSearchKeywords': () => {},
4113
- // 'setMetaTitle': () => {},
4114
- // 'setMetaDescription': () => {},
4115
- // 'setMetaKeywords': () => {},
4116
4406
  // 'revertStagedChanges': () => {},
4117
4407
  // 'revertStagedVariantChanges': () => {},
4118
- // 'transitionState': () => {},
4119
4408
  };
4120
4409
  };
4121
4410
  var checkForStagedChanges = (product) => {
@@ -4149,6 +4438,7 @@ var getVariant = (productData, variantId, sku) => {
4149
4438
  var variantFromDraft = (variantId, variant) => ({
4150
4439
  id: variantId,
4151
4440
  sku: variant?.sku,
4441
+ key: variant?.key,
4152
4442
  attributes: variant?.attributes ?? [],
4153
4443
  prices: variant?.prices?.map(priceFromDraft),
4154
4444
  assets: [],
@@ -4156,6 +4446,7 @@ var variantFromDraft = (variantId, variant) => ({
4156
4446
  });
4157
4447
  var priceFromDraft = (draft) => ({
4158
4448
  id: uuidv46(),
4449
+ key: draft.key,
4159
4450
  country: draft.country,
4160
4451
  value: createTypedMoney(draft.value)
4161
4452
  });