@labdigital/commercetools-mock 2.28.1 → 2.29.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.28.1",
3
+ "version": "2.29.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -26,6 +26,8 @@ import type {
26
26
  ProductSetMetaDescriptionAction,
27
27
  ProductSetMetaKeywordsAction,
28
28
  ProductSetMetaTitleAction,
29
+ ProductSetProductPriceCustomFieldAction,
30
+ ProductSetProductPriceCustomTypeAction,
29
31
  ProductSetTaxCategoryAction,
30
32
  ProductTransitionStateAction,
31
33
  ProductUpdateAction,
@@ -35,8 +37,11 @@ import type {
35
37
  } from "@commercetools/platform-sdk";
36
38
  import { CommercetoolsError } from "~src/exceptions";
37
39
  import type { Writable } from "~src/types";
38
- import { AbstractUpdateHandler, RepositoryContext } from "../abstract";
39
- import { getReferenceFromResourceIdentifier } from "../helpers";
40
+ import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
41
+ import {
42
+ createCustomFields,
43
+ getReferenceFromResourceIdentifier,
44
+ } from "../helpers";
40
45
  import {
41
46
  checkForStagedChanges,
42
47
  getVariant,
@@ -853,6 +858,87 @@ export class ProductUpdateHandler
853
858
  return resource;
854
859
  }
855
860
 
861
+ setProductPriceCustomField(
862
+ context: RepositoryContext,
863
+ resource: Writable<Product>,
864
+ { name, value, staged, priceId }: ProductSetProductPriceCustomFieldAction,
865
+ ) {
866
+ const updatePriceCustomFields = (data: Writable<ProductData>) => {
867
+ const price = [data.masterVariant, ...(data.variants ?? [])]
868
+ .flatMap((variant) => variant.prices ?? [])
869
+ .find((price) => price.id === priceId);
870
+
871
+ if (!price) {
872
+ throw new Error(
873
+ `Price with id ${priceId} not found on product ${resource.id}`,
874
+ );
875
+ }
876
+ if (price.custom) {
877
+ if (value === null) {
878
+ delete price.custom.fields[name];
879
+ } else {
880
+ price.custom.fields[name] = value;
881
+ }
882
+ }
883
+ return data;
884
+ };
885
+
886
+ resource.masterData.staged = updatePriceCustomFields(
887
+ resource.masterData.staged,
888
+ );
889
+
890
+ const onlyStaged = staged !== undefined ? staged : true;
891
+ if (!onlyStaged) {
892
+ resource.masterData.current = updatePriceCustomFields(
893
+ resource.masterData.current,
894
+ );
895
+ }
896
+ checkForStagedChanges(resource);
897
+ return resource;
898
+ }
899
+
900
+ setProductPriceCustomType(
901
+ context: RepositoryContext,
902
+ resource: Writable<Product>,
903
+ { type, fields, staged, priceId }: ProductSetProductPriceCustomTypeAction,
904
+ ) {
905
+ const updatePriceCustomType = (data: Writable<ProductData>) => {
906
+ const price = [data.masterVariant, ...(data.variants ?? [])]
907
+ .flatMap((variant) => variant.prices ?? [])
908
+ .find((price) => price.id === priceId);
909
+
910
+ if (price) {
911
+ if (type) {
912
+ price.custom = createCustomFields(
913
+ { type, fields },
914
+ context.projectKey,
915
+ this._storage,
916
+ );
917
+ } else {
918
+ price.custom = undefined;
919
+ }
920
+ } else {
921
+ throw new Error(
922
+ `Price with id ${priceId} not found on product ${resource.id}`,
923
+ );
924
+ }
925
+ return data;
926
+ };
927
+
928
+ resource.masterData.staged = updatePriceCustomType(
929
+ resource.masterData.staged,
930
+ );
931
+
932
+ const onlyStaged = staged !== undefined ? staged : true;
933
+ if (!onlyStaged) {
934
+ resource.masterData.current = updatePriceCustomType(
935
+ resource.masterData.current,
936
+ );
937
+ }
938
+ checkForStagedChanges(resource);
939
+ return resource;
940
+ }
941
+
856
942
  setTaxCategory(
857
943
  context: RepositoryContext,
858
944
  resource: Writable<Product>,
@@ -919,8 +1005,6 @@ export class ProductUpdateHandler
919
1005
  }
