@reactionary/provider-commercetools 0.0.42 → 0.0.51
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/README.md +11 -0
- package/core/client.js +130 -72
- package/core/initialize.js +4 -4
- package/package.json +4 -3
- package/providers/cart.provider.js +76 -96
- package/providers/category.provider.js +29 -32
- package/providers/checkout.provider.js +457 -0
- package/providers/identity.provider.js +11 -64
- package/providers/index.js +4 -0
- package/providers/inventory.provider.js +23 -22
- package/providers/order.provider.js +118 -0
- package/providers/price.provider.js +48 -36
- package/providers/product.provider.js +28 -16
- package/providers/profile.provider.js +30 -0
- package/providers/search.provider.js +9 -12
- package/providers/store.provider.js +42 -0
- package/schema/capabilities.schema.js +4 -2
- package/schema/commercetools.schema.js +4 -4
- package/schema/configuration.schema.js +3 -1
- package/src/core/client.d.ts +70 -10
- package/src/core/initialize.d.ts +9 -3
- package/src/providers/cart.provider.d.ts +34 -23
- package/src/providers/category.provider.d.ts +13 -13
- package/src/providers/checkout.provider.d.ts +67 -0
- package/src/providers/identity.provider.d.ts +7 -7
- package/src/providers/index.d.ts +4 -0
- package/src/providers/inventory.provider.d.ts +8 -6
- package/src/providers/order.provider.d.ts +11 -0
- package/src/providers/price.provider.d.ts +12 -8
- package/src/providers/product.provider.d.ts +10 -8
- package/src/providers/profile.provider.d.ts +14 -0
- package/src/providers/search.provider.d.ts +6 -6
- package/src/providers/store.provider.d.ts +13 -0
- package/src/schema/capabilities.schema.d.ts +6 -4
- package/src/schema/commercetools.schema.d.ts +3 -3
- package/src/schema/configuration.schema.d.ts +22 -0
- package/providers/cart-payment.provider.js +0 -152
- package/src/providers/cart-payment.provider.d.ts +0 -16
package/README.md
CHANGED
|
@@ -11,6 +11,14 @@ Run `nx build provider-commercetools` to build the library.
|
|
|
11
11
|
Run `nx test provider-commercetools` to execute the unit tests via [Jest](https://jestjs.io).
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
# ASSUMPTIONS for backend config
|
|
15
|
+
|
|
16
|
+
- You will have 2 different channels for prices, one called `Offer Price` and one called `List Price`
|
|
17
|
+
- ProductVariants will all have unique SKU values.
|
|
18
|
+
- Your Supply Channels double as Store Locations
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
14
22
|
## TODO List
|
|
15
23
|
|
|
16
24
|
### Core
|
|
@@ -25,3 +33,6 @@ Run `nx test provider-commercetools` to execute the unit tests via [Jest](https:
|
|
|
25
33
|
### Inventory
|
|
26
34
|
- [ ] Be traced and cached
|
|
27
35
|
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
package/core/client.js
CHANGED
|
@@ -1,99 +1,149 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ClientBuilder
|
|
3
|
+
} from "@commercetools/ts-client";
|
|
2
4
|
import { createApiBuilderFromCtpClient } from "@commercetools/platform-sdk";
|
|
3
5
|
import { randomUUID } from "crypto";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
import {
|
|
7
|
+
AnonymousIdentitySchema,
|
|
8
|
+
GuestIdentitySchema,
|
|
9
|
+
RegisteredIdentitySchema
|
|
10
|
+
} from "@reactionary/core";
|
|
11
|
+
import * as crypto from "crypto";
|
|
12
|
+
import createDebug from "debug";
|
|
13
|
+
const debug = createDebug("commercetools:debug");
|
|
14
|
+
class RequestContextTokenCache {
|
|
15
|
+
constructor(context) {
|
|
16
|
+
this.context = context;
|
|
13
17
|
}
|
|
14
|
-
async
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const url = `${this.config.authUrl}/oauth/${this.config.projectKey}/customers/token?${queryParams.toString()}`;
|
|
21
|
-
const headers = {
|
|
22
|
-
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
18
|
+
async get(tokenCacheOptions) {
|
|
19
|
+
const identity = this.context.identity;
|
|
20
|
+
return {
|
|
21
|
+
refreshToken: identity.refresh_token,
|
|
22
|
+
token: identity.token || "",
|
|
23
|
+
expirationTime: identity.expiry.getTime()
|
|
23
24
|
};
|
|
24
|
-
const remote = await fetch(url, { method: "POST", headers });
|
|
25
|
-
const json = await remote.json();
|
|
26
|
-
return json;
|
|
27
25
|
}
|
|
28
|
-
async
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const headers = {
|
|
34
|
-
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
35
|
-
};
|
|
36
|
-
const remote = await fetch(url, { method: "POST", headers });
|
|
37
|
-
const json = await remote.json();
|
|
38
|
-
return json;
|
|
26
|
+
async set(cache, tokenCacheOptions) {
|
|
27
|
+
const identity = this.context.identity;
|
|
28
|
+
identity.refresh_token = cache.refreshToken;
|
|
29
|
+
identity.token = cache.token;
|
|
30
|
+
identity.expiry = new Date(cache.expirationTime);
|
|
39
31
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
});
|
|
45
|
-
const url = `${this.config.authUrl}/oauth/token/revoke?${queryParams.toString()}`;
|
|
46
|
-
const headers = {
|
|
47
|
-
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
48
|
-
};
|
|
49
|
-
const remote = await fetch(url, { method: "POST", headers });
|
|
50
|
-
return remote;
|
|
32
|
+
}
|
|
33
|
+
class CommercetoolsClient {
|
|
34
|
+
constructor(config) {
|
|
35
|
+
this.config = config;
|
|
51
36
|
}
|
|
52
|
-
async
|
|
53
|
-
|
|
54
|
-
token
|
|
55
|
-
});
|
|
56
|
-
const url = `${this.config.authUrl}/oauth/introspect?` + queryParams;
|
|
57
|
-
const headers = {
|
|
58
|
-
Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
|
|
59
|
-
};
|
|
60
|
-
const remote = await fetch(url, { method: "POST", headers });
|
|
61
|
-
const json = await remote.json();
|
|
62
|
-
return json;
|
|
37
|
+
async getClient(reqCtx) {
|
|
38
|
+
return this.createClient(reqCtx);
|
|
63
39
|
}
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const builder = this.createBaseClientBuilder().withClientCredentialsFlow({
|
|
40
|
+
async register(username, password, reqCtx) {
|
|
41
|
+
const registrationBuilder = this.createBaseClientBuilder().withAnonymousSessionFlow({
|
|
67
42
|
host: this.config.authUrl,
|
|
68
43
|
projectKey: this.config.projectKey,
|
|
69
44
|
credentials: {
|
|
70
45
|
clientId: this.config.clientId,
|
|
71
46
|
clientSecret: this.config.clientSecret
|
|
72
47
|
},
|
|
73
|
-
scopes:
|
|
48
|
+
scopes: this.config.scopes
|
|
74
49
|
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
createClientWithToken(token) {
|
|
78
|
-
const builder = this.createBaseClientBuilder().withExistingTokenFlow(
|
|
79
|
-
`Bearer ${token}`,
|
|
80
|
-
{ force: true }
|
|
50
|
+
const registrationClient = createApiBuilderFromCtpClient(
|
|
51
|
+
registrationBuilder.build()
|
|
81
52
|
);
|
|
53
|
+
const registration = await registrationClient.withProjectKey({ projectKey: this.config.projectKey }).me().signup().post({
|
|
54
|
+
body: {
|
|
55
|
+
email: username,
|
|
56
|
+
password
|
|
57
|
+
}
|
|
58
|
+
}).execute();
|
|
59
|
+
const login = await this.login(username, password, reqCtx);
|
|
60
|
+
return login;
|
|
61
|
+
}
|
|
62
|
+
async login(username, password, reqCtx) {
|
|
63
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
64
|
+
const loginBuilder = this.createBaseClientBuilder().withPasswordFlow({
|
|
65
|
+
host: this.config.authUrl,
|
|
66
|
+
projectKey: this.config.projectKey,
|
|
67
|
+
credentials: {
|
|
68
|
+
clientId: this.config.clientId,
|
|
69
|
+
clientSecret: this.config.clientSecret,
|
|
70
|
+
user: { username, password }
|
|
71
|
+
},
|
|
72
|
+
tokenCache: cache,
|
|
73
|
+
scopes: this.config.scopes
|
|
74
|
+
});
|
|
75
|
+
const loginClient = createApiBuilderFromCtpClient(loginBuilder.build());
|
|
76
|
+
const login = await loginClient.withProjectKey({ projectKey: this.config.projectKey }).me().login().post({
|
|
77
|
+
body: {
|
|
78
|
+
email: username,
|
|
79
|
+
password
|
|
80
|
+
}
|
|
81
|
+
}).execute();
|
|
82
|
+
reqCtx.identity = RegisteredIdentitySchema.parse({
|
|
83
|
+
...reqCtx.identity,
|
|
84
|
+
type: "Registered",
|
|
85
|
+
logonId: username,
|
|
86
|
+
id: {
|
|
87
|
+
userId: login.body.customer.id
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return reqCtx.identity;
|
|
91
|
+
}
|
|
92
|
+
async logout(reqCtx) {
|
|
93
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
94
|
+
await cache.set({ token: "", refreshToken: "", expirationTime: 0 });
|
|
95
|
+
reqCtx.identity = AnonymousIdentitySchema.parse({});
|
|
96
|
+
return reqCtx.identity;
|
|
97
|
+
}
|
|
98
|
+
createClient(reqCtx) {
|
|
99
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
100
|
+
if (reqCtx.identity.type === "Anonymous") {
|
|
101
|
+
reqCtx.identity = GuestIdentitySchema.parse({
|
|
102
|
+
id: {
|
|
103
|
+
userId: crypto.randomUUID().toString()
|
|
104
|
+
},
|
|
105
|
+
type: "Guest"
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const identity = reqCtx.identity;
|
|
109
|
+
let builder = this.createBaseClientBuilder(reqCtx);
|
|
110
|
+
if (!identity.token || !identity.refresh_token) {
|
|
111
|
+
builder = builder.withAnonymousSessionFlow({
|
|
112
|
+
host: this.config.authUrl,
|
|
113
|
+
projectKey: this.config.projectKey,
|
|
114
|
+
credentials: {
|
|
115
|
+
clientId: this.config.clientId,
|
|
116
|
+
clientSecret: this.config.clientSecret,
|
|
117
|
+
anonymousId: identity.id.userId
|
|
118
|
+
},
|
|
119
|
+
tokenCache: cache
|
|
120
|
+
});
|
|
121
|
+
} else {
|
|
122
|
+
builder = builder.withRefreshTokenFlow({
|
|
123
|
+
credentials: {
|
|
124
|
+
clientId: this.config.clientId,
|
|
125
|
+
clientSecret: this.config.clientSecret
|
|
126
|
+
},
|
|
127
|
+
host: this.config.authUrl,
|
|
128
|
+
projectKey: this.config.projectKey,
|
|
129
|
+
refreshToken: identity.refresh_token || "",
|
|
130
|
+
tokenCache: cache
|
|
131
|
+
});
|
|
132
|
+
}
|
|
82
133
|
return createApiBuilderFromCtpClient(builder.build());
|
|
83
134
|
}
|
|
84
|
-
createBaseClientBuilder() {
|
|
85
|
-
|
|
135
|
+
createBaseClientBuilder(reqCtx) {
|
|
136
|
+
let builder = new ClientBuilder().withProjectKey(this.config.projectKey).withQueueMiddleware({
|
|
86
137
|
concurrency: 20
|
|
87
138
|
}).withConcurrentModificationMiddleware({
|
|
88
139
|
concurrentModificationHandlerFn: (version, request) => {
|
|
89
|
-
console.log(
|
|
140
|
+
console.log(
|
|
141
|
+
`Concurrent modification error, retry with version ${version}`
|
|
142
|
+
);
|
|
90
143
|
const body = request.body;
|
|
91
144
|
body["version"] = version;
|
|
92
145
|
return Promise.resolve(body);
|
|
93
146
|
}
|
|
94
|
-
}).withCorrelationIdMiddleware({
|
|
95
|
-
// ideally this would be pushed in as part of the session context, so we can trace it end-to-end
|
|
96
|
-
generate: () => `REACTIONARY-${randomUUID()}`
|
|
97
147
|
}).withHttpMiddleware({
|
|
98
148
|
retryConfig: {
|
|
99
149
|
backoff: true,
|
|
@@ -109,9 +159,17 @@ class CommercetoolsClient {
|
|
|
109
159
|
host: this.config.apiUrl,
|
|
110
160
|
httpClient: fetch
|
|
111
161
|
});
|
|
162
|
+
const correlationId = reqCtx?.correlationId || "REACTIONARY-" + (typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : randomUUID());
|
|
163
|
+
builder = builder.withCorrelationIdMiddleware({
|
|
164
|
+
generate: () => correlationId
|
|
165
|
+
});
|
|
166
|
+
if (debug.enabled) {
|
|
167
|
+
builder = builder.withLoggerMiddleware();
|
|
168
|
+
}
|
|
112
169
|
return builder;
|
|
113
170
|
}
|
|
114
171
|
}
|
|
115
172
|
export {
|
|
116
|
-
CommercetoolsClient
|
|
173
|
+
CommercetoolsClient,
|
|
174
|
+
RequestContextTokenCache
|
|
117
175
|
};
|
package/core/initialize.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
ProductSchema,
|
|
7
7
|
SearchResultSchema,
|
|
8
8
|
CategorySchema,
|
|
9
|
-
|
|
9
|
+
CheckoutSchema
|
|
10
10
|
} from "@reactionary/core";
|
|
11
11
|
import { CommercetoolsSearchProvider } from "../providers/search.provider";
|
|
12
12
|
import { CommercetoolsProductProvider } from "../providers/product.provider";
|
|
@@ -15,7 +15,7 @@ import { CommercetoolsCartProvider } from "../providers/cart.provider";
|
|
|
15
15
|
import { CommercetoolsInventoryProvider } from "../providers/inventory.provider";
|
|
16
16
|
import { CommercetoolsPriceProvider } from "../providers/price.provider";
|
|
17
17
|
import { CommercetoolsCategoryProvider } from "../providers/category.provider";
|
|
18
|
-
import {
|
|
18
|
+
import { CommercetoolsCheckoutProvider } from "../providers";
|
|
19
19
|
function withCommercetoolsCapabilities(configuration, capabilities) {
|
|
20
20
|
return (cache) => {
|
|
21
21
|
const client = {};
|
|
@@ -40,8 +40,8 @@ function withCommercetoolsCapabilities(configuration, capabilities) {
|
|
|
40
40
|
if (capabilities.category) {
|
|
41
41
|
client.category = new CommercetoolsCategoryProvider(configuration, CategorySchema, cache);
|
|
42
42
|
}
|
|
43
|
-
if (capabilities.
|
|
44
|
-
client.
|
|
43
|
+
if (capabilities.checkout) {
|
|
44
|
+
client.checkout = new CommercetoolsCheckoutProvider(configuration, CheckoutSchema, cache);
|
|
45
45
|
}
|
|
46
46
|
return client;
|
|
47
47
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-commercetools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.51",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.0.
|
|
8
|
-
"@reactionary/otel": "0.0.
|
|
7
|
+
"@reactionary/core": "0.0.51",
|
|
8
|
+
"@reactionary/otel": "0.0.51",
|
|
9
|
+
"debug": "^4.4.3",
|
|
9
10
|
"zod": "4.1.9",
|
|
10
11
|
"@commercetools/ts-client": "^4.2.1",
|
|
11
12
|
"@commercetools/platform-sdk": "^8.8.0"
|
|
@@ -24,21 +24,21 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
24
24
|
super(schema, cache);
|
|
25
25
|
this.config = config;
|
|
26
26
|
}
|
|
27
|
-
async getById(payload,
|
|
27
|
+
async getById(payload, reqCtx) {
|
|
28
28
|
try {
|
|
29
|
-
const client = this.getClient(
|
|
29
|
+
const client = await this.getClient(reqCtx);
|
|
30
30
|
const ctId = payload.cart;
|
|
31
|
-
const remote = await client.withId({ ID: ctId.key }).get().execute();
|
|
32
|
-
return this.parseSingle(remote.body,
|
|
31
|
+
const remote = await client.carts.withId({ ID: ctId.key }).get().execute();
|
|
32
|
+
return this.parseSingle(remote.body, reqCtx);
|
|
33
33
|
} catch (e) {
|
|
34
34
|
return this.createEmptyCart();
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
async add(payload,
|
|
38
|
-
const client = this.getClient(
|
|
37
|
+
async add(payload, reqCtx) {
|
|
38
|
+
const client = await this.getClient(reqCtx);
|
|
39
39
|
let cartIdentifier = payload.cart;
|
|
40
40
|
if (!cartIdentifier.key) {
|
|
41
|
-
cartIdentifier = await this.createCart(
|
|
41
|
+
cartIdentifier = await this.createCart(reqCtx);
|
|
42
42
|
}
|
|
43
43
|
return this.applyActions(
|
|
44
44
|
cartIdentifier,
|
|
@@ -52,10 +52,10 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
52
52
|
action: "recalculate"
|
|
53
53
|
}
|
|
54
54
|
],
|
|
55
|
-
|
|
55
|
+
reqCtx
|
|
56
56
|
);
|
|
57
57
|
}
|
|
58
|
-
async remove(payload,
|
|
58
|
+
async remove(payload, reqCtx) {
|
|
59
59
|
return this.applyActions(
|
|
60
60
|
payload.cart,
|
|
61
61
|
[
|
|
@@ -67,12 +67,12 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
67
67
|
action: "recalculate"
|
|
68
68
|
}
|
|
69
69
|
],
|
|
70
|
-
|
|
70
|
+
reqCtx
|
|
71
71
|
);
|
|
72
72
|
}
|
|
73
|
-
async changeQuantity(payload,
|
|
73
|
+
async changeQuantity(payload, reqCtx) {
|
|
74
74
|
if (payload.quantity === 0) {
|
|
75
|
-
return this.getById({ cart: payload.cart },
|
|
75
|
+
return this.getById({ cart: payload.cart }, reqCtx);
|
|
76
76
|
}
|
|
77
77
|
return this.applyActions(
|
|
78
78
|
payload.cart,
|
|
@@ -86,19 +86,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
86
86
|
action: "recalculate"
|
|
87
87
|
}
|
|
88
88
|
],
|
|
89
|
-
|
|
89
|
+
reqCtx
|
|
90
90
|
);
|
|
91
91
|
}
|
|
92
|
-
async getActiveCartId(
|
|
93
|
-
const client = this.getClient(
|
|
92
|
+
async getActiveCartId(reqCtx) {
|
|
93
|
+
const client = await this.getClient(reqCtx);
|
|
94
94
|
try {
|
|
95
|
-
const carts = await client.
|
|
96
|
-
queryArgs: {
|
|
97
|
-
limit: 1,
|
|
98
|
-
sort: "lastModifiedAt desc",
|
|
99
|
-
where: 'cartState="Active"'
|
|
100
|
-
}
|
|
101
|
-
}).execute();
|
|
95
|
+
const carts = await client.activeCart.get().execute();
|
|
102
96
|
return CommercetoolsCartIdentifierSchema.parse({
|
|
103
97
|
key: carts.body.id,
|
|
104
98
|
version: carts.body.version || 0
|
|
@@ -110,22 +104,22 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
110
104
|
});
|
|
111
105
|
}
|
|
112
106
|
}
|
|
113
|
-
async deleteCart(payload,
|
|
114
|
-
const client = this.getClient(
|
|
107
|
+
async deleteCart(payload, reqCtx) {
|
|
108
|
+
const client = await this.getClient(reqCtx);
|
|
115
109
|
if (payload.cart.key) {
|
|
116
110
|
const ctId = payload.cart;
|
|
117
|
-
await client.withId({ ID: ctId.key }).delete({
|
|
111
|
+
await client.carts.withId({ ID: ctId.key }).delete({
|
|
118
112
|
queryArgs: {
|
|
119
113
|
version: ctId.version,
|
|
120
114
|
dataErasure: false
|
|
121
115
|
}
|
|
122
116
|
}).execute();
|
|
123
117
|
}
|
|
124
|
-
const activeCartId = await this.getActiveCartId(
|
|
125
|
-
return this.getById({ cart: activeCartId },
|
|
118
|
+
const activeCartId = await this.getActiveCartId(reqCtx);
|
|
119
|
+
return this.getById({ cart: activeCartId }, reqCtx);
|
|
126
120
|
}
|
|
127
|
-
setShippingInfo(payload,
|
|
128
|
-
const client = this.getClient(
|
|
121
|
+
async setShippingInfo(payload, reqCtx) {
|
|
122
|
+
const client = await this.getClient(reqCtx);
|
|
129
123
|
const ctId = payload.cart;
|
|
130
124
|
const actions = new Array();
|
|
131
125
|
if (payload.shippingMethod) {
|
|
@@ -141,7 +135,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
141
135
|
actions.push({
|
|
142
136
|
action: "setShippingAddress",
|
|
143
137
|
address: {
|
|
144
|
-
country: payload.shippingAddress.countryCode || "US",
|
|
138
|
+
country: payload.shippingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US",
|
|
145
139
|
firstName: payload.shippingAddress.firstName,
|
|
146
140
|
lastName: payload.shippingAddress.lastName,
|
|
147
141
|
city: payload.shippingAddress.city,
|
|
@@ -151,9 +145,9 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
151
145
|
}
|
|
152
146
|
});
|
|
153
147
|
}
|
|
154
|
-
return this.applyActions(payload.cart, actions,
|
|
148
|
+
return this.applyActions(payload.cart, actions, reqCtx);
|
|
155
149
|
}
|
|
156
|
-
setBillingAddress(payload,
|
|
150
|
+
setBillingAddress(payload, reqCtx) {
|
|
157
151
|
return this.applyActions(
|
|
158
152
|
payload.cart,
|
|
159
153
|
[
|
|
@@ -162,7 +156,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
162
156
|
address: {
|
|
163
157
|
email: payload.notificationEmailAddress,
|
|
164
158
|
mobile: payload.notificationPhoneNumber,
|
|
165
|
-
country: payload.billingAddress.countryCode || "US",
|
|
159
|
+
country: payload.billingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US",
|
|
166
160
|
firstName: payload.billingAddress.firstName,
|
|
167
161
|
lastName: payload.billingAddress.lastName,
|
|
168
162
|
city: payload.billingAddress.city,
|
|
@@ -177,13 +171,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
177
171
|
},
|
|
178
172
|
{
|
|
179
173
|
action: "setCountry",
|
|
180
|
-
country: payload.billingAddress.countryCode || "US"
|
|
174
|
+
country: payload.billingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US"
|
|
181
175
|
}
|
|
182
176
|
],
|
|
183
|
-
|
|
177
|
+
reqCtx
|
|
184
178
|
);
|
|
185
179
|
}
|
|
186
|
-
applyCouponCode(payload,
|
|
180
|
+
applyCouponCode(payload, reqCtx) {
|
|
187
181
|
return this.applyActions(
|
|
188
182
|
payload.cart,
|
|
189
183
|
[
|
|
@@ -195,10 +189,10 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
195
189
|
action: "recalculate"
|
|
196
190
|
}
|
|
197
191
|
],
|
|
198
|
-
|
|
192
|
+
reqCtx
|
|
199
193
|
);
|
|
200
194
|
}
|
|
201
|
-
removeCouponCode(payload,
|
|
195
|
+
removeCouponCode(payload, reqCtx) {
|
|
202
196
|
return this.applyActions(
|
|
203
197
|
payload.cart,
|
|
204
198
|
[
|
|
@@ -213,19 +207,16 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
213
207
|
action: "recalculate"
|
|
214
208
|
}
|
|
215
209
|
],
|
|
216
|
-
|
|
210
|
+
reqCtx
|
|
217
211
|
);
|
|
218
212
|
}
|
|
219
|
-
async checkout(payload,
|
|
220
|
-
const client = this.
|
|
213
|
+
async checkout(payload, reqCtx) {
|
|
214
|
+
const client = await this.getClient(reqCtx);
|
|
221
215
|
const ctId = payload.cart;
|
|
222
|
-
const orderResponse = await client.post({
|
|
216
|
+
const orderResponse = await client.orders.post({
|
|
223
217
|
body: {
|
|
224
218
|
version: ctId.version,
|
|
225
|
-
|
|
226
|
-
typeId: "cart",
|
|
227
|
-
id: ctId.key
|
|
228
|
-
}
|
|
219
|
+
id: ctId.key
|
|
229
220
|
}
|
|
230
221
|
}).execute();
|
|
231
222
|
return CommercetoolsOrderIdentifierSchema.parse({
|
|
@@ -233,14 +224,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
233
224
|
version: orderResponse.body.version || 0
|
|
234
225
|
});
|
|
235
226
|
}
|
|
236
|
-
async changeCurrency(payload,
|
|
237
|
-
const client = this.getClient(
|
|
238
|
-
const currentCart = await client.withId({ ID: payload.cart.key }).get().execute();
|
|
239
|
-
const newCart = await client.post({
|
|
227
|
+
async changeCurrency(payload, reqCtx) {
|
|
228
|
+
const client = await this.getClient(reqCtx);
|
|
229
|
+
const currentCart = await client.carts.withId({ ID: payload.cart.key }).get().execute();
|
|
230
|
+
const newCart = await client.carts.post({
|
|
240
231
|
body: {
|
|
241
232
|
currency: payload.newCurrency,
|
|
242
|
-
|
|
243
|
-
locale: session.languageContext.locale
|
|
233
|
+
locale: reqCtx.languageContext.locale
|
|
244
234
|
}
|
|
245
235
|
}).execute();
|
|
246
236
|
const newCartId = CommercetoolsCartIdentifierSchema.parse({
|
|
@@ -262,9 +252,9 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
262
252
|
action: "recalculate"
|
|
263
253
|
}
|
|
264
254
|
],
|
|
265
|
-
|
|
255
|
+
reqCtx
|
|
266
256
|
);
|
|
267
|
-
await client.withId({ ID: payload.cart.key }).delete({
|
|
257
|
+
await client.carts.withId({ ID: payload.cart.key }).delete({
|
|
268
258
|
queryArgs: {
|
|
269
259
|
version: currentCart.body.version || 0,
|
|
270
260
|
dataErasure: false
|
|
@@ -272,13 +262,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
272
262
|
}).execute();
|
|
273
263
|
return response;
|
|
274
264
|
}
|
|
275
|
-
async createCart(
|
|
276
|
-
const client = this.getClient(
|
|
277
|
-
const response = await client.post({
|
|
265
|
+
async createCart(reqCtx) {
|
|
266
|
+
const client = await this.getClient(reqCtx);
|
|
267
|
+
const response = await client.carts.post({
|
|
278
268
|
body: {
|
|
279
|
-
currency:
|
|
280
|
-
country:
|
|
281
|
-
locale:
|
|
269
|
+
currency: reqCtx.languageContext.currencyCode || "USD",
|
|
270
|
+
country: reqCtx.taxJurisdiction.countryCode || "US",
|
|
271
|
+
locale: reqCtx.languageContext.locale
|
|
282
272
|
}
|
|
283
273
|
}).execute();
|
|
284
274
|
return CommercetoolsCartIdentifierSchema.parse({
|
|
@@ -286,42 +276,35 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
286
276
|
version: response.body.version || 0
|
|
287
277
|
});
|
|
288
278
|
}
|
|
289
|
-
async applyActions(cart, actions,
|
|
290
|
-
const client = this.getClient(
|
|
279
|
+
async applyActions(cart, actions, reqCtx) {
|
|
280
|
+
const client = await this.getClient(reqCtx);
|
|
291
281
|
const ctId = cart;
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
282
|
+
try {
|
|
283
|
+
const response = await client.carts.withId({ ID: ctId.key }).post({
|
|
284
|
+
body: {
|
|
285
|
+
version: ctId.version,
|
|
286
|
+
actions
|
|
287
|
+
}
|
|
288
|
+
}).execute();
|
|
289
|
+
if (response.error) {
|
|
290
|
+
console.error(response.error);
|
|
296
291
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
(x) => x.service === "commercetools"
|
|
303
|
-
)?.token;
|
|
304
|
-
const client = new CommercetoolsClient(this.config).getClient(token);
|
|
305
|
-
const cartClient = client.withProjectKey({ projectKey: this.config.projectKey }).carts();
|
|
306
|
-
return cartClient;
|
|
307
|
-
}
|
|
308
|
-
getOrderClient(session) {
|
|
309
|
-
const token = session.identity.keyring.find(
|
|
310
|
-
(x) => x.service === "commercetools"
|
|
311
|
-
)?.token;
|
|
312
|
-
const client = new CommercetoolsClient(this.config).getClient(token);
|
|
313
|
-
const orderClient = client.withProjectKey({ projectKey: this.config.projectKey }).orders();
|
|
314
|
-
return orderClient;
|
|
292
|
+
return this.parseSingle(response.body, reqCtx);
|
|
293
|
+
} catch (e) {
|
|
294
|
+
console.error("Error applying actions to cart:", e);
|
|
295
|
+
throw e;
|
|
296
|
+
}
|
|
315
297
|
}
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
298
|
+
async getClient(reqCtx) {
|
|
299
|
+
const client = await new CommercetoolsClient(this.config).getClient(reqCtx);
|
|
300
|
+
const clientWithProject = client.withProjectKey({ projectKey: this.config.projectKey });
|
|
301
|
+
return {
|
|
302
|
+
carts: clientWithProject.me().carts(),
|
|
303
|
+
activeCart: clientWithProject.me().activeCart(),
|
|
304
|
+
orders: clientWithProject.me().orders()
|
|
305
|
+
};
|
|
323
306
|
}
|
|
324
|
-
parseSingle(remote,
|
|
307
|
+
parseSingle(remote, reqCtx) {
|
|
325
308
|
const result = this.newModel();
|
|
326
309
|
result.identifier = CommercetoolsCartIdentifierSchema.parse({
|
|
327
310
|
key: remote.id,
|
|
@@ -395,7 +378,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
395
378
|
result.meta = {
|
|
396
379
|
cache: {
|
|
397
380
|
hit: false,
|
|
398
|
-
key: this.generateCacheKeySingle(result.identifier,
|
|
381
|
+
key: this.generateCacheKeySingle(result.identifier, reqCtx)
|
|
399
382
|
},
|
|
400
383
|
placeholder: false
|
|
401
384
|
};
|
|
@@ -441,9 +424,6 @@ __decorateClass([
|
|
|
441
424
|
__decorateClass([
|
|
442
425
|
traced()
|
|
443
426
|
], CommercetoolsCartProvider.prototype, "getClient", 1);
|
|
444
|
-
__decorateClass([
|
|
445
|
-
traced()
|
|
446
|
-
], CommercetoolsCartProvider.prototype, "getPaymentClient", 1);
|
|
447
427
|
export {
|
|
448
428
|
CommercetoolsCartProvider
|
|
449
429
|
};
|