@labdigital/commercetools-mock 2.29.1 → 2.30.1

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
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import { SetupServerApi, SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
- import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, ProductTailoring, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
4
+ import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, ProductTailoring, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductSearchRequest, ProductPagedSearchResponse, Product, ProductProjection, ProductDraft, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
5
5
 
6
6
  type Token = {
7
7
  access_token: string;
@@ -299,9 +299,18 @@ declare class PaymentRepository extends AbstractResourceRepository<"payment"> {
299
299
  create(context: RepositoryContext, draft: PaymentDraft): Payment;
300
300
  }
301
301
 
302
+ declare class ProductSearch {
303
+ protected _storage: AbstractStorage;
304
+ constructor(storage: AbstractStorage);
305
+ search(projectKey: string, params: ProductSearchRequest): ProductPagedSearchResponse;
306
+ transform(product: Product, staged: boolean): ProductProjection;
307
+ }
308
+
302
309
  declare class ProductRepository extends AbstractResourceRepository<"product"> {
310
+ protected _searchService: ProductSearch;
303
311
  constructor(storage: AbstractStorage);
304
312
  create(context: RepositoryContext, draft: ProductDraft): Product;
313
+ search(context: RepositoryContext, searchRequest: ProductSearchRequest): ProductPagedSearchResponse;
305
314
  }
306
315
 
307
316
  declare class ProductDiscountRepository extends AbstractResourceRepository<"product-discount"> {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import { SetupServerApi, SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
- import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, ProductTailoring, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
4
+ import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, ProductTailoring, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductSearchRequest, ProductPagedSearchResponse, Product, ProductProjection, ProductDraft, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
5
5
 
6
6
  type Token = {
7
7
  access_token: string;
@@ -299,9 +299,18 @@ declare class PaymentRepository extends AbstractResourceRepository<"payment"> {
299
299
  create(context: RepositoryContext, draft: PaymentDraft): Payment;
300
300
  }
301
301
 
302
+ declare class ProductSearch {
303
+ protected _storage: AbstractStorage;
304
+ constructor(storage: AbstractStorage);
305
+ search(projectKey: string, params: ProductSearchRequest): ProductPagedSearchResponse;
306
+ transform(product: Product, staged: boolean): ProductProjection;
307
+ }
308
+
302
309
  declare class ProductRepository extends AbstractResourceRepository<"product"> {
310
+ protected _searchService: ProductSearch;
303
311
  constructor(storage: AbstractStorage);
304
312
  create(context: RepositoryContext, draft: ProductDraft): Product;
313
+ search(context: RepositoryContext, searchRequest: ProductSearchRequest): ProductPagedSearchResponse;
305
314
  }
306
315
 
307
316
  declare class ProductDiscountRepository extends AbstractResourceRepository<"product-discount"> {
package/dist/index.js CHANGED
@@ -3953,6 +3953,9 @@ var OrderUpdateHandler = class extends AbstractUpdateHandler {
3953
3953
  changePaymentState(context, resource, { paymentState }) {
3954
3954
  resource.paymentState = paymentState;
3955
3955
  }
3956
+ changeShipmentState(context, resource, { shipmentState }) {
3957
+ resource.shipmentState = shipmentState;
3958
+ }
3956
3959
  setBillingAddress(context, resource, { address }) {
3957
3960
  resource.billingAddress = createAddress(
3958
3961
  address,
@@ -4504,6 +4507,165 @@ var PaymentRepository = class extends AbstractResourceRepository {
4504
4507
  }
4505
4508
  };
4506
4509
 
4510
+ // src/lib/searchQueryTypeChecker.ts
4511
+ var validateSearchQuery = (query) => {
4512
+ if (isSearchAndExpression(query)) {
4513
+ query.and.forEach((expr) => validateSearchQuery(expr));
4514
+ } else if (isSearchOrExpression(query)) {
4515
+ query.or.forEach((expr) => validateSearchQuery(expr));
4516
+ } else if (isSearchNotExpression(query)) {
4517
+ validateSearchQuery(query.not);
4518
+ } else if (isSearchFilterExpression(query) || isSearchRangeExpression(query) || isSearchExactExpression(query) || isSearchExistsExpression(query) || isSearchFullTextExpression(query) || isSearchFullTextPrefixExpression(query) || isSearchPrefixExpression(query) || isSearchWildCardExpression(query) || isSearchAnyValue(query)) {
4519
+ return;
4520
+ } else {
4521
+ throw new Error("Unsupported search query expression");
4522
+ }
4523
+ };
4524
+ var isSearchAndExpression = (expr) => expr.and !== void 0;
4525
+ var isSearchOrExpression = (expr) => expr.or !== void 0;
4526
+ var isSearchNotExpression = (expr) => expr.not !== void 0;
4527
+ var isSearchFilterExpression = (expr) => expr.filter !== void 0;
4528
+ var isSearchRangeExpression = (expr) => expr.range !== void 0;
4529
+ var isSearchExactExpression = (expr) => expr.exact !== void 0;
4530
+ var isSearchExistsExpression = (expr) => expr.exists !== void 0;
4531
+ var isSearchFullTextExpression = (expr) => expr.fullText !== void 0;
4532
+ var isSearchFullTextPrefixExpression = (expr) => expr.fullTextPrefix !== void 0;
4533
+ var isSearchPrefixExpression = (expr) => expr.prefix !== void 0;
4534
+ var isSearchWildCardExpression = (expr) => expr.wildcard !== void 0;
4535
+ var isSearchAnyValue = (expr) => expr.value !== void 0;
4536
+
4537
+ // src/priceSelector.ts
4538
+ var applyPriceSelector = (products, selector, noScopedPrice = false) => {
4539
+ validatePriceSelector(selector);
4540
+ for (const product of products) {
4541
+ const variants = [
4542
+ product.masterVariant,
4543
+ ...product.variants ?? []
4544
+ ].filter((x) => x != void 0);
4545
+ for (const variant of variants) {
4546
+ const scopedPrices = variant.prices?.filter((p) => priceSelectorFilter(p, selector)) ?? [];
4547
+ if (scopedPrices.length > 0) {
4548
+ const price = scopedPrices[0];
4549
+ variant.price = scopedPrices[0];
4550
+ if (!noScopedPrice) {
4551
+ variant.scopedPriceDiscounted = false;
4552
+ variant.scopedPrice = {
4553
+ ...price,
4554
+ currentValue: price.value
4555
+ };
4556
+ }
4557
+ }
4558
+ }
4559
+ }
4560
+ };
4561
+ var validatePriceSelector = (selector) => {
4562
+ if ((selector.country || selector.channel || selector.customerGroup) && !selector.currency) {
4563
+ throw new CommercetoolsError(
4564
+ {
4565
+ code: "InvalidInput",
4566
+ message: "The price selecting parameters country, channel and customerGroup cannot be used without the currency."
4567
+ },
4568
+ 400
4569
+ );
4570
+ }
4571
+ };
4572
+ var priceSelectorFilter = (price, selector) => {
4573
+ if ((selector.country || price.country) && selector.country !== price.country) {
4574
+ return false;
4575
+ }
4576
+ if ((selector.currency || price.value.currencyCode) && selector.currency !== price.value.currencyCode) {
4577
+ return false;
4578
+ }
4579
+ if ((selector.channel || price.channel?.id) && selector.channel !== price.channel?.id) {
4580
+ return false;
4581
+ }
4582
+ if ((selector.customerGroup || price.customerGroup?.id) && selector.customerGroup !== price.customerGroup?.id) {
4583
+ return false;
4584
+ }
4585
+ return true;
4586
+ };
4587
+
4588
+ // src/product-search.ts
4589
+ var ProductSearch = class {
4590
+ _storage;
4591
+ constructor(storage) {
4592
+ this._storage = storage;
4593
+ }
4594
+ search(projectKey, params) {
4595
+ const resources = this._storage.all(projectKey, "product").map(
4596
+ (r) => this.transform(r, params.productProjectionParameters?.staged ?? false)
4597
+ ).filter((p) => {
4598
+ if (!params.productProjectionParameters?.staged) {
4599
+ return p.published;
4600
+ }
4601
+ return true;
4602
+ });
4603
+ if (params.query) {
4604
+ try {
4605
+ validateSearchQuery(params.query);
4606
+ } catch (err) {
4607
+ console.error(err);
4608
+ throw new CommercetoolsError(
4609
+ {
4610
+ code: "InvalidInput",
4611
+ message: err.message
4612
+ },
4613
+ 400
4614
+ );
4615
+ }
4616
+ }
4617
+ if (params.productProjectionParameters) {
4618
+ applyPriceSelector(resources, {
4619
+ country: params.productProjectionParameters.priceCountry,
4620
+ channel: params.productProjectionParameters.priceChannel,
4621
+ customerGroup: params.productProjectionParameters.priceCustomerGroup,
4622
+ currency: params.productProjectionParameters.priceCurrency
4623
+ });
4624
+ }
4625
+ const offset = params.offset || 0;
4626
+ const limit = params.limit || 20;
4627
+ const productProjectionsResult = resources.slice(offset, offset + limit);
4628
+ const productProjectionsParameterGiven = !!params?.productProjectionParameters;
4629
+ const results = productProjectionsResult.map(
4630
+ (product) => ({
4631
+ productProjection: productProjectionsParameterGiven ? product : void 0,
4632
+ id: product.id
4633
+ /**
4634
+ * @TODO: possibly add support for optional matchingVariants
4635
+ * https://docs.commercetools.com/api/projects/product-search#productsearchmatchingvariants
4636
+ */
4637
+ })
4638
+ );
4639
+ return {
4640
+ total: resources.length,
4641
+ offset,
4642
+ limit,
4643
+ results,
4644
+ facets: []
4645
+ };
4646
+ }
4647
+ transform(product, staged) {
4648
+ const obj = !staged ? product.masterData.current : product.masterData.staged;
4649
+ return {
4650
+ id: product.id,
4651
+ createdAt: product.createdAt,
4652
+ lastModifiedAt: product.lastModifiedAt,
4653
+ version: product.version,
4654
+ name: obj.name,
4655
+ key: product.key,
4656
+ description: obj.description,
4657
+ metaDescription: obj.metaDescription,
4658
+ slug: obj.slug,
4659
+ categories: obj.categories,
4660
+ masterVariant: obj.masterVariant,
4661
+ variants: obj.variants,
4662
+ productType: product.productType,
4663
+ hasStagedChanges: product.masterData.hasStagedChanges,
4664
+ published: product.masterData.published
4665
+ };
4666
+ }
4667
+ };
4668
+
4507
4669
  // src/repositories/product/helpers.ts
4508
4670
  import deepEqual2 from "deep-equal";
4509
4671
  import { v4 as uuidv410 } from "uuid";
@@ -5234,9 +5396,11 @@ var ProductUpdateHandler = class extends AbstractUpdateHandler {
5234
5396
 
5235
5397
  // src/repositories/product/index.ts
5236
5398
  var ProductRepository = class extends AbstractResourceRepository {
5399
+ _searchService;
5237
5400
  constructor(storage) {
5238
5401
  super("product", storage);
5239
5402
  this.actions = new ProductUpdateHandler(storage);
5403
+ this._searchService = new ProductSearch(storage);
5240
5404
  }
5241
5405
  create(context, draft) {
5242
5406
  if (!draft.masterVariant) {
@@ -5329,6 +5493,9 @@ var ProductRepository = class extends AbstractResourceRepository {
5329
5493
  };
5330
5494
  return this.saveNew(context, resource);
5331
5495
  }
5496
+ search(context, searchRequest) {
5497
+ return this._searchService.search(context.projectKey, searchRequest);
5498
+ }
5332
5499
  };
5333
5500
 
5334
5501
  // src/repositories/product-discount.ts
@@ -5412,57 +5579,6 @@ var ProductDiscountUpdateHandler = class extends AbstractUpdateHandler {
5412
5579
  }
5413
5580
  };
5414
5581
 
5415
- // src/priceSelector.ts
5416
- var applyPriceSelector = (products, selector, noScopedPrice = false) => {
5417
- validatePriceSelector(selector);
5418
- for (const product of products) {
5419
- const variants = [
5420
- product.masterVariant,
5421
- ...product.variants ?? []
5422
- ].filter((x) => x != void 0);
5423
- for (const variant of variants) {
5424
- const scopedPrices = variant.prices?.filter((p) => priceSelectorFilter(p, selector)) ?? [];
5425
- if (scopedPrices.length > 0) {
5426
- const price = scopedPrices[0];
5427
- variant.price = scopedPrices[0];
5428
- if (!noScopedPrice) {
5429
- variant.scopedPriceDiscounted = false;
5430
- variant.scopedPrice = {
5431
- ...price,
5432
- currentValue: price.value
5433
- };
5434
- }
5435
- }
5436
- }
5437
- }
5438
- };
5439
- var validatePriceSelector = (selector) => {
5440
- if ((selector.country || selector.channel || selector.customerGroup) && !selector.currency) {
5441
- throw new CommercetoolsError(
5442
- {
5443
- code: "InvalidInput",
5444
- message: "The price selecting parameters country, channel and customerGroup cannot be used without the currency."
5445
- },
5446
- 400
5447
- );
5448
- }
5449
- };
5450
- var priceSelectorFilter = (price, selector) => {
5451
- if ((selector.country || price.country) && selector.country !== price.country) {
5452
- return false;
5453
- }
5454
- if ((selector.currency || price.value.currencyCode) && selector.currency !== price.value.currencyCode) {
5455
- return false;
5456
- }
5457
- if ((selector.channel || price.channel?.id) && selector.channel !== price.channel?.id) {
5458
- return false;
5459
- }
5460
- if ((selector.customerGroup || price.customerGroup?.id) && selector.customerGroup !== price.customerGroup?.id) {
5461
- return false;
5462
- }
5463
- return true;
5464
- };
5465
-
5466
5582
  // src/lib/projectionSearchFilter.ts
5467
5583
  var parseFilterExpression = (filter) => {
5468
5584
  const exprFunc = generateMatchFunc2(filter);
@@ -7967,6 +8083,17 @@ var ProductService = class extends AbstractService {
7967
8083
  getBasePath() {
7968
8084
  return "products";
7969
8085
  }
8086
+ extraRoutes(router) {
8087
+ router.post("/search", this.search.bind(this));
8088
+ }
8089
+ search(request, response) {
8090
+ const searchBody = request.body;
8091
+ const resource = this.repository.search(
8092
+ getRepositoryContext(request),
8093
+ searchBody
8094
+ );
8095
+ return response.status(200).send(resource);
8096
+ }
7970
8097
  };
7971
8098
 
7972
8099
  // src/services/product-discount.ts