@labdigital/commercetools-mock 2.6.0 → 2.8.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
@@ -1231,11 +1231,13 @@ var InMemoryStorage = class extends AbstractStorage {
1231
1231
  if (reference === void 0)
1232
1232
  return;
1233
1233
  if (reference.typeId !== void 0 && (reference.id !== void 0 || reference.key !== void 0)) {
1234
- reference.obj = this.getByResourceIdentifier(projectKey, {
1235
- typeId: reference.typeId,
1236
- id: reference.id,
1237
- key: reference.key
1238
- });
1234
+ if (!reference.obj) {
1235
+ reference.obj = this.getByResourceIdentifier(projectKey, {
1236
+ typeId: reference.typeId,
1237
+ id: reference.id,
1238
+ key: reference.key
1239
+ });
1240
+ }
1239
1241
  if (expand) {
1240
1242
  this._resolveResource(projectKey, reference.obj, expand);
1241
1243
  }
@@ -1670,7 +1672,7 @@ var getReferenceFromResourceIdentifier = (resourceIdentifier, projectKey, storag
1670
1672
  resourceIdentifier
1671
1673
  );
1672
1674
  if (!resource) {
1673
- const errIdentifier = resourceIdentifier.key ? `key '${resourceIdentifier.key}'` : `identifier '${resourceIdentifier.key}'`;
1675
+ const errIdentifier = resourceIdentifier.key ? `key '${resourceIdentifier.key}'` : `identifier '${resourceIdentifier.id}'`;
1674
1676
  throw new CommercetoolsError(
1675
1677
  {
1676
1678
  code: "ReferencedResourceNotFound",
@@ -3010,7 +3012,8 @@ var CustomerRepository = class extends AbstractResourceRepository {
3010
3012
  email: draft.email.toLowerCase(),
3011
3013
  password: draft.password ? hashPassword(draft.password) : void 0,
3012
3014
  isEmailVerified: draft.isEmailVerified || false,
3013
- addresses: []
3015
+ addresses: [],
3016
+ customerNumber: draft.customerNumber
3014
3017
  };
3015
3018
  this.saveNew(context, resource);
3016
3019
  return resource;
@@ -3516,10 +3519,11 @@ var OrderRepository = class extends AbstractResourceRepository {
3516
3519
  name: draft.name,
3517
3520
  quantity: draft.quantity ?? 0,
3518
3521
  perMethodTaxRate: [],
3519
- priceMode: draft.priceMode,
3522
+ priceMode: draft.priceMode ?? "Standard",
3520
3523
  slug: draft.slug,
3521
3524
  state: [],
3522
- totalPrice: createCentPrecisionMoney(draft.money)
3525
+ totalPrice: createCentPrecisionMoney(draft.money),
3526
+ taxedPricePortions: []
3523
3527
  };
3524
3528
  return lineItem;
3525
3529
  }
@@ -3555,6 +3559,37 @@ var OrderRepository = class extends AbstractResourceRepository {
3555
3559
  id: payment.id
3556
3560
  });
3557
3561
  },
3562
+ addReturnInfo: (context, resource, info) => {
3563
+ if (!resource.returnInfo) {
3564
+ resource.returnInfo = [];
3565
+ }
3566
+ const resolved = {
3567
+ items: info.items.map((item) => {
3568
+ const common = {
3569
+ ...getBaseResourceProperties(),
3570
+ quantity: item.quantity,
3571
+ paymentState: "Initial",
3572
+ shipmentState: "Initial",
3573
+ comment: item.comment
3574
+ };
3575
+ if (item.customLineItemId) {
3576
+ return {
3577
+ ...common,
3578
+ type: "CustomLineItemReturnItem",
3579
+ customLineItemId: item.customLineItemId
3580
+ };
3581
+ }
3582
+ return {
3583
+ ...common,
3584
+ type: "LineItemReturnItem",
3585
+ lineItemId: item.customLineItemId || item.lineItemId
3586
+ };
3587
+ }),
3588
+ returnTrackingId: info.returnTrackingId,
3589
+ returnDate: info.returnDate
3590
+ };
3591
+ resource.returnInfo.push(resolved);
3592
+ },
3558
3593
  changeOrderState: (context, resource, { orderState }) => {
3559
3594
  resource.orderState = orderState;
3560
3595
  },
@@ -3865,9 +3900,9 @@ var ProductRepository = class extends AbstractResourceRepository {
3865
3900
  slug: draft.slug,
3866
3901
  description: draft.description,
3867
3902
  categories: categoryReferences,
3868
- masterVariant: variantFromDraft(1, draft.masterVariant),
3903
+ masterVariant: this.variantFromDraft(context, 1, draft.masterVariant),
3869
3904
  variants: draft.variants?.map(
3870
- (variant, index) => variantFromDraft(index + 2, variant)
3905
+ (variant, index) => this.variantFromDraft(context, index + 2, variant)
3871
3906
  ) ?? [],
3872
3907
  metaTitle: draft.metaTitle,
3873
3908
  metaDescription: draft.metaDescription,
@@ -3890,6 +3925,30 @@ var ProductRepository = class extends AbstractResourceRepository {
3890
3925
  this.saveNew(context, resource);
3891
3926
  return resource;
3892
3927
  }
3928
+ variantFromDraft(context, variantId, variant) {
3929
+ return {
3930
+ id: variantId,
3931
+ sku: variant?.sku,
3932
+ key: variant?.key,
3933
+ attributes: variant?.attributes ?? [],
3934
+ prices: variant?.prices?.map((p) => this.priceFromDraft(context, p)),
3935
+ assets: [],
3936
+ images: []
3937
+ };
3938
+ }
3939
+ priceFromDraft(context, draft) {
3940
+ return {
3941
+ id: (0, import_uuid6.v4)(),
3942
+ key: draft.key,
3943
+ country: draft.country,
3944
+ value: createTypedMoney(draft.value),
3945
+ channel: draft.channel ? getReferenceFromResourceIdentifier(
3946
+ draft.channel,
3947
+ context.projectKey,
3948
+ this._storage
3949
+ ) : void 0
3950
+ };
3951
+ }
3893
3952
  actions = {
3894
3953
  publish: (context, resource, { scope }) => {
3895
3954
  resource.masterData.current = resource.masterData.staged;
@@ -4134,7 +4193,7 @@ var ProductRepository = class extends AbstractResourceRepository {
4134
4193
  data.variants[variantIndex] = variant;
4135
4194
  }
4136
4195
  };
4137
- const priceToAdd = priceFromDraft(price);
4196
+ const priceToAdd = this.priceFromDraft(context, price);
4138
4197
  const onlyStaged = staged !== void 0 ? staged : true;
4139
4198
  addVariantPrice(resource.masterData.staged, priceToAdd);
4140
4199
  if (!onlyStaged) {
@@ -4291,7 +4350,7 @@ var ProductRepository = class extends AbstractResourceRepository {
4291
4350
  (max, element) => element.id > max ? element.id : max,
4292
4351
  0
4293
4352
  );
4294
- const variant = variantFromDraft(maxId + 1, variantDraft);
4353
+ const variant = this.variantFromDraft(context, maxId + 1, variantDraft);
4295
4354
  dataStaged.variants.push(variant);
4296
4355
  const onlyStaged = staged !== void 0 ? staged : true;
4297
4356
  if (!onlyStaged) {
@@ -4529,21 +4588,6 @@ var getVariant = (productData, variantId, sku) => {
4529
4588
  variantIndex: !isMasterVariant && foundVariant ? productData.variants.indexOf(foundVariant) : -1
4530
4589
  };
4531
4590
  };
4532
- var variantFromDraft = (variantId, variant) => ({
4533
- id: variantId,
4534
- sku: variant?.sku,
4535
- key: variant?.key,
4536
- attributes: variant?.attributes ?? [],
4537
- prices: variant?.prices?.map(priceFromDraft),
4538
- assets: [],
4539
- images: []
4540
- });
4541
- var priceFromDraft = (draft) => ({
4542
- id: (0, import_uuid6.v4)(),
4543
- key: draft.key,
4544
- country: draft.country,
4545
- value: createTypedMoney(draft.value)
4546
- });
4547
4591
 
4548
4592
  // src/repositories/product-discount.ts
4549
4593
  var ProductDiscountRepository = class extends AbstractResourceRepository {
@@ -4815,7 +4859,7 @@ var getVariants = (p) => [
4815
4859
  ];
4816
4860
 
4817
4861
  // src/priceSelector.ts
4818
- var applyPriceSelector = (products, selector) => {
4862
+ var applyPriceSelector = (products, selector, noScopedPrice = false) => {
4819
4863
  validatePriceSelector(selector);
4820
4864
  for (const product of products) {
4821
4865
  const variants = [
@@ -4826,11 +4870,14 @@ var applyPriceSelector = (products, selector) => {
4826
4870
  const scopedPrices = variant.prices?.filter((p) => priceSelectorFilter(p, selector)) ?? [];
4827
4871
  if (scopedPrices.length > 0) {
4828
4872
  const price = scopedPrices[0];
4829
- variant.scopedPriceDiscounted = false;
4830
- variant.scopedPrice = {
4831
- ...price,
4832
- currentValue: price.value
4833
- };
4873
+ variant.price = scopedPrices[0];
4874
+ if (!noScopedPrice) {
4875
+ variant.scopedPriceDiscounted = false;
4876
+ variant.scopedPrice = {
4877
+ ...price,
4878
+ currentValue: price.value
4879
+ };
4880
+ }
4834
4881
  }
4835
4882
  }
4836
4883
  }
@@ -5165,6 +5212,16 @@ var ProductProjectionRepository = class extends AbstractResourceRepository {
5165
5212
  );
5166
5213
  }
5167
5214
  }
5215
+ applyPriceSelector(
5216
+ resources,
5217
+ {
5218
+ country: params.priceCountry,
5219
+ channel: params.priceChannel,
5220
+ customerGroup: params.priceCustomerGroup,
5221
+ currency: params.priceCurrency
5222
+ },
5223
+ true
5224
+ );
5168
5225
  if (params.expand !== void 0) {
5169
5226
  resources = resources.map(
5170
5227
  (resource) => this._storage.expand(context.projectKey, resource, params.expand)
@@ -5199,7 +5256,6 @@ var ProductSelectionRepository = class extends AbstractResourceRepository {
5199
5256
  productCount: 0,
5200
5257
  key: draft.key,
5201
5258
  name: draft.name,
5202
- type: "individual",
5203
5259
  mode: "Individual"
5204
5260
  };
5205
5261
  this.saveNew(context, resource);
@@ -5415,9 +5471,28 @@ var ReviewRepository = class extends AbstractResourceRepository {
5415
5471
  return "review";
5416
5472
  }
5417
5473
  create(context, draft) {
5474
+ if (!draft.target)
5475
+ throw new Error("Missing target");
5418
5476
  const resource = {
5419
5477
  ...getBaseResourceProperties(),
5420
- includedInStatistics: false
5478
+ locale: draft.locale,
5479
+ authorName: draft.authorName,
5480
+ title: draft.title,
5481
+ text: draft.text,
5482
+ rating: draft.rating,
5483
+ uniquenessValue: draft.uniquenessValue,
5484
+ state: draft.state ? getReferenceFromResourceIdentifier(
5485
+ draft.state,
5486
+ context.projectKey,
5487
+ this._storage
5488
+ ) : void 0,
5489
+ target: draft.target ? getReferenceFromResourceIdentifier(draft.target, context.projectKey, this._storage) : void 0,
5490
+ includedInStatistics: false,
5491
+ custom: createCustomFields(
5492
+ draft.custom,
5493
+ context.projectKey,
5494
+ this._storage
5495
+ )
5421
5496
  };
5422
5497
  this.saveNew(context, resource);
5423
5498
  return resource;
@@ -6031,6 +6106,7 @@ var createRepositories = (storage) => ({
6031
6106
  "my-order": new MyOrderRepository(storage),
6032
6107
  "my-customer": new CustomerRepository(storage),
6033
6108
  "my-payment": new PaymentRepository(storage),
6109
+ "my-shopping-list": new ShoppingListRepository(storage),
6034
6110
  product: new ProductRepository(storage),
6035
6111
  "product-type": new ProductTypeRepository(storage),
6036
6112
  "product-discount": new ProductDiscountRepository(storage),
@@ -6486,6 +6562,7 @@ var MyCustomerService = class extends AbstractService {
6486
6562
  const router = (0, import_express4.Router)({ mergeParams: true });
6487
6563
  this.extraRoutes(router);
6488
6564
  router.get("", this.getMe.bind(this));
6565
+ router.post("", this.updateMe.bind(this));
6489
6566
  router.post("/signup", this.signUp.bind(this));
6490
6567
  router.post("/login", this.signIn.bind(this));
6491
6568
  parent.use(`/${basePath}`, router);
@@ -6497,6 +6574,21 @@ var MyCustomerService = class extends AbstractService {
6497
6574
  }
6498
6575
  return response.status(200).send(resource);
6499
6576
  }
6577
+ updateMe(request, response) {
6578
+ const resource = this.repository.getMe(getRepositoryContext(request));
6579
+ if (!resource) {
6580
+ return response.status(404).send("Not found");
6581
+ }
6582
+ const updateRequest = request.body;
6583
+ const updatedResource = this.repository.processUpdateActions(
6584
+ getRepositoryContext(request),
6585
+ resource,
6586
+ updateRequest.version,
6587
+ updateRequest.actions
6588
+ );
6589
+ const result = this._expandWithId(request, updatedResource.id);
6590
+ return response.status(200).send(result);
6591
+ }
6500
6592
  signUp(request, response) {
6501
6593
  const draft = request.body;
6502
6594
  const resource = this.repository.create(
@@ -6563,6 +6655,18 @@ var MyPaymentService = class extends AbstractService {
6563
6655
  }
6564
6656
  };
6565
6657
 
6658
+ // src/services/my-shopping-list.ts
6659
+ var MyShoppingListService = class extends AbstractService {
6660
+ repository;
6661
+ constructor(parent, repository) {
6662
+ super(parent);
6663
+ this.repository = repository;
6664
+ }
6665
+ getBasePath() {
6666
+ return "me/shopping-lists";
6667
+ }
6668
+ };
6669
+
6566
6670
  // src/services/order.ts
6567
6671
  var OrderService = class extends AbstractService {
6568
6672
  repository;
@@ -6708,6 +6812,18 @@ var ProductService = class extends AbstractService {
6708
6812
  }
6709
6813
  };
6710
6814
 
6815
+ // src/services/reviews.ts
6816
+ var ReviewService = class extends AbstractService {
6817
+ repository;
6818
+ constructor(parent, repository) {
6819
+ super(parent);
6820
+ this.repository = repository;
6821
+ }
6822
+ getBasePath() {
6823
+ return "reviews";
6824
+ }
6825
+ };
6826
+
6711
6827
  // src/services/shipping-method.ts
6712
6828
  var ShippingMethodService = class extends AbstractService {
6713
6829
  repository;
@@ -6850,6 +6966,10 @@ var createServices = (router, repos) => ({
6850
6966
  "my-order": new MyOrderService(router, repos["my-order"]),
6851
6967
  "my-customer": new MyCustomerService(router, repos["my-customer"]),
6852
6968
  "my-payment": new MyPaymentService(router, repos["my-payment"]),
6969
+ "my-shopping-list": new MyShoppingListService(
6970
+ router,
6971
+ repos["my-shopping-list"]
6972
+ ),
6853
6973
  "shipping-method": new ShippingMethodService(
6854
6974
  router,
6855
6975
  repos["shipping-method"]
@@ -6868,6 +6988,7 @@ var createServices = (router, repos) => ({
6868
6988
  router,
6869
6989
  repos["product-selection"]
6870
6990
  ),
6991
+ reviews: new ReviewService(router, repos["review"]),
6871
6992
  "shopping-list": new ShoppingListService(router, repos["shopping-list"]),
6872
6993
  state: new StateService(router, repos["state"]),
6873
6994
  store: new StoreService(router, repos["store"]),
@@ -6921,7 +7042,6 @@ var CommercetoolsMock = class {
6921
7042
  this._mswServer = void 0;
6922
7043
  }
6923
7044
  clear() {
6924
- this._mswServer?.resetHandlers();
6925
7045
  this._storage.clear();
6926
7046
  }
6927
7047
  project(projectKey) {
@@ -7058,6 +7178,9 @@ var CommercetoolsMock = class {
7058
7178
  })
7059
7179
  );
7060
7180
  }
7181
+ mswServer() {
7182
+ return this._mswServer;
7183
+ }
7061
7184
  startServer() {
7062
7185
  if (_globalListeners.length > 0) {
7063
7186
  if (this._mswServer !== void 0) {