920
1006
 
921
1007
  // 'setPrices': () => {},
922
- // 'setProductPriceCustomType': () => {},
923
- // 'setProductPriceCustomField': () => {},
924
1008
  // 'setDiscountedPrice': () => {},
925
1009
  // 'setAttributeInAllVariants': () => {},
926
1010
  // 'setCategoryOrderHint': () => {},
@@ -12,6 +12,8 @@ import type {
12
12
  StateDraft,
13
13
  TaxCategory,
14
14
  TaxCategoryDraft,
15
+ Type,
16
+ TypeDraft,
15
17
  } from "@commercetools/platform-sdk";
16
18
  import assert from "assert";
17
19
  import supertest from "supertest";
@@ -78,6 +80,30 @@ const productState2Draft: StateDraft = {
78
80
  },
79
81
  };
80
82
 
83
+ const productPriceTypeDraft: TypeDraft = {
84
+ key: "product-price",
85
+ name: {
86
+ en: "ProductPriceType",
87
+ },
88
+ description: {
89
+ en: "Product Price Type",
90
+ },
91
+ resourceTypeIds: ["product-price"],
92
+ fieldDefinitions: [
93
+ {
94
+ name: "lastModifiedAt",
95
+ label: {
96
+ en: "Last modified at",
97
+ },
98
+ required: false,
99
+ type: {
100
+ name: "DateTime",
101
+ },
102
+ inputHint: "SingleLine",
103
+ },
104
+ ],
105
+ };
106
+
81
107
  const publishedProductDraft: ProductDraft = {
82
108
  name: {
83
109
  "nl-NL": "test published product",
@@ -250,6 +276,7 @@ let taxCategory1: TaxCategory;
250
276
  let taxCategory2: TaxCategory;
251
277
  let productState1: State;
252
278
  let productState2: State;
279
+ let productPriceType: Type;
253
280
 
254
281
  async function beforeAllProductTests(mock: CommercetoolsMock) {
255
282
  let response;
@@ -308,6 +335,12 @@ async function beforeAllProductTests(mock: CommercetoolsMock) {
308
335
 
309
336
  expect(response.status).toBe(201);
310
337
  productState2 = response.body;
338
+
339
+ response = await supertest(mock.app)
340
+ .post("/dummy/types")
341
+ .send(productPriceTypeDraft);
342
+ expect(response.status).toBe(201);
343
+ productPriceType = response.body;
311
344
  }
312
345
 
313
346
  describe("Product", () => {
@@ -1418,4 +1451,38 @@ describe("Product update actions", () => {
1418
1451
  id: productState2.id,
1419
1452
  });
1420
1453
  });
1454
+
1455
+ test("setProductPriceCustomField", async () => {
1456
+ assert(productPublished, "product not created");
1457
+ const priceId =
1458
+ productPublished?.masterData.current.masterVariant.prices?.[0].id;
1459
+ assert(priceId);
1460
+
1461
+ const response = await supertest(ctMock.app)
1462
+ .post(`/dummy/products/${productPublished.id}`)
1463
+ .send({
1464
+ version: 1,
1465
+ actions: [
1466
+ {
1467
+ action: "setProductPriceCustomType",
1468
+ priceId,
1469
+ type: {
1470
+ typeId: "type",
1471
+ key: productPriceType.key,
1472
+ },
1473
+ },
1474
+ {
1475
+ action: "setProductPriceCustomField",
1476
+ name: "myCustomField",
1477
+ value: "MyRandomValue",
1478
+ priceId,
1479
+ },
1480
+ ],
1481
+ });
1482
+ expect(response.status).toBe(200);
1483
+ expect(
1484
+ response.body.masterData.staged.masterVariant.prices?.[0].custom.fields
1485
+ ?.myCustomField,
1486
+ ).toBe("MyRandomValue");
1487
+ });
1421
1488
  });