@labdigital/commercetools-mock 2.45.0 → 2.46.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
@@ -1589,6 +1589,18 @@ var CartRepository = class extends AbstractResourceRepository {
1589
1589
  this.actions = new CartUpdateHandler(this._storage);
1590
1590
  }
1591
1591
  create(context, draft) {
1592
+ if (draft.anonymousId && draft.customerId) {
1593
+ throw new CommercetoolsError({
1594
+ code: "InvalidOperation",
1595
+ message: "Can set only one of customer OR anonymousId"
1596
+ });
1597
+ }
1598
+ if (draft.customerId) {
1599
+ this._storage.getByResourceIdentifier(context.projectKey, {
1600
+ typeId: "customer",
1601
+ id: draft.customerId
1602
+ });
1603
+ }
1592
1604
  const lineItems = draft.lineItems?.map(
1593
1605
  (draftLineItem) => this.draftLineItemtoLineItem(
1594
1606
  context.projectKey,
@@ -1603,6 +1615,7 @@ var CartRepository = class extends AbstractResourceRepository {
1603
1615
  billingAddress: draft.billingAddress ? createAddress(draft.billingAddress, context.projectKey, this._storage) : void 0,
1604
1616
  cartState: "Active",
1605
1617
  country: draft.country,
1618
+ customerId: draft.customerId,
1606
1619
  customerEmail: draft.customerEmail,
1607
1620
  customLineItems: [],
1608
1621
  directDiscounts: [],
@@ -4891,8 +4904,23 @@ var ProductSearch = class {
4891
4904
  this._storage = config.storage;
4892
4905
  }
4893
4906
  search(projectKey, params) {
4894
- let resources = this._storage.all(projectKey, "product").map(
4895
- (r) => this.transform(r, params.productProjectionParameters?.staged ?? false)
4907
+ const availabilityBySku = this._storage.all(projectKey, "inventory-entry").reduce((acc, entry) => {
4908
+ const existingEntry = acc.get(entry.sku);
4909
+ acc.set(entry.sku, {
4910
+ isOnStock: existingEntry?.isOnStock || entry.quantityOnStock > 0,
4911
+ availableQuantity: existingEntry?.availableQuantity ?? 0 + entry.quantityOnStock,
4912
+ // NOTE: This doesn't handle inventory entries for multiple channels,
4913
+ // so it doesn't exactly replicate the behavior of the commercetools api.
4914
+ isOnStockForChannel: existingEntry?.isOnStockForChannel ?? entry.supplyChannel?.id
4915
+ });
4916
+ return acc;
4917
+ }, /* @__PURE__ */ new Map());
4918
+ let productResources = this._storage.all(projectKey, "product").map(
4919
+ (r) => this.transformProduct(
4920
+ r,
4921
+ params.productProjectionParameters?.staged ?? false,
4922
+ availabilityBySku
4923
+ )
4896
4924
  ).filter((p) => {
4897
4925
  if (!(params.productProjectionParameters?.staged ?? false)) {
4898
4926
  return p.published;
@@ -4904,7 +4932,7 @@ var ProductSearch = class {
4904
4932
  try {
4905
4933
  validateSearchQuery(params.query);
4906
4934
  const matchFunc = parseSearchQuery(params.query);
4907
- resources = resources.filter(
4935
+ productResources = productResources.filter(
4908
4936
  (resource) => matchFunc(resource, markMatchingVariant)
4909
4937
  );
4910
4938
  } catch (err) {
@@ -4919,7 +4947,7 @@ var ProductSearch = class {
4919
4947
  }
4920
4948
  }
4921
4949
  if (params.productProjectionParameters) {
4922
- applyPriceSelector(resources, {
4950
+ applyPriceSelector(productResources, {
4923
4951
  country: params.productProjectionParameters.priceCountry,
4924
4952
  channel: params.productProjectionParameters.priceChannel,
4925
4953
  customerGroup: params.productProjectionParameters.priceCustomerGroup,
@@ -4928,7 +4956,10 @@ var ProductSearch = class {
4928
4956
  }
4929
4957
  const offset = params.offset || 0;
4930
4958
  const limit = params.limit || 20;
4931
- const productProjectionsResult = resources.slice(offset, offset + limit);
4959
+ const productProjectionsResult = productResources.slice(
4960
+ offset,
4961
+ offset + limit
4962
+ );
4932
4963
  const productProjectionsParameterGiven = !!params?.productProjectionParameters;
4933
4964
  const results = productProjectionsResult.map(
4934
4965
  (product) => ({
@@ -4941,14 +4972,14 @@ var ProductSearch = class {
4941
4972
  })
4942
4973
  );
4943
4974
  return {
4944
- total: resources.length,
4975
+ total: productResources.length,
4945
4976
  offset,
4946
4977
  limit,
4947
4978
  results,
4948
4979
  facets: []
4949
4980
  };
4950
4981
  }
4951
- transform(product, staged) {
4982
+ transformProduct(product, staged, availabilityBySku) {
4952
4983
  const obj = !staged ? product.masterData.current : product.masterData.staged;
4953
4984
  return {
4954
4985
  id: product.id,
@@ -4962,7 +4993,10 @@ var ProductSearch = class {
4962
4993
  slug: obj.slug,
4963
4994
  categories: obj.categories,
4964
4995
  masterVariant: obj.masterVariant,
4965
- variants: obj.variants,
4996
+ variants: obj.variants.map((variant) => ({
4997
+ ...variant,
4998
+ availability: variant.sku ? availabilityBySku.get(variant.sku) : { isOnStock: false, availableQuantity: 0, isOnStockForChannel: [] }
4999
+ })),
4966
5000
  productType: product.productType,
4967
5001
  hasStagedChanges: product.masterData.hasStagedChanges,
4968
5002
  published: product.masterData.published
@@ -5007,9 +5041,17 @@ var variantFromDraft = (context, storage, variantId, variant) => ({
5007
5041
  key: variant?.key,
5008
5042
  attributes: variant?.attributes ?? [],
5009
5043
  prices: variant?.prices?.map((p) => priceFromDraft(context, storage, p)),
5010
- assets: [],
5011
- images: []
5044
+ assets: variant.assets?.map((a) => assetFromDraft(context, storage, a)) ?? [],
5045
+ images: variant.images ?? []
5012
5046
  });
5047
+ var assetFromDraft = (context, storage, draft) => {
5048
+ const asset = {
5049
+ ...draft,
5050
+ id: (0, import_uuid10.v4)(),
5051
+ custom: createCustomFields(draft.custom, context.projectKey, storage)
5052
+ };
5053
+ return asset;
5054
+ };
5013
5055
  var priceFromDraft = (context, storage, draft) => ({
5014
5056
  id: (0, import_uuid10.v4)(),
5015
5057
  key: draft.key,
@@ -5946,7 +5988,10 @@ var resolveValue = (obj, val) => {
5946
5988
  }
5947
5989
  return obj[val.value];
5948
5990
  };
5949
- var getLexer2 = (value) => new Lexer(value).token("AND", /and(?![-_a-z0-9]+)/i).token("OR", /or(?![-_a-z0-9]+)/i).token("NOT", /not(?![-_a-z0-9]+)/i).token("WITHIN", /within(?![-_a-z0-9]+)/i).token("IN", /in(?![-_a-z0-9]+)/i).token("MATCHES_IGNORE_CASE", /matches\s+ignore\s+case(?![-_a-z0-9]+)/i).token("CONTAINS", /contains(?![-_a-z0-9]+)/i).token("ALL", /all(?![-_a-z0-9]+)/i).token("ANY", /any(?![-_a-z0-9]+)/i).token("EMPTY", /empty(?![-_a-z0-9]+)/i).token("IS", /is(?![-_a-z0-9]+)/i).token("DEFINED", /defined(?![-_a-z0-9]+)/i).token("FLOAT", /\d+\.\d+/).token("INT", /\d+/).token("VARIABLE", /:([-_A-Za-z0-9]+)/).token("BOOLEAN", /(true|false)/).token("IDENTIFIER", /[-_A-Za-z0-9]+/).token("STRING", /"((?:\\.|[^"\\])*)"/).token("STRING", /'((?:\\.|[^'\\])*)'/).token("COMMA", ",").token("(", "(").token(")", ")").token(">=", ">=").token("<=", "<=").token(">", ">").token("<", "<").token("!=", "!=").token("=", "=").token('"', '"').token("WS", /\s+/, true);
5991
+ var getLexer2 = (value) => new Lexer(value).token("AND", /and(?![-_a-z0-9]+)/i).token("OR", /or(?![-_a-z0-9]+)/i).token("NOT", /not(?![-_a-z0-9]+)/i).token("WITHIN", /within(?![-_a-z0-9]+)/i).token("IN", /in(?![-_a-z0-9]+)/i).token("MATCHES_IGNORE_CASE", /matches\s+ignore\s+case(?![-_a-z0-9]+)/i).token("CONTAINS", /contains(?![-_a-z0-9]+)/i).token("ALL", /all(?![-_a-z0-9]+)/i).token("ANY", /any(?![-_a-z0-9]+)/i).token("EMPTY", /empty(?![-_a-z0-9]+)/i).token("IS", /is(?![-_a-z0-9]+)/i).token("DEFINED", /defined(?![-_a-z0-9]+)/i).token(
5992
+ "IDENTIFIER",
5993
+ /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/
5994
+ ).token("FLOAT", /\d+\.\d+/).token("INT", /\d+/).token("VARIABLE", /:([-_A-Za-z0-9]+)/).token("BOOLEAN", /(true|false)/).token("IDENTIFIER", /[-_A-Za-z0-9]+/).token("STRING", /"((?:\\.|[^"\\])*)"/).token("STRING", /'((?:\\.|[^'\\])*)'/).token("COMMA", ",").token("(", "(").token(")", ")").token(">=", ">=").token("<=", "<=").token(">", ">").token("<", "<").token("!=", "!=").token("=", "=").token('"', '"').token("WS", /\s+/, true);
5950
5995
  var generateMatchFunc2 = (predicate) => {
5951
5996
  const lexer = getLexer2(predicate);
5952
5997
  const parser = new Parser(lexer).builder().nud(