@labdigital/commercetools-mock 2.28.0 → 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.0",
3
+ "version": "2.29.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -11,6 +11,7 @@ import type {
11
11
  OrderSetCustomTypeAction,
12
12
  OrderSetCustomerEmailAction,
13
13
  OrderSetCustomerIdAction,
14
+ OrderSetDeliveryCustomFieldAction,
14
15
  OrderSetLocaleAction,
15
16
  OrderSetOrderNumberAction,
16
17
  OrderSetShippingAddressAction,
@@ -23,6 +24,7 @@ import type {
23
24
  Store,
24
25
  SyncInfo,
25
26
  } from "@commercetools/platform-sdk";
27
+ import assert from "assert";
26
28
  import { getBaseResourceProperties } from "~src/helpers";
27
29
  import type { Writable } from "~src/types";
28
30
  import {
@@ -180,6 +182,26 @@ export class OrderUpdateHandler
180
182
  }
181
183
  }
182
184
 
185
+ setDeliveryCustomField(
186
+ context: RepositoryContext,
187
+ resource: Writable<Order>,
188
+ { deliveryId, name, value }: OrderSetDeliveryCustomFieldAction,
189
+ ) {
190
+ assert(resource.shippingInfo, "shippingInfo is not defined");
191
+
192
+ if (Array.isArray(resource.shippingInfo.deliveries)) {
193
+ resource.shippingInfo.deliveries.map((delivery) => {
194
+ if (delivery.id !== deliveryId) throw "No matching delivery id found";
195
+ if (delivery.custom) {
196
+ const update = delivery.custom.fields;
197
+ update[name] = value;
198
+ Object.assign(delivery.custom.fields, update);
199
+ }
200
+ return delivery;
201
+ });
202
+ }
203
+ }
204
+
183
205
  setLocale(
184
206
  context: RepositoryContext,
185
207
  resource: Writable<Order>,
@@ -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': () => {},
@@ -2,6 +2,7 @@ import type { Order, Payment, State } from "@commercetools/platform-sdk";
2
2
  import assert from "assert";
3
3
  import supertest from "supertest";
4
4
  import { afterEach, beforeEach, describe, expect, test } from "vitest";
5
+ import { generateRandomString } from "~src/helpers";
5
6
  import { CommercetoolsMock, getBaseResourceProperties } from "../index";
6
7
 
7
8
  describe("Order Query", () => {
@@ -402,6 +403,166 @@ describe("Order Update Actions", () => {
402
403
  expect(response.body.paymentState).toBe("Failed");
403
404
  });
404
405
 
406
+ test("setDeliveryCustomField", async () => {
407
+ const order: Order = {
408
+ ...getBaseResourceProperties(),
409
+ customLineItems: [],
410
+ lastMessageSequenceNumber: 0,
411
+ lineItems: [],
412
+ orderNumber: "1389",
413
+ orderState: "Open",
414
+ origin: "Customer",
415
+ paymentInfo: {
416
+ payments: [
417
+ {
418
+ typeId: "payment",
419
+ id: generateRandomString(10),
420
+ },
421
+ ],
422
+ },
423
+ refusedGifts: [],
424
+ shippingInfo: {
425
+ shippingMethodName: "Home delivery (package)",
426
+ price: {
427
+ type: "centPrecision",
428
+ currencyCode: "EUR",
429
+ centAmount: 999,
430
+ fractionDigits: 2,
431
+ },
432
+ shippingRate: {
433
+ price: {
434
+ type: "centPrecision",
435
+ currencyCode: "EUR",
436
+ centAmount: 999,
437
+ fractionDigits: 2,
438
+ },
439
+ tiers: [
440
+ {
441
+ type: "CartScore",
442
+ score: 24,
443
+ price: {
444
+ type: "centPrecision",
445
+ currencyCode: "EUR",
446
+ centAmount: 1998,
447
+ fractionDigits: 2,
448
+ },
449
+ },
450
+ {
451
+ type: "CartScore",
452
+ score: 47,
453
+ price: {
454
+ type: "centPrecision",
455
+ currencyCode: "EUR",
456
+ centAmount: 2997,
457
+ fractionDigits: 2,
458
+ },
459
+ },
460
+ {
461
+ type: "CartScore",
462
+ score: 70,
463
+ price: {
464
+ type: "centPrecision",
465
+ currencyCode: "EUR",
466
+ centAmount: 3996,
467
+ fractionDigits: 2,
468
+ },
469
+ },
470
+ {
471
+ type: "CartScore",
472
+ score: 93,
473
+ price: {
474
+ type: "centPrecision",
475
+ currencyCode: "EUR",
476
+ centAmount: 4995,
477
+ fractionDigits: 2,
478
+ },
479
+ },
480
+ ],
481
+ },
482
+ deliveries: [
483
+ {
484
+ id: "6a458cad-dd46-4f5f-8b73-debOede6a17d",
485
+ key: "CT-Z243002",
486
+ createdAt: "2024-07-29T13:37:48.047Z",
487
+ items: [
488
+ {
489
+ id: "5d209544-2892-45c9-bef0-dde4e250188e",
490
+ quantity: 1,
491
+ },
492
+ ],
493
+ parcels: [],
494
+ custom: {
495
+ type: {
496
+ typeId: "type",
497
+ id: "c493b7bb-d415-450c-b421-e128a8b26569",
498
+ },
499
+ fields: {
500
+ location: "test",
501
+ status: "created",
502
+ carrier: "test_carrier",
503
+ },
504
+ },
505
+ },
506
+ ],
507
+ shippingMethodState: "MatchesCart",
508
+ },
509
+ shipping: [],
510
+ shippingMode: "Single",
511
+ syncInfo: [],
512
+ totalPrice: {
513
+ type: "centPrecision",
514
+ fractionDigits: 2,
515
+ centAmount: 2000,
516
+ currencyCode: "EUR",
517
+ },
518
+ };
519
+ ctMock.project("dummy").add("order", order);
520
+
521
+ const response = await supertest(ctMock.app).get(
522
+ `/dummy/orders/order-number=${order.orderNumber}`,
523
+ );
524
+
525
+ // check if status is set
526
+ const _updateResponse = await supertest(ctMock.app)
527
+ .post(`/dummy/orders/${response.body.id}`)
528
+ .send({
529
+ version: 0,
530
+ actions: [
531
+ {
532
+ action: "setDeliveryCustomField",
533
+ deliveryId: "6a458cad-dd46-4f5f-8b73-debOede6a17d",
534
+ name: "status",
535
+ value: "delayed",
536
+ },
537
+ ],
538
+ });
539
+ expect(_updateResponse.status).toBe(200);
540
+ expect(_updateResponse.body.version).toBe(1);
541
+ expect(
542
+ _updateResponse.body.shippingInfo.deliveries[0].custom.fields.status,
543
+ ).toBe("delayed");
544
+
545
+ // check if other field can be set
546
+ const _updateResponse2 = await supertest(ctMock.app)
547
+ .post(`/dummy/orders/${response.body.id}`)
548
+ .send({
549
+ version: 1,
550
+ actions: [
551
+ {
552
+ action: "setDeliveryCustomField",
553
+ deliveryId: "6a458cad-dd46-4f5f-8b73-debOede6a17d",
554
+ name: "carrier",
555
+ value: "dhl",
556
+ },
557
+ ],
558
+ });
559
+ expect(_updateResponse2.status).toBe(200);
560
+ expect(_updateResponse2.body.version).toBe(2);
561
+ expect(
562
+ _updateResponse2.body.shippingInfo.deliveries[0].custom.fields.carrier,
563
+ ).toBe("dhl");
564
+ });
565
+
405
566
  test("updateSyncInfo", async () => {
406
567
  assert(order, "order not created");
407
568
 
@@ -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
  });