@labdigital/commercetools-mock 2.54.0 → 2.56.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.
Files changed (53) hide show
  1. package/dist/index.d.ts +107 -77
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +455 -8
  4. package/dist/index.js.map +1 -1
  5. package/package.json +4 -3
  6. package/src/lib/predicateParser.test.ts +13 -37
  7. package/src/lib/predicateParser.ts +12 -1
  8. package/src/lib/productSearchFilter.test.ts +1 -0
  9. package/src/lib/projectionSearchFilter.test.ts +1 -0
  10. package/src/priceSelector.test.ts +1 -0
  11. package/src/product-projection-search.ts +2 -0
  12. package/src/product-search.ts +1 -0
  13. package/src/repositories/cart/actions.ts +178 -0
  14. package/src/repositories/cart/helpers.ts +170 -3
  15. package/src/repositories/cart/index.test.ts +86 -2
  16. package/src/repositories/cart/index.ts +19 -2
  17. package/src/repositories/cart-discount/index.ts +1 -1
  18. package/src/repositories/customer/index.ts +2 -0
  19. package/src/repositories/discount-group/actions.ts +50 -0
  20. package/src/repositories/discount-group/index.ts +29 -0
  21. package/src/repositories/index.ts +6 -0
  22. package/src/repositories/order/index.test.ts +126 -125
  23. package/src/repositories/payment/actions.ts +87 -0
  24. package/src/repositories/payment/index.ts +1 -1
  25. package/src/repositories/product/index.ts +1 -0
  26. package/src/repositories/product-type.ts +1 -0
  27. package/src/repositories/quote/index.ts +1 -0
  28. package/src/repositories/quote-request/index.test.ts +1 -0
  29. package/src/repositories/quote-request/index.ts +1 -0
  30. package/src/repositories/recurrence-policy/actions.ts +53 -0
  31. package/src/repositories/recurrence-policy/index.ts +36 -0
  32. package/src/repositories/recurring-order/actions.ts +157 -0
  33. package/src/repositories/recurring-order/index.ts +52 -0
  34. package/src/repositories/review.test.ts +2 -0
  35. package/src/repositories/shopping-list/actions.ts +1 -0
  36. package/src/repositories/shopping-list/index.ts +1 -0
  37. package/src/services/cart.test.ts +282 -0
  38. package/src/services/discount-group.test.ts +270 -0
  39. package/src/services/discount-group.ts +16 -0
  40. package/src/services/index.ts +12 -0
  41. package/src/services/my-cart.test.ts +1 -0
  42. package/src/services/my-payment.test.ts +1 -0
  43. package/src/services/payment.test.ts +1 -0
  44. package/src/services/product-projection.test.ts +4 -0
  45. package/src/services/product-type.test.ts +1 -0
  46. package/src/services/product.test.ts +1 -0
  47. package/src/services/recurrence-policy.test.ts +316 -0
  48. package/src/services/recurrence-policy.ts +16 -0
  49. package/src/services/recurring-order.test.ts +424 -0
  50. package/src/services/recurring-order.ts +16 -0
  51. package/src/services/shopping-list.test.ts +3 -0
  52. package/src/storage/in-memory.ts +6 -0
  53. package/src/types.ts +6 -0
@@ -0,0 +1,36 @@
1
+ import assert from "node:assert";
2
+ import type {
3
+ RecurrencePolicy,
4
+ RecurrencePolicyDraft,
5
+ RecurringOrder,
6
+ RecurringOrderDraft,
7
+ } from "@commercetools/platform-sdk";
8
+ import type { Config } from "~src/config";
9
+ import { getBaseResourceProperties } from "~src/helpers";
10
+ import {
11
+ AbstractResourceRepository,
12
+ type RepositoryContext,
13
+ } from "../abstract";
14
+ import { OrderRepository } from "../order";
15
+ import { RecurrencePolicyUpdateHandler } from "./actions";
16
+
17
+ export class RecurrencePolicyRepository extends AbstractResourceRepository<"recurrence-policy"> {
18
+ constructor(config: Config) {
19
+ super("recurrence-policy", config);
20
+ this.actions = new RecurrencePolicyUpdateHandler(config.storage);
21
+ }
22
+
23
+ create(
24
+ context: RepositoryContext,
25
+ draft: RecurrencePolicyDraft,
26
+ ): RecurrencePolicy {
27
+ const resource: RecurrencePolicy = {
28
+ ...getBaseResourceProperties(),
29
+ key: draft.key,
30
+ name: draft.name,
31
+ description: draft.description,
32
+ schedule: draft.schedule,
33
+ };
34
+ return this.saveNew(context, resource);
35
+ }
36
+ }
@@ -0,0 +1,157 @@
1
+ import type {
2
+ RecurringOrder,
3
+ RecurringOrderSetCustomFieldAction,
4
+ RecurringOrderSetCustomTypeAction,
5
+ RecurringOrderSetExpiresAtAction,
6
+ RecurringOrderSetKeyAction,
7
+ RecurringOrderSetOrderSkipConfigurationAction,
8
+ RecurringOrderSetScheduleAction,
9
+ RecurringOrderSetStartsAtAction,
10
+ RecurringOrderSetStateAction,
11
+ RecurringOrderTransitionStateAction,
12
+ RecurringOrderUpdateAction,
13
+ } from "@commercetools/platform-sdk";
14
+ import type { Writable } from "~src/types";
15
+ import type { UpdateHandlerInterface } from "../abstract";
16
+ import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
17
+
18
+ export class RecurringOrderUpdateHandler
19
+ extends AbstractUpdateHandler
20
+ implements
21
+ Partial<UpdateHandlerInterface<RecurringOrder, RecurringOrderUpdateAction>>
22
+ {
23
+ setCustomField(
24
+ context: RepositoryContext,
25
+ resource: Writable<RecurringOrder>,
26
+ { name, value }: RecurringOrderSetCustomFieldAction,
27
+ ) {
28
+ if (!resource.custom) {
29
+ throw new Error("Resource has no custom field");
30
+ }
31
+ if (value === null) {
32
+ delete resource.custom.fields[name];
33
+ } else {
34
+ resource.custom.fields[name] = value;
35
+ }
36
+ }
37
+
38
+ setCustomType(
39
+ context: RepositoryContext,
40
+ resource: Writable<RecurringOrder>,
41
+ { type, fields }: RecurringOrderSetCustomTypeAction,
42
+ ) {
43
+ if (!type) {
44
+ resource.custom = undefined;
45
+ } else {
46
+ const resolvedType = this._storage.getByResourceIdentifier(
47
+ context.projectKey,
48
+ type,
49
+ );
50
+ if (!resolvedType) {
51
+ throw new Error(`Type ${type} not found`);
52
+ }
53
+
54
+ resource.custom = {
55
+ type: {
56
+ typeId: "type",
57
+ id: resolvedType.id,
58
+ },
59
+ fields: fields || {},
60
+ };
61
+ }
62
+ }
63
+
64
+ setExpiresAt(
65
+ context: RepositoryContext,
66
+ resource: Writable<RecurringOrder>,
67
+ { expiresAt }: RecurringOrderSetExpiresAtAction,
68
+ ) {
69
+ resource.expiresAt = expiresAt;
70
+ }
71
+
72
+ setKey(
73
+ context: RepositoryContext,
74
+ resource: Writable<RecurringOrder>,
75
+ { key }: RecurringOrderSetKeyAction,
76
+ ) {
77
+ resource.key = key;
78
+ }
79
+
80
+ setOrderSkipConfiguration(
81
+ context: RepositoryContext,
82
+ resource: Writable<RecurringOrder>,
83
+ {
84
+ skipConfiguration,
85
+ updatedExpiresAt,
86
+ }: RecurringOrderSetOrderSkipConfigurationAction,
87
+ ) {
88
+ if (skipConfiguration) {
89
+ resource.skipConfiguration = {
90
+ type: skipConfiguration.type,
91
+ totalToSkip: skipConfiguration.totalToSkip,
92
+ skipped: 0,
93
+ lastSkippedAt: undefined,
94
+ };
95
+ } else {
96
+ resource.skipConfiguration = undefined;
97
+ }
98
+ if (updatedExpiresAt !== undefined) {
99
+ resource.expiresAt = updatedExpiresAt;
100
+ }
101
+ }
102
+
103
+ setSchedule(
104
+ context: RepositoryContext,
105
+ resource: Writable<RecurringOrder>,
106
+ { recurrencePolicy }: RecurringOrderSetScheduleAction,
107
+ ) {
108
+ resource.schedule = {
109
+ ...resource.schedule,
110
+ ...recurrencePolicy,
111
+ };
112
+ }
113
+
114
+ setStartsAt(
115
+ context: RepositoryContext,
116
+ resource: Writable<RecurringOrder>,
117
+ { startsAt }: RecurringOrderSetStartsAtAction,
118
+ ) {
119
+ resource.startsAt = startsAt;
120
+ }
121
+
122
+ setRecurringOrderState(
123
+ context: RepositoryContext,
124
+ resource: Writable<RecurringOrder>,
125
+ { recurringOrderState }: RecurringOrderSetStateAction,
126
+ ) {
127
+ // Map the state draft to the actual state
128
+ switch (recurringOrderState.type) {
129
+ case "active":
130
+ resource.recurringOrderState = "Active";
131
+ if (recurringOrderState.resumesAt) {
132
+ resource.resumesAt = recurringOrderState.resumesAt;
133
+ }
134
+ break;
135
+ case "canceled":
136
+ resource.recurringOrderState = "Canceled";
137
+ break;
138
+ case "expired":
139
+ resource.recurringOrderState = "Expired";
140
+ break;
141
+ case "paused":
142
+ resource.recurringOrderState = "Paused";
143
+ break;
144
+ }
145
+ }
146
+
147
+ transitionState(
148
+ context: RepositoryContext,
149
+ resource: Writable<RecurringOrder>,
150
+ { state, force }: RecurringOrderTransitionStateAction,
151
+ ) {
152
+ resource.state = {
153
+ typeId: "state",
154
+ id: state.id!,
155
+ };
156
+ }
157
+ }
@@ -0,0 +1,52 @@
1
+ import assert from "node:assert";
2
+ import type {
3
+ RecurringOrder,
4
+ RecurringOrderDraft,
5
+ } from "@commercetools/platform-sdk";
6
+ import type { Config } from "~src/config";
7
+ import { getBaseResourceProperties } from "~src/helpers";
8
+ import {
9
+ AbstractResourceRepository,
10
+ type RepositoryContext,
11
+ } from "../abstract";
12
+ import { OrderRepository } from "../order";
13
+ import { RecurringOrderUpdateHandler } from "./actions";
14
+
15
+ export class RecurringOrderRepository extends AbstractResourceRepository<"recurring-order"> {
16
+ constructor(config: Config) {
17
+ super("recurring-order", config);
18
+ this.actions = new RecurringOrderUpdateHandler(config.storage);
19
+ }
20
+
21
+ create(
22
+ context: RepositoryContext,
23
+ draft: RecurringOrderDraft,
24
+ ): RecurringOrder {
25
+ assert(draft.cart, "draft.cart is missing");
26
+
27
+ const orderRepo = new OrderRepository(this.config);
28
+
29
+ const initialOrder = orderRepo.createFromCart(context, {
30
+ id: draft.cart.id!,
31
+ typeId: "cart",
32
+ });
33
+
34
+ const resource: RecurringOrder = {
35
+ ...getBaseResourceProperties(),
36
+ key: draft.key,
37
+ cart: {
38
+ typeId: "cart",
39
+ id: draft.cart.id!,
40
+ },
41
+ originOrder: {
42
+ typeId: "order",
43
+ id: initialOrder.id,
44
+ },
45
+ startsAt: draft.startsAt,
46
+ expiresAt: draft.expiresAt,
47
+ recurringOrderState: "Active",
48
+ schedule: { type: "standard", intervalUnit: "month", value: 1 },
49
+ };
50
+ return this.saveNew(context, resource);
51
+ }
52
+ }
@@ -36,6 +36,7 @@ describe("Review Repository", () => {
36
36
  current: {
37
37
  name: { "en-US": "Test Product" },
38
38
  slug: { "en-US": "test-product" },
39
+ attributes: [],
39
40
  categories: [],
40
41
  masterVariant: {
41
42
  id: 1,
@@ -49,6 +50,7 @@ describe("Review Repository", () => {
49
50
  staged: {
50
51
  name: { "en-US": "Test Product" },
51
52
  slug: { "en-US": "test-product" },
53
+ attributes: [],
52
54
  categories: [],
53
55
  masterVariant: {
54
56
  id: 1,
@@ -103,6 +103,7 @@ export class ShoppingListUpdateHandler
103
103
  name: product.masterData.current.name,
104
104
  variantId: varId,
105
105
  quantity,
106
+ published: Boolean(product.masterData.current),
106
107
  });
107
108
  }
108
109
  }
@@ -75,6 +75,7 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
75
75
  productId: draftLineItem.productId ?? "",
76
76
  name: {},
77
77
  variantId,
78
+ published: true,
78
79
  quantity: draftLineItem.quantity ?? 1,
79
80
  productType: { typeId: "product-type", id: "" },
80
81
  custom: createCustomFields(
@@ -111,6 +111,7 @@ describe("Carts Query", () => {
111
111
  describe("Cart Update Actions", () => {
112
112
  const ctMock = new CommercetoolsMock();
113
113
  let cart: Cart | undefined;
114
+ let taxCategory: TaxCategory;
114
115
 
115
116
  const createCart = async (currency: string) => {
116
117
  const response = await supertest(ctMock.app).post("/dummy/carts").send({
@@ -220,6 +221,18 @@ describe("Cart Update Actions", () => {
220
221
 
221
222
  beforeEach(async () => {
222
223
  await createCart("EUR");
224
+ taxCategory = await createTaxCategory({
225
+ name: "Standard VAT",
226
+ key: "standard-vat",
227
+ rates: [
228
+ {
229
+ name: "NL VAT",
230
+ amount: 0.21,
231
+ includedInPrice: false,
232
+ country: "NL",
233
+ },
234
+ ],
235
+ });
223
236
  });
224
237
 
225
238
  afterEach(() => {
@@ -1327,4 +1340,273 @@ describe("Cart Update Actions", () => {
1327
1340
  expect(updatedLineItem.shippingDetails).toBeDefined();
1328
1341
  expect(updatedLineItem.shippingDetails.targets).toHaveLength(1);
1329
1342
  });
1343
+
1344
+ test("addCustomLineItem", async () => {
1345
+ assert(cart, "cart not created");
1346
+ const type = await supertest(ctMock.app)
1347
+ .post("/dummy/types")
1348
+ .send({
1349
+ key: "custom-line-item-type",
1350
+ name: { en: "Custom Line Item Type" },
1351
+ resourceTypeIds: ["custom-line-item"],
1352
+ fieldDefinitions: [
1353
+ {
1354
+ name: "description",
1355
+ label: { en: "Description" },
1356
+ required: false,
1357
+ type: { name: "String" },
1358
+ inputHint: "SingleLine",
1359
+ },
1360
+ ],
1361
+ })
1362
+ .then((res) => res.body);
1363
+
1364
+ const response = await supertest(ctMock.app)
1365
+ .post(`/dummy/carts/${cart.id}`)
1366
+ .send({
1367
+ version: 1,
1368
+ actions: [
1369
+ {
1370
+ action: "addCustomLineItem",
1371
+ name: { en: "Custom Service Fee" },
1372
+ slug: "service-fee",
1373
+ money: {
1374
+ currencyCode: "EUR",
1375
+ centAmount: 1000,
1376
+ },
1377
+ quantity: 1,
1378
+ taxCategory: {
1379
+ typeId: "tax-category",
1380
+ id: taxCategory.id,
1381
+ },
1382
+ custom: {
1383
+ type: { typeId: "type", key: type.key },
1384
+ fields: { description: "Premium support service" },
1385
+ },
1386
+ },
1387
+ ],
1388
+ });
1389
+
1390
+ expect(response.status).toBe(200);
1391
+ expect(response.body.version).toBe(2);
1392
+ expect(response.body.customLineItems).toHaveLength(1);
1393
+
1394
+ const customLineItem = response.body.customLineItems[0];
1395
+ expect(customLineItem.name).toEqual({ en: "Custom Service Fee" });
1396
+ expect(customLineItem.slug).toBe("service-fee");
1397
+ expect(customLineItem.money.centAmount).toBe(1000);
1398
+ expect(customLineItem.quantity).toBe(1);
1399
+ expect(customLineItem.totalPrice.centAmount).toBe(1000);
1400
+ expect(customLineItem.taxCategory.id).toBe(taxCategory.id);
1401
+ expect(customLineItem.taxedPrice).toBeDefined();
1402
+ expect(customLineItem.id).toBeDefined();
1403
+ expect(customLineItem.custom).toBeDefined();
1404
+ expect(customLineItem.custom.fields.description).toBe(
1405
+ "Premium support service",
1406
+ );
1407
+ });
1408
+
1409
+ test("removeCustomLineItem by ID", async () => {
1410
+ assert(cart, "cart not created");
1411
+
1412
+ const addResponse = await supertest(ctMock.app)
1413
+ .post(`/dummy/carts/${cart.id}`)
1414
+ .send({
1415
+ version: 1,
1416
+ actions: [
1417
+ {
1418
+ action: "addCustomLineItem",
1419
+ name: { en: "Service Fee" },
1420
+ slug: "service-fee",
1421
+ money: {
1422
+ currencyCode: "EUR",
1423
+ centAmount: 1000,
1424
+ },
1425
+ quantity: 1,
1426
+ },
1427
+ ],
1428
+ });
1429
+
1430
+ expect(addResponse.status).toBe(200);
1431
+ expect(addResponse.body.customLineItems).toHaveLength(1);
1432
+
1433
+ const customLineItemId = addResponse.body.customLineItems[0].id;
1434
+ const removeResponse = await supertest(ctMock.app)
1435
+ .post(`/dummy/carts/${cart.id}`)
1436
+ .send({
1437
+ version: addResponse.body.version,
1438
+ actions: [
1439
+ {
1440
+ action: "removeCustomLineItem",
1441
+ customLineItemId,
1442
+ },
1443
+ ],
1444
+ });
1445
+
1446
+ expect(removeResponse.status).toBe(200);
1447
+ expect(removeResponse.body.customLineItems).toHaveLength(0);
1448
+ });
1449
+
1450
+ test("removeCustomLineItem by key", async () => {
1451
+ assert(cart, "cart not created");
1452
+
1453
+ const addResponse = await supertest(ctMock.app)
1454
+ .post(`/dummy/carts/${cart.id}`)
1455
+ .send({
1456
+ version: 1,
1457
+ actions: [
1458
+ {
1459
+ action: "addCustomLineItem",
1460
+ name: { en: "Service Fee" },
1461
+ slug: "service-fee",
1462
+ key: "custom-service-fee",
1463
+ money: {
1464
+ currencyCode: "EUR",
1465
+ centAmount: 1000,
1466
+ },
1467
+ quantity: 1,
1468
+ },
1469
+ ],
1470
+ });
1471
+
1472
+ expect(addResponse.status).toBe(200);
1473
+ expect(addResponse.body.customLineItems).toHaveLength(1);
1474
+
1475
+ const removeResponse = await supertest(ctMock.app)
1476
+ .post(`/dummy/carts/${cart.id}`)
1477
+ .send({
1478
+ version: addResponse.body.version,
1479
+ actions: [
1480
+ {
1481
+ action: "removeCustomLineItem",
1482
+ customLineItemKey: "custom-service-fee",
1483
+ },
1484
+ ],
1485
+ });
1486
+
1487
+ expect(removeResponse.status).toBe(200);
1488
+ expect(removeResponse.body.customLineItems).toHaveLength(0);
1489
+ });
1490
+
1491
+ test("changeCustomLineItemQuantity", async () => {
1492
+ assert(cart, "cart not created");
1493
+ const addResponse = await supertest(ctMock.app)
1494
+ .post(`/dummy/carts/${cart.id}`)
1495
+ .send({
1496
+ version: 1,
1497
+ actions: [
1498
+ {
1499
+ action: "addCustomLineItem",
1500
+ name: { en: "Service Fee" },
1501
+ slug: "service-fee",
1502
+ money: {
1503
+ currencyCode: "EUR",
1504
+ centAmount: 1000,
1505
+ },
1506
+ quantity: 1,
1507
+ },
1508
+ ],
1509
+ });
1510
+
1511
+ const customLineItemId = addResponse.body.customLineItems[0].id;
1512
+ const changeResponse = await supertest(ctMock.app)
1513
+ .post(`/dummy/carts/${cart.id}`)
1514
+ .send({
1515
+ version: addResponse.body.version,
1516
+ actions: [
1517
+ {
1518
+ action: "changeCustomLineItemQuantity",
1519
+ customLineItemId,
1520
+ quantity: 3,
1521
+ },
1522
+ ],
1523
+ });
1524
+
1525
+ expect(changeResponse.status).toBe(200);
1526
+ expect(changeResponse.body.customLineItems).toHaveLength(1);
1527
+
1528
+ const customLineItem = changeResponse.body.customLineItems[0];
1529
+ expect(customLineItem.quantity).toBe(3);
1530
+ expect(customLineItem.totalPrice.centAmount).toBe(3000);
1531
+ });
1532
+
1533
+ test("changeCustomLineItemMoney", async () => {
1534
+ assert(cart, "cart not created");
1535
+ const addResponse = await supertest(ctMock.app)
1536
+ .post(`/dummy/carts/${cart.id}`)
1537
+ .send({
1538
+ version: 1,
1539
+ actions: [
1540
+ {
1541
+ action: "addCustomLineItem",
1542
+ name: { en: "Service Fee" },
1543
+ slug: "service-fee",
1544
+ money: {
1545
+ currencyCode: "EUR",
1546
+ centAmount: 1000,
1547
+ },
1548
+ quantity: 2,
1549
+ },
1550
+ ],
1551
+ });
1552
+
1553
+ const customLineItemId = addResponse.body.customLineItems[0].id;
1554
+ const changeResponse = await supertest(ctMock.app)
1555
+ .post(`/dummy/carts/${cart.id}`)
1556
+ .send({
1557
+ version: addResponse.body.version,
1558
+ actions: [
1559
+ {
1560
+ action: "changeCustomLineItemMoney",
1561
+ customLineItemId,
1562
+ money: {
1563
+ currencyCode: "EUR",
1564
+ centAmount: 1500,
1565
+ },
1566
+ },
1567
+ ],
1568
+ });
1569
+
1570
+ expect(changeResponse.status).toBe(200);
1571
+ expect(changeResponse.body.customLineItems).toHaveLength(1);
1572
+
1573
+ const customLineItem = changeResponse.body.customLineItems[0];
1574
+ expect(customLineItem.money.centAmount).toBe(1500);
1575
+ expect(customLineItem.totalPrice.centAmount).toBe(3000);
1576
+ });
1577
+
1578
+ test("addCustomLineItem with tax calculation", async () => {
1579
+ assert(cart, "cart not created");
1580
+
1581
+ const response = await supertest(ctMock.app)
1582
+ .post(`/dummy/carts/${cart.id}`)
1583
+ .send({
1584
+ version: 1,
1585
+ actions: [
1586
+ {
1587
+ action: "addCustomLineItem",
1588
+ name: { en: "Taxed Service" },
1589
+ slug: "taxed-service",
1590
+ money: {
1591
+ currencyCode: "EUR",
1592
+ centAmount: 1000,
1593
+ },
1594
+ quantity: 1,
1595
+ taxCategory: {
1596
+ typeId: "tax-category",
1597
+ id: taxCategory.id,
1598
+ },
1599
+ },
1600
+ ],
1601
+ });
1602
+
1603
+ expect(response.status).toBe(200);
1604
+ const customLineItem = response.body.customLineItems[0];
1605
+
1606
+ expect(customLineItem.taxedPrice).toBeDefined();
1607
+ expect(customLineItem.taxedPrice.totalNet.centAmount).toBe(1000);
1608
+ expect(customLineItem.taxedPrice.totalGross.centAmount).toBe(1210);
1609
+ expect(customLineItem.taxedPrice.taxPortions).toHaveLength(1);
1610
+ expect(customLineItem.taxedPrice.taxPortions[0].rate).toBe(0.21);
1611
+ });
1330
1612
  });