@labdigital/commercetools-mock 2.34.1 → 2.34.3
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.cjs +33 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +33 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/cart/index.test.ts +151 -0
- package/src/repositories/cart/index.ts +15 -5
- package/src/repositories/order/index.test.ts +127 -45
- package/src/repositories/order/index.ts +28 -16
package/package.json
CHANGED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { CartDraft, LineItem } from "@commercetools/platform-sdk";
|
|
2
|
+
import { describe, expect, test } from "vitest";
|
|
3
|
+
import { InMemoryStorage } from "~src/storage";
|
|
4
|
+
import { CartRepository } from "./index";
|
|
5
|
+
|
|
6
|
+
describe("Cart repository", () => {
|
|
7
|
+
const storage = new InMemoryStorage();
|
|
8
|
+
const repository = new CartRepository(storage);
|
|
9
|
+
|
|
10
|
+
test("create cart in store", async () => {
|
|
11
|
+
storage.add("dummy", "product", {
|
|
12
|
+
createdAt: "",
|
|
13
|
+
lastModifiedAt: "",
|
|
14
|
+
productType: {
|
|
15
|
+
typeId: "product-type",
|
|
16
|
+
id: "fake-product-type-id",
|
|
17
|
+
},
|
|
18
|
+
version: 1,
|
|
19
|
+
id: "15fc56ba-a74e-4cf8-b4b0-bada5c101541",
|
|
20
|
+
masterData: {
|
|
21
|
+
current: {
|
|
22
|
+
name: { "nl-NL": "Dummy" },
|
|
23
|
+
slug: { "nl-NL": "Dummy" },
|
|
24
|
+
categories: [],
|
|
25
|
+
masterVariant: {
|
|
26
|
+
sku: "MYSKU",
|
|
27
|
+
id: 1,
|
|
28
|
+
prices: [
|
|
29
|
+
{
|
|
30
|
+
id: "fake-price-id",
|
|
31
|
+
value: {
|
|
32
|
+
currencyCode: "EUR",
|
|
33
|
+
centAmount: 1000,
|
|
34
|
+
type: "centPrecision",
|
|
35
|
+
fractionDigits: 2,
|
|
36
|
+
},
|
|
37
|
+
country: "NL",
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
variants: [],
|
|
42
|
+
searchKeywords: {},
|
|
43
|
+
},
|
|
44
|
+
published: false,
|
|
45
|
+
staged: {
|
|
46
|
+
name: { "nl-NL": "Dummy" },
|
|
47
|
+
slug: { "nl-NL": "Dummy" },
|
|
48
|
+
categories: [],
|
|
49
|
+
masterVariant: {
|
|
50
|
+
sku: "MYSKU",
|
|
51
|
+
id: 1,
|
|
52
|
+
prices: [
|
|
53
|
+
{
|
|
54
|
+
id: "fake-price-id",
|
|
55
|
+
value: {
|
|
56
|
+
currencyCode: "EUR",
|
|
57
|
+
centAmount: 1000,
|
|
58
|
+
type: "centPrecision",
|
|
59
|
+
fractionDigits: 2,
|
|
60
|
+
},
|
|
61
|
+
country: "NL",
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
variants: [],
|
|
66
|
+
searchKeywords: {},
|
|
67
|
+
},
|
|
68
|
+
hasStagedChanges: false,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const cart: CartDraft = {
|
|
73
|
+
anonymousId: "1234567890",
|
|
74
|
+
billingAddress: {
|
|
75
|
+
id: "1234567890",
|
|
76
|
+
country: "NL",
|
|
77
|
+
firstName: "John",
|
|
78
|
+
lastName: "Doe",
|
|
79
|
+
streetName: "Main Street",
|
|
80
|
+
streetNumber: "123",
|
|
81
|
+
postalCode: "123456",
|
|
82
|
+
},
|
|
83
|
+
country: "NL",
|
|
84
|
+
currency: "EUR",
|
|
85
|
+
customerEmail: "john.doe@example.com",
|
|
86
|
+
customLineItems: [],
|
|
87
|
+
inventoryMode: "None",
|
|
88
|
+
itemShippingAddresses: [],
|
|
89
|
+
lineItems: [
|
|
90
|
+
{
|
|
91
|
+
id: "15fc56ba-a74e-4cf8-b4b0-bada5c101541",
|
|
92
|
+
sku: "MYSKU",
|
|
93
|
+
variantId: 1,
|
|
94
|
+
quantity: 1,
|
|
95
|
+
variant: {
|
|
96
|
+
prices: [
|
|
97
|
+
{
|
|
98
|
+
id: "fake-price-id",
|
|
99
|
+
value: {
|
|
100
|
+
currencyCode: "EUR",
|
|
101
|
+
centAmount: 1000,
|
|
102
|
+
type: "centPrecision",
|
|
103
|
+
fractionDigits: 2,
|
|
104
|
+
},
|
|
105
|
+
country: "NL",
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
} as unknown as LineItem,
|
|
110
|
+
],
|
|
111
|
+
origin: "Customer",
|
|
112
|
+
shipping: [],
|
|
113
|
+
shippingAddress: {
|
|
114
|
+
id: "1234567890",
|
|
115
|
+
country: "NL",
|
|
116
|
+
firstName: "John",
|
|
117
|
+
lastName: "Doe",
|
|
118
|
+
streetName: "Main Street",
|
|
119
|
+
streetNumber: "123",
|
|
120
|
+
postalCode: "123456",
|
|
121
|
+
},
|
|
122
|
+
shippingMode: "Single",
|
|
123
|
+
taxMode: "Platform",
|
|
124
|
+
taxRoundingMode: "HalfEven",
|
|
125
|
+
taxCalculationMode: "UnitPriceLevel",
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const ctx = { projectKey: "dummy", storeKey: "dummyStore" };
|
|
129
|
+
|
|
130
|
+
const result = repository.create(ctx, cart);
|
|
131
|
+
expect(result.id).toBeDefined();
|
|
132
|
+
|
|
133
|
+
const items = repository.query(ctx);
|
|
134
|
+
expect(items.count).toBe(1);
|
|
135
|
+
|
|
136
|
+
expect(result.anonymousId).toEqual(cart.anonymousId);
|
|
137
|
+
expect(result.billingAddress).toEqual(cart.billingAddress);
|
|
138
|
+
expect(result.country).toEqual(cart.country);
|
|
139
|
+
expect(result.customerEmail).toEqual(cart.customerEmail);
|
|
140
|
+
expect(result.customerId).toEqual(cart.customerId);
|
|
141
|
+
expect(result.locale).toEqual(cart.locale);
|
|
142
|
+
expect(result.origin).toEqual(cart.origin);
|
|
143
|
+
expect(result.shipping).toEqual(cart.shipping);
|
|
144
|
+
expect(result.shippingAddress).toEqual(cart.shippingAddress);
|
|
145
|
+
expect(result.shippingMode).toEqual(cart.shippingMode);
|
|
146
|
+
expect(result.taxCalculationMode).toEqual(cart.taxCalculationMode);
|
|
147
|
+
expect(result.taxMode).toEqual(cart.taxMode);
|
|
148
|
+
expect(result.taxRoundingMode).toEqual(cart.taxRoundingMode);
|
|
149
|
+
expect(result.store?.key).toEqual(ctx.storeKey);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
@@ -39,8 +39,13 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
39
39
|
|
|
40
40
|
const resource: Writable<Cart> = {
|
|
41
41
|
...getBaseResourceProperties(),
|
|
42
|
+
anonymousId: draft.anonymousId,
|
|
43
|
+
billingAddress: draft.billingAddress
|
|
44
|
+
? createAddress(draft.billingAddress, context.projectKey, this._storage)
|
|
45
|
+
: undefined,
|
|
42
46
|
cartState: "Active",
|
|
43
47
|
country: draft.country,
|
|
48
|
+
customerEmail: draft.customerEmail,
|
|
44
49
|
customLineItems: [],
|
|
45
50
|
directDiscounts: [],
|
|
46
51
|
discountCodes: [],
|
|
@@ -58,11 +63,13 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
58
63
|
fractionDigits: 0,
|
|
59
64
|
},
|
|
60
65
|
shippingMode: "Single",
|
|
61
|
-
shippingAddress:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
shippingAddress: draft.shippingAddress
|
|
67
|
+
? createAddress(
|
|
68
|
+
draft.shippingAddress,
|
|
69
|
+
context.projectKey,
|
|
70
|
+
this._storage,
|
|
71
|
+
)
|
|
72
|
+
: undefined,
|
|
66
73
|
shipping: [],
|
|
67
74
|
origin: draft.origin ?? "Customer",
|
|
68
75
|
refusedGifts: [],
|
|
@@ -73,6 +80,9 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
73
80
|
),
|
|
74
81
|
};
|
|
75
82
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
|
|
83
|
+
resource.store = context.storeKey
|
|
84
|
+
? { typeId: "store", key: context.storeKey }
|
|
85
|
+
: undefined;
|
|
76
86
|
|
|
77
87
|
return this.saveNew(context, resource);
|
|
78
88
|
}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Cart,
|
|
3
|
+
LineItem,
|
|
4
|
+
OrderImportDraft,
|
|
5
|
+
} from "@commercetools/platform-sdk";
|
|
2
6
|
import { describe, expect, test } from "vitest";
|
|
3
7
|
import { InMemoryStorage } from "~src/storage";
|
|
4
8
|
import { OrderRepository } from "./index";
|
|
@@ -17,7 +21,14 @@ describe("Order repository", () => {
|
|
|
17
21
|
directDiscounts: [],
|
|
18
22
|
inventoryMode: "None",
|
|
19
23
|
itemShippingAddresses: [],
|
|
20
|
-
lineItems: [
|
|
24
|
+
lineItems: [
|
|
25
|
+
{
|
|
26
|
+
id: "15fc56ba-a74e-4cf8-b4b0-bada5c101541",
|
|
27
|
+
productId: "PRODUCTID",
|
|
28
|
+
variantId: 1,
|
|
29
|
+
quantity: 1,
|
|
30
|
+
} as unknown as LineItem,
|
|
31
|
+
],
|
|
21
32
|
customLineItems: [],
|
|
22
33
|
totalPrice: {
|
|
23
34
|
type: "centPrecision",
|
|
@@ -33,6 +44,90 @@ describe("Order repository", () => {
|
|
|
33
44
|
taxCalculationMode: "UnitPriceLevel",
|
|
34
45
|
refusedGifts: [],
|
|
35
46
|
origin: "Customer",
|
|
47
|
+
anonymousId: "1234567890",
|
|
48
|
+
billingAddress: {
|
|
49
|
+
id: "1234567890",
|
|
50
|
+
country: "NL",
|
|
51
|
+
firstName: "John",
|
|
52
|
+
lastName: "Doe",
|
|
53
|
+
streetName: "Main Street",
|
|
54
|
+
streetNumber: "123",
|
|
55
|
+
postalCode: "123456",
|
|
56
|
+
},
|
|
57
|
+
customerEmail: "john.doe@example.com",
|
|
58
|
+
customerGroup: {
|
|
59
|
+
id: "1234567890",
|
|
60
|
+
typeId: "customer-group",
|
|
61
|
+
},
|
|
62
|
+
customerId: "1234567890",
|
|
63
|
+
custom: {
|
|
64
|
+
type: {
|
|
65
|
+
typeId: "type",
|
|
66
|
+
id: "1234567890",
|
|
67
|
+
},
|
|
68
|
+
fields: {
|
|
69
|
+
description: "example description",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
shippingAddress: {
|
|
74
|
+
id: "1234567890",
|
|
75
|
+
country: "NL",
|
|
76
|
+
firstName: "John",
|
|
77
|
+
lastName: "Doe",
|
|
78
|
+
streetName: "Main Street",
|
|
79
|
+
streetNumber: "123",
|
|
80
|
+
postalCode: "123456",
|
|
81
|
+
},
|
|
82
|
+
shippingInfo: {
|
|
83
|
+
shippingMethodName: "Standard Shipping",
|
|
84
|
+
price: {
|
|
85
|
+
type: "centPrecision",
|
|
86
|
+
currencyCode: "EUR",
|
|
87
|
+
centAmount: 1000,
|
|
88
|
+
fractionDigits: 2,
|
|
89
|
+
},
|
|
90
|
+
shippingRate: {
|
|
91
|
+
price: {
|
|
92
|
+
type: "centPrecision",
|
|
93
|
+
currencyCode: "EUR",
|
|
94
|
+
centAmount: 1000,
|
|
95
|
+
fractionDigits: 2,
|
|
96
|
+
},
|
|
97
|
+
tiers: [],
|
|
98
|
+
},
|
|
99
|
+
shippingMethodState: "Shipped",
|
|
100
|
+
},
|
|
101
|
+
taxedPrice: {
|
|
102
|
+
totalNet: {
|
|
103
|
+
type: "centPrecision",
|
|
104
|
+
currencyCode: "EUR",
|
|
105
|
+
centAmount: 1000,
|
|
106
|
+
fractionDigits: 2,
|
|
107
|
+
},
|
|
108
|
+
taxPortions: [],
|
|
109
|
+
totalGross: {
|
|
110
|
+
type: "centPrecision",
|
|
111
|
+
currencyCode: "EUR",
|
|
112
|
+
centAmount: 1210,
|
|
113
|
+
fractionDigits: 2,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
taxedShippingPrice: {
|
|
117
|
+
totalNet: {
|
|
118
|
+
type: "centPrecision",
|
|
119
|
+
currencyCode: "EUR",
|
|
120
|
+
centAmount: 100,
|
|
121
|
+
fractionDigits: 2,
|
|
122
|
+
},
|
|
123
|
+
taxPortions: [],
|
|
124
|
+
totalGross: {
|
|
125
|
+
type: "centPrecision",
|
|
126
|
+
currencyCode: "EUR",
|
|
127
|
+
centAmount: 121,
|
|
128
|
+
fractionDigits: 2,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
36
131
|
};
|
|
37
132
|
|
|
38
133
|
storage.add("dummy", "cart", cart);
|
|
@@ -49,50 +144,37 @@ describe("Order repository", () => {
|
|
|
49
144
|
|
|
50
145
|
const items = repository.query(ctx);
|
|
51
146
|
expect(items.count).toBe(1);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test("create from cart - in store", async () => {
|
|
55
|
-
const cart: Cart = {
|
|
56
|
-
id: "b3875a58-4ab2-4aaa-b399-184ce7561c27",
|
|
57
|
-
version: 1,
|
|
58
|
-
createdAt: "2021-09-02T12:23:30.036Z",
|
|
59
|
-
lastModifiedAt: "2021-09-02T12:23:30.546Z",
|
|
60
|
-
discountCodes: [],
|
|
61
|
-
directDiscounts: [],
|
|
62
|
-
inventoryMode: "None",
|
|
63
|
-
itemShippingAddresses: [],
|
|
64
|
-
lineItems: [],
|
|
65
|
-
customLineItems: [],
|
|
66
|
-
totalPrice: {
|
|
67
|
-
type: "centPrecision",
|
|
68
|
-
currencyCode: "EUR",
|
|
69
|
-
centAmount: 10000,
|
|
70
|
-
fractionDigits: 2,
|
|
71
|
-
},
|
|
72
|
-
cartState: "Active",
|
|
73
|
-
shippingMode: "Single",
|
|
74
|
-
shipping: [],
|
|
75
|
-
taxMode: "Platform",
|
|
76
|
-
taxRoundingMode: "HalfEven",
|
|
77
|
-
taxCalculationMode: "UnitPriceLevel",
|
|
78
|
-
refusedGifts: [],
|
|
79
|
-
origin: "Customer",
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
storage.add("dummy", "cart", cart);
|
|
83
147
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
94
|
-
expect(result.
|
|
95
|
-
expect(result.
|
|
148
|
+
expect(result.orderNumber).not.toBeUndefined();
|
|
149
|
+
expect(result.anonymousId).toEqual(cart.anonymousId);
|
|
150
|
+
expect(result.billingAddress).toEqual(cart.billingAddress);
|
|
151
|
+
expect(result.cart?.id).toEqual(cart.id);
|
|
152
|
+
expect(result.country).toEqual(cart.country);
|
|
153
|
+
expect(result.custom).toEqual(cart.custom);
|
|
154
|
+
expect(result.customerEmail).toEqual(cart.customerEmail);
|
|
155
|
+
expect(result.customerGroup).toEqual(cart.customerGroup);
|
|
156
|
+
expect(result.customerId).toEqual(cart.customerId);
|
|
157
|
+
expect(result.customLineItems).toEqual(cart.customLineItems);
|
|
158
|
+
expect(result.directDiscounts).toEqual(cart.directDiscounts);
|
|
159
|
+
expect(result.discountCodes).toEqual(cart.discountCodes);
|
|
160
|
+
expect(result.discountOnTotalPrice).toEqual(cart.discountOnTotalPrice);
|
|
161
|
+
expect(result.lineItems).toEqual(cart.lineItems);
|
|
162
|
+
expect(result.locale).toEqual(cart.locale);
|
|
163
|
+
expect(result.orderState).toEqual("Open");
|
|
164
|
+
expect(result.origin).toEqual(cart.origin);
|
|
165
|
+
expect(result.paymentInfo).toEqual(cart.paymentInfo);
|
|
166
|
+
expect(result.refusedGifts).toEqual(cart.refusedGifts);
|
|
167
|
+
expect(result.shipping).toEqual(cart.shipping);
|
|
168
|
+
expect(result.shippingAddress).toEqual(cart.shippingAddress);
|
|
169
|
+
expect(result.shippingMode).toEqual(cart.shippingMode);
|
|
170
|
+
expect(result.syncInfo).toEqual([]);
|
|
171
|
+
expect(result.taxCalculationMode).toEqual(cart.taxCalculationMode);
|
|
172
|
+
expect(result.taxedPrice).toEqual(cart.taxedPrice);
|
|
173
|
+
expect(result.taxedShippingPrice).toEqual(cart.taxedShippingPrice);
|
|
174
|
+
expect(result.taxMode).toEqual(cart.taxMode);
|
|
175
|
+
expect(result.taxRoundingMode).toEqual(cart.taxRoundingMode);
|
|
176
|
+
expect(result.totalPrice).toEqual(cart.totalPrice);
|
|
177
|
+
expect(result.store).toEqual(cart.store);
|
|
96
178
|
});
|
|
97
179
|
|
|
98
180
|
test("import exiting product", async () => {
|
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
} from "@commercetools/platform-sdk";
|
|
16
16
|
import assert from "assert";
|
|
17
17
|
import { CommercetoolsError } from "~src/exceptions";
|
|
18
|
-
import { getBaseResourceProperties } from "~src/helpers";
|
|
18
|
+
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
|
|
19
19
|
import { AbstractStorage } from "~src/storage/abstract";
|
|
20
20
|
import {
|
|
21
21
|
AbstractResourceRepository,
|
|
@@ -65,25 +65,37 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
65
65
|
|
|
66
66
|
const resource: Order = {
|
|
67
67
|
...getBaseResourceProperties(),
|
|
68
|
-
|
|
68
|
+
anonymousId: cart.anonymousId,
|
|
69
|
+
billingAddress: cart.billingAddress,
|
|
69
70
|
cart: cartReference,
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
country: cart.country,
|
|
72
|
+
custom: cart.custom,
|
|
73
|
+
customerEmail: cart.customerEmail,
|
|
74
|
+
customerGroup: cart.customerGroup,
|
|
75
|
+
customerId: cart.customerId,
|
|
72
76
|
customLineItems: [],
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
directDiscounts: cart.directDiscounts,
|
|
78
|
+
discountCodes: cart.discountCodes,
|
|
79
|
+
discountOnTotalPrice: cart.discountOnTotalPrice,
|
|
80
|
+
lastMessageSequenceNumber: 0,
|
|
81
|
+
lineItems: cart.lineItems,
|
|
82
|
+
locale: cart.locale,
|
|
83
|
+
orderNumber: orderNumber ?? generateRandomString(10),
|
|
84
|
+
orderState: "Open",
|
|
75
85
|
origin: "Customer",
|
|
76
|
-
|
|
77
|
-
|
|
86
|
+
paymentInfo: cart.paymentInfo,
|
|
87
|
+
refusedGifts: [],
|
|
78
88
|
shipping: cart.shipping,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
shippingAddress: cart.shippingAddress,
|
|
90
|
+
shippingMode: cart.shippingMode,
|
|
91
|
+
syncInfo: [],
|
|
92
|
+
taxCalculationMode: cart.taxCalculationMode,
|
|
93
|
+
taxedPrice: cart.taxedPrice,
|
|
94
|
+
taxedShippingPrice: cart.taxedShippingPrice,
|
|
95
|
+
taxMode: cart.taxMode,
|
|
96
|
+
taxRoundingMode: cart.taxRoundingMode,
|
|
97
|
+
totalPrice: cart.totalPrice,
|
|
98
|
+
store: cart.store,
|
|
87
99
|
};
|
|
88
100
|
return this.saveNew(context, resource);
|
|
89
101
|
}
|