@reactionary/provider-commercetools 0.3.17 → 0.3.18
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,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-commercetools",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.18",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.3.
|
|
7
|
+
"@reactionary/core": "0.3.18",
|
|
8
8
|
"debug": "^4.4.3",
|
|
9
9
|
"zod": "4.1.9",
|
|
10
10
|
"@commercetools/ts-client": "^4.2.1",
|
|
@@ -32,6 +32,7 @@ import { CommercetoolsCartIdentifierSchema } from "../schema/commercetools.schem
|
|
|
32
32
|
class CommercetoolsCartProvider extends CartProvider {
|
|
33
33
|
constructor(config, cache, context, commercetools) {
|
|
34
34
|
super(cache, context);
|
|
35
|
+
this.expandedCartFields = ["discountCodes[*].discountCode"];
|
|
35
36
|
this.config = config;
|
|
36
37
|
this.commercetools = commercetools;
|
|
37
38
|
}
|
|
@@ -39,7 +40,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
39
40
|
const client = await this.getClient();
|
|
40
41
|
const ctId = payload.cart;
|
|
41
42
|
try {
|
|
42
|
-
const remote = await client.carts.withId({ ID: ctId.key }).get(
|
|
43
|
+
const remote = await client.carts.withId({ ID: ctId.key }).get(
|
|
44
|
+
{
|
|
45
|
+
queryArgs: {
|
|
46
|
+
expand: this.expandedCartFields
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
).execute();
|
|
43
50
|
return success(this.parseSingle(remote.body));
|
|
44
51
|
} catch (err) {
|
|
45
52
|
return error({
|
|
@@ -53,6 +60,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
53
60
|
if (!cartIdentifier) {
|
|
54
61
|
cartIdentifier = await this.createCart();
|
|
55
62
|
}
|
|
63
|
+
const channelId = await this.commercetools.resolveChannelIdByRole("Primary");
|
|
56
64
|
const result = await this.applyActions(cartIdentifier, [
|
|
57
65
|
{
|
|
58
66
|
action: "addLineItem",
|
|
@@ -61,7 +69,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
61
69
|
// FIXME: This should be dynamic, probably as part of the context...
|
|
62
70
|
distributionChannel: {
|
|
63
71
|
typeId: "channel",
|
|
64
|
-
|
|
72
|
+
id: channelId
|
|
65
73
|
}
|
|
66
74
|
},
|
|
67
75
|
{
|
|
@@ -142,11 +150,21 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
142
150
|
return success(result);
|
|
143
151
|
}
|
|
144
152
|
async removeCouponCode(payload) {
|
|
153
|
+
const client = await this.getClient();
|
|
154
|
+
const currentCart = await client.carts.withId({ ID: payload.cart.key }).get({
|
|
155
|
+
queryArgs: {
|
|
156
|
+
expand: this.expandedCartFields
|
|
157
|
+
}
|
|
158
|
+
}).execute();
|
|
159
|
+
const discountCodeReference = currentCart.body.discountCodes?.find((dc) => dc.discountCode.obj?.code === payload.couponCode)?.discountCode;
|
|
160
|
+
if (!discountCodeReference) {
|
|
161
|
+
return success(this.parseSingle(currentCart.body));
|
|
162
|
+
}
|
|
145
163
|
const result = await this.applyActions(payload.cart, [
|
|
146
164
|
{
|
|
147
165
|
action: "removeDiscountCode",
|
|
148
166
|
discountCode: {
|
|
149
|
-
id:
|
|
167
|
+
id: discountCodeReference.id,
|
|
150
168
|
typeId: "discount-code"
|
|
151
169
|
}
|
|
152
170
|
},
|
|
@@ -197,6 +215,9 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
197
215
|
currency: this.context.languageContext.currencyCode || "USD",
|
|
198
216
|
country: this.context.taxJurisdiction.countryCode || "US",
|
|
199
217
|
locale: this.context.languageContext.locale
|
|
218
|
+
},
|
|
219
|
+
queryArgs: {
|
|
220
|
+
expand: this.expandedCartFields
|
|
200
221
|
}
|
|
201
222
|
}).execute();
|
|
202
223
|
return CommercetoolsCartIdentifierSchema.parse({
|
|
@@ -212,6 +233,9 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
212
233
|
body: {
|
|
213
234
|
version: ctId.version,
|
|
214
235
|
actions
|
|
236
|
+
},
|
|
237
|
+
queryArgs: {
|
|
238
|
+
expand: this.expandedCartFields
|
|
215
239
|
}
|
|
216
240
|
}).execute();
|
|
217
241
|
if (response.error) {
|
|
@@ -242,7 +266,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
242
266
|
parseCartItem(remoteItem) {
|
|
243
267
|
const unitPrice = remoteItem.price.value.centAmount;
|
|
244
268
|
const totalPrice = remoteItem.totalPrice.centAmount || 0;
|
|
245
|
-
|
|
269
|
+
let itemDiscount = 0;
|
|
270
|
+
if (remoteItem.discountedPricePerQuantity && remoteItem.discountedPricePerQuantity.length > 0) {
|
|
271
|
+
itemDiscount = remoteItem.discountedPricePerQuantity.reduce((sum, discPrQty) => {
|
|
272
|
+
return sum + discPrQty.quantity * discPrQty.discountedPrice?.includedDiscounts?.reduce((sum2, discount) => sum2 + discount.discountedAmount.centAmount, 0) || 0;
|
|
273
|
+
}, 0);
|
|
274
|
+
}
|
|
275
|
+
const totalDiscount = (remoteItem.price.discounted?.value.centAmount || 0) + itemDiscount;
|
|
246
276
|
const unitDiscount = totalDiscount / remoteItem.quantity;
|
|
247
277
|
const currency = remoteItem.price.value.currencyCode.toUpperCase();
|
|
248
278
|
const item = {
|
|
@@ -282,11 +312,16 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
282
312
|
key: remote.id,
|
|
283
313
|
version: remote.version || 0
|
|
284
314
|
};
|
|
315
|
+
const items = new Array();
|
|
316
|
+
for (const remoteItem of remote.lineItems) {
|
|
317
|
+
const item = this.parseCartItem(remoteItem);
|
|
318
|
+
items.push(item);
|
|
319
|
+
}
|
|
285
320
|
const grandTotal = remote.totalPrice.centAmount || 0;
|
|
286
321
|
const shippingTotal = remote.shippingInfo?.price.centAmount || 0;
|
|
287
322
|
const productTotal = grandTotal - shippingTotal;
|
|
288
323
|
const taxTotal = remote.taxedPrice?.totalTax?.centAmount || 0;
|
|
289
|
-
const discountTotal = remote.discountOnTotalPrice?.discountedAmount.centAmount || 0;
|
|
324
|
+
const discountTotal = (remote.discountOnTotalPrice?.discountedAmount.centAmount || 0) + items.reduce((sum, item) => sum + (item.price.totalDiscount.value * 100 || 0), 0);
|
|
290
325
|
const surchargeTotal = 0;
|
|
291
326
|
const currency = remote.totalPrice.currencyCode;
|
|
292
327
|
const price = {
|
|
@@ -315,10 +350,17 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
315
350
|
currency
|
|
316
351
|
}
|
|
317
352
|
};
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
353
|
+
const localeString = this.context.languageContext.locale || "en";
|
|
354
|
+
const appliedPromotions = [];
|
|
355
|
+
if (remote.discountCodes) {
|
|
356
|
+
for (const promo of remote.discountCodes) {
|
|
357
|
+
appliedPromotions.push({
|
|
358
|
+
code: promo.discountCode.obj?.code || "",
|
|
359
|
+
isCouponCode: true,
|
|
360
|
+
name: promo.discountCode.obj?.name?.[localeString] || "",
|
|
361
|
+
description: promo.discountCode.obj?.description?.[localeString] || ""
|
|
362
|
+
});
|
|
363
|
+
}
|
|
322
364
|
}
|
|
323
365
|
const cart = {
|
|
324
366
|
identifier,
|
|
@@ -328,6 +370,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
328
370
|
name: remote.custom?.fields["name"] || "",
|
|
329
371
|
description: remote.custom?.fields["description"] || "",
|
|
330
372
|
price,
|
|
373
|
+
appliedPromotions,
|
|
331
374
|
items
|
|
332
375
|
};
|
|
333
376
|
return cart;
|
|
@@ -364,8 +407,7 @@ __decorateClass([
|
|
|
364
407
|
], CommercetoolsCartProvider.prototype, "getActiveCartId", 1);
|
|
365
408
|
__decorateClass([
|
|
366
409
|
Reactionary({
|
|
367
|
-
inputSchema: CartMutationDeleteCartSchema
|
|
368
|
-
outputSchema: CartSchema
|
|
410
|
+
inputSchema: CartMutationDeleteCartSchema
|
|
369
411
|
})
|
|
370
412
|
], CommercetoolsCartProvider.prototype, "deleteCart", 1);
|
|
371
413
|
__decorateClass([
|
|
@@ -6,6 +6,7 @@ import type { CommercetoolsAPI } from '../core/client.js';
|
|
|
6
6
|
export declare class CommercetoolsCartProvider extends CartProvider {
|
|
7
7
|
protected config: CommercetoolsConfiguration;
|
|
8
8
|
protected commercetools: CommercetoolsAPI;
|
|
9
|
+
protected expandedCartFields: string[];
|
|
9
10
|
constructor(config: CommercetoolsConfiguration, cache: Cache, context: RequestContext, commercetools: CommercetoolsAPI);
|
|
10
11
|
getById(payload: CartQueryById): Promise<Result<Cart, NotFoundError>>;
|
|
11
12
|
add(payload: CartMutationItemAdd): Promise<Result<Cart>>;
|