@reactionary/commercetools 0.6.3 → 0.6.5

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.
@@ -10,18 +10,27 @@ class CommercetoolsCartFactory {
10
10
  }
11
11
  parseCartIdentifier(_context, data) {
12
12
  return this.cartIdentifierSchema.parse({
13
- key: data.key || ""
13
+ key: data.key || "",
14
+ version: data.version || 0
14
15
  });
15
16
  }
16
17
  parseCart(context, data) {
17
18
  const identifier = this.parseCartIdentifier(context, {
18
- key: data.id
19
+ key: data.id,
20
+ version: data.version
19
21
  });
22
+ const items = [];
23
+ for (const lineItem of data.lineItems) {
24
+ items.push(this.parseCartItem(lineItem));
25
+ }
20
26
  const grandTotal = data.totalPrice.centAmount || 0;
21
27
  const shippingTotal = data.shippingInfo?.price.centAmount || 0;
22
28
  const productTotal = grandTotal - shippingTotal;
23
29
  const taxTotal = data.taxedPrice?.totalTax?.centAmount || 0;
24
- const discountTotal = data.discountOnTotalPrice?.discountedAmount.centAmount || 0;
30
+ const discountTotal = (data.discountOnTotalPrice?.discountedAmount.centAmount || 0) + items.reduce(
31
+ (sum, item) => sum + (item.price.totalDiscount.value * 100 || 0),
32
+ 0
33
+ );
25
34
  const surchargeTotal = 0;
26
35
  const currency = data.totalPrice.currencyCode;
27
36
  const price = {
@@ -50,9 +59,17 @@ class CommercetoolsCartFactory {
50
59
  currency
51
60
  }
52
61
  };
53
- const items = [];
54
- for (const lineItem of data.lineItems) {
55
- items.push(this.parseCartItem(lineItem));
62
+ const localeString = context.languageContext.locale || "en";
63
+ const appliedPromotions = [];
64
+ if (data.discountCodes) {
65
+ for (const promo of data.discountCodes) {
66
+ appliedPromotions.push({
67
+ code: promo.discountCode.obj?.code || "",
68
+ isCouponCode: true,
69
+ name: promo.discountCode.obj?.name?.[localeString] || "",
70
+ description: promo.discountCode.obj?.description?.[localeString] || ""
71
+ });
72
+ }
56
73
  }
57
74
  const result = {
58
75
  identifier,
@@ -63,14 +80,26 @@ class CommercetoolsCartFactory {
63
80
  description: data.custom?.fields["description"] || "",
64
81
  price,
65
82
  items,
66
- appliedPromotions: []
83
+ appliedPromotions
67
84
  };
68
85
  return this.cartSchema.parse(result);
69
86
  }
70
87
  parseCartItem(lineItem) {
71
88
  const unitPrice = lineItem.price.value.centAmount;
72
89
  const totalPrice = lineItem.totalPrice.centAmount || 0;
73
- const totalDiscount = lineItem.price.discounted?.value.centAmount || 0;
90
+ let itemDiscount = 0;
91
+ if (lineItem.discountedPricePerQuantity && lineItem.discountedPricePerQuantity.length > 0) {
92
+ itemDiscount = lineItem.discountedPricePerQuantity.reduce(
93
+ (sum, discPrQty) => {
94
+ return sum + discPrQty.quantity * discPrQty.discountedPrice?.includedDiscounts?.reduce(
95
+ (sum2, discount) => sum2 + discount.discountedAmount.centAmount,
96
+ 0
97
+ ) || 0;
98
+ },
99
+ 0
100
+ );
101
+ }
102
+ const totalDiscount = (lineItem.price.discounted?.value.centAmount || 0) + itemDiscount;
74
103
  const unitDiscount = totalDiscount / lineItem.quantity;
75
104
  const currency = lineItem.price.value.currencyCode.toUpperCase();
76
105
  return CartItemSchema.parse({
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@reactionary/commercetools",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",
7
7
  "dependencies": {
8
8
  "vitest": "^4.0.9",
9
9
  "@nx/vite": "22.4.5",
10
- "@reactionary/core": "0.6.3",
10
+ "@reactionary/core": "0.6.5",
11
11
  "zod": "4.1.9",
12
12
  "@commercetools/ts-client": "^4.9.1",
13
13
  "@commercetools/platform-sdk": "^8.25.0",
@@ -1,13 +1,15 @@
1
1
  import type { Cart as CTCart, LineItem } from '@commercetools/platform-sdk';
2
- import type { CartIdentifierSchema, CartSchema } from '@reactionary/core';
2
+ import type { CartSchema } from '@reactionary/core';
3
3
  import { type AnyCartIdentifierSchema, type AnyCartSchema, type CartFactory, type CartItem, type RequestContext } from '@reactionary/core';
4
4
  import type * as z from 'zod';
5
- export declare class CommercetoolsCartFactory<TCartSchema extends AnyCartSchema = typeof CartSchema, TCartIdentifierSchema extends AnyCartIdentifierSchema = typeof CartIdentifierSchema> implements CartFactory<TCartSchema, TCartIdentifierSchema> {
5
+ import type { CommercetoolsCartIdentifierSchema } from '../../schema/commercetools.schema.js';
6
+ export declare class CommercetoolsCartFactory<TCartSchema extends AnyCartSchema = typeof CartSchema, TCartIdentifierSchema extends AnyCartIdentifierSchema = typeof CommercetoolsCartIdentifierSchema> implements CartFactory<TCartSchema, TCartIdentifierSchema> {
6
7
  readonly cartSchema: TCartSchema;
7
8
  readonly cartIdentifierSchema: TCartIdentifierSchema;
8
9
  constructor(cartSchema: TCartSchema, cartIdentifierSchema: TCartIdentifierSchema);
9
10
  parseCartIdentifier(_context: RequestContext, data: {
10
11
  key?: string;
12
+ version?: number;
11
13
  }): z.output<TCartIdentifierSchema>;
12
14
  parseCart(context: RequestContext, data: CTCart): z.output<TCartSchema>;
13
15
  protected parseCartItem(lineItem: LineItem): CartItem;