@labdigital/commercetools-mock 2.45.1 → 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.d.cts CHANGED
@@ -193,11 +193,16 @@ declare class PaymentRepository extends AbstractResourceRepository<"payment"> {
193
193
  create(context: RepositoryContext, draft: PaymentDraft): Payment;
194
194
  }
195
195
 
196
+ interface ProductSearchVariantAvailability {
197
+ isOnStock: boolean;
198
+ availableQuantity: number;
199
+ isOnStockForChannel: string | undefined;
200
+ }
196
201
  declare class ProductSearch {
197
202
  protected _storage: AbstractStorage;
198
203
  constructor(config: Config);
199
204
  search(projectKey: string, params: ProductSearchRequest): ProductPagedSearchResponse;
200
- transform(product: Product, staged: boolean): ProductProjection;
205
+ transformProduct(product: Product, staged: boolean, availabilityBySku: Map<string, ProductSearchVariantAvailability>): ProductProjection;
201
206
  }
202
207
 
203
208
  declare class ProductRepository extends AbstractResourceRepository<"product"> {
package/dist/index.d.ts CHANGED
@@ -193,11 +193,16 @@ declare class PaymentRepository extends AbstractResourceRepository<"payment"> {
193
193
  create(context: RepositoryContext, draft: PaymentDraft): Payment;
194
194
  }
195
195
 
196
+ interface ProductSearchVariantAvailability {
197
+ isOnStock: boolean;
198
+ availableQuantity: number;
199
+ isOnStockForChannel: string | undefined;
200
+ }
196
201
  declare class ProductSearch {
197
202
  protected _storage: AbstractStorage;
198
203
  constructor(config: Config);
199
204
  search(projectKey: string, params: ProductSearchRequest): ProductPagedSearchResponse;
200
- transform(product: Product, staged: boolean): ProductProjection;
205
+ transformProduct(product: Product, staged: boolean, availabilityBySku: Map<string, ProductSearchVariantAvailability>): ProductProjection;
201
206
  }
202
207
 
203
208
  declare class ProductRepository extends AbstractResourceRepository<"product"> {
package/dist/index.js CHANGED
@@ -1552,6 +1552,18 @@ var CartRepository = class extends AbstractResourceRepository {
1552
1552
  this.actions = new CartUpdateHandler(this._storage);
1553
1553
  }
1554
1554
  create(context, draft) {
1555
+ if (draft.anonymousId && draft.customerId) {
1556
+ throw new CommercetoolsError({
1557
+ code: "InvalidOperation",
1558
+ message: "Can set only one of customer OR anonymousId"
1559
+ });
1560
+ }
1561
+ if (draft.customerId) {
1562
+ this._storage.getByResourceIdentifier(context.projectKey, {
1563
+ typeId: "customer",
1564
+ id: draft.customerId
1565
+ });
1566
+ }
1555
1567
  const lineItems = draft.lineItems?.map(
1556
1568
  (draftLineItem) => this.draftLineItemtoLineItem(
1557
1569
  context.projectKey,
@@ -1566,6 +1578,7 @@ var CartRepository = class extends AbstractResourceRepository {
1566
1578
  billingAddress: draft.billingAddress ? createAddress(draft.billingAddress, context.projectKey, this._storage) : void 0,
1567
1579
  cartState: "Active",
1568
1580
  country: draft.country,
1581
+ customerId: draft.customerId,
1569
1582
  customerEmail: draft.customerEmail,
1570
1583
  customLineItems: [],
1571
1584
  directDiscounts: [],
@@ -4854,8 +4867,23 @@ var ProductSearch = class {
4854
4867
  this._storage = config.storage;
4855
4868
  }
4856
4869
  search(projectKey, params) {
4857
- let resources = this._storage.all(projectKey, "product").map(
4858
- (r) => this.transform(r, params.productProjectionParameters?.staged ?? false)
4870
+ const availabilityBySku = this._storage.all(projectKey, "inventory-entry").reduce((acc, entry) => {
4871
+ const existingEntry = acc.get(entry.sku);
4872
+ acc.set(entry.sku, {
4873
+ isOnStock: existingEntry?.isOnStock || entry.quantityOnStock > 0,
4874
+ availableQuantity: existingEntry?.availableQuantity ?? 0 + entry.quantityOnStock,
4875
+ // NOTE: This doesn't handle inventory entries for multiple channels,
4876
+ // so it doesn't exactly replicate the behavior of the commercetools api.
4877
+ isOnStockForChannel: existingEntry?.isOnStockForChannel ?? entry.supplyChannel?.id
4878
+ });
4879
+ return acc;
4880
+ }, /* @__PURE__ */ new Map());
4881
+ let productResources = this._storage.all(projectKey, "product").map(
4882
+ (r) => this.transformProduct(
4883
+ r,
4884
+ params.productProjectionParameters?.staged ?? false,
4885
+ availabilityBySku
4886
+ )
4859
4887
  ).filter((p) => {
4860
4888
  if (!(params.productProjectionParameters?.staged ?? false)) {
4861
4889
  return p.published;
@@ -4867,7 +4895,7 @@ var ProductSearch = class {
4867
4895
  try {
4868
4896
  validateSearchQuery(params.query);
4869
4897
  const matchFunc = parseSearchQuery(params.query);
4870
- resources = resources.filter(
4898
+ productResources = productResources.filter(
4871
4899
  (resource) => matchFunc(resource, markMatchingVariant)
4872
4900
  );
4873
4901
  } catch (err) {
@@ -4882,7 +4910,7 @@ var ProductSearch = class {
4882
4910
  }
4883
4911
  }
4884
4912
  if (params.productProjectionParameters) {
4885
- applyPriceSelector(resources, {
4913
+ applyPriceSelector(productResources, {
4886
4914
  country: params.productProjectionParameters.priceCountry,
4887
4915
  channel: params.productProjectionParameters.priceChannel,
4888
4916
  customerGroup: params.productProjectionParameters.priceCustomerGroup,
@@ -4891,7 +4919,10 @@ var ProductSearch = class {
4891
4919
  }
4892
4920
  const offset = params.offset || 0;
4893
4921
  const limit = params.limit || 20;
4894
- const productProjectionsResult = resources.slice(offset, offset + limit);
4922
+ const productProjectionsResult = productResources.slice(
4923
+ offset,
4924
+ offset + limit
4925
+ );
4895
4926
  const productProjectionsParameterGiven = !!params?.productProjectionParameters;
4896
4927
  const results = productProjectionsResult.map(
4897
4928
  (product) => ({
@@ -4904,14 +4935,14 @@ var ProductSearch = class {
4904
4935
  })
4905
4936
  );
4906
4937
  return {
4907
- total: resources.length,
4938
+ total: productResources.length,
4908
4939
  offset,
4909
4940
  limit,
4910
4941
  results,
4911
4942
  facets: []
4912
4943
  };
4913
4944
  }
4914
- transform(product, staged) {
4945
+ transformProduct(product, staged, availabilityBySku) {
4915
4946
  const obj = !staged ? product.masterData.current : product.masterData.staged;
4916
4947
  return {
4917
4948
  id: product.id,
@@ -4925,7 +4956,10 @@ var ProductSearch = class {
4925
4956
  slug: obj.slug,
4926
4957
  categories: obj.categories,
4927
4958
  masterVariant: obj.masterVariant,
4928
- variants: obj.variants,
4959
+ variants: obj.variants.map((variant) => ({
4960
+ ...variant,
4961
+ availability: variant.sku ? availabilityBySku.get(variant.sku) : { isOnStock: false, availableQuantity: 0, isOnStockForChannel: [] }
4962
+ })),
4929
4963
  productType: product.productType,
4930
4964
  hasStagedChanges: product.masterData.hasStagedChanges,
4931
4965
  published: product.masterData.published
@@ -5917,7 +5951,10 @@ var resolveValue = (obj, val) => {
5917
5951
  }
5918
5952
  return obj[val.value];
5919
5953
  };
5920
- 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);
5954
+ 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(
5955
+ "IDENTIFIER",
5956
+ /[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}/
5957
+ ).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);
5921
5958
  var generateMatchFunc2 = (predicate) => {
5922
5959
  const lexer = getLexer2(predicate);
5923
5960
  const parser = new Parser(lexer).builder().nud(