@reactionary/medusa 0.10.47 → 0.10.49

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.
@@ -50,7 +50,7 @@ class MedusaCartCapability extends CartCapability {
50
50
  *
51
51
  * example: this.includedFields = [includedFields, '+discounts.*'].join(',');
52
52
  */
53
- includedFields = ["+items.*"].join(",");
53
+ includedFields = ["+items.*", "+items.adjustments.*", "+shipping_methods.adjustments.*"].join(",");
54
54
  async listCarts(payload) {
55
55
  const client = await this.getClient();
56
56
  const sessionData = this.medusaApi.getSessionData();
@@ -52,6 +52,7 @@ class MedusaCartFactory {
52
52
  for (const remoteItem of allItems) {
53
53
  items.push(this.parseCartItem(context, remoteItem, price.grandTotal.currency));
54
54
  }
55
+ const adjustmentsByPromotionId = this.groupAdjustmentsByPromotionId(data);
55
56
  const appliedPromotions = [];
56
57
  if (data.promotions) {
57
58
  for (const promo of data.promotions) {
@@ -63,11 +64,13 @@ class MedusaCartFactory {
63
64
  if (promo.application_method?.type === "fixed") {
64
65
  promoDescription = `-${promo.application_method.value} ${price.grandTotal.currency}`;
65
66
  }
67
+ const discountedAmount = adjustmentsByPromotionId.get(promo.id);
66
68
  appliedPromotions.push({
67
69
  code: promo.code || "",
68
70
  isCouponCode: promo.is_automatic ? false : true,
69
71
  name: promotionName || promoDescription,
70
- description: promoDescription
72
+ description: promoDescription,
73
+ ...discountedAmount !== void 0 ? { amount: { value: Math.abs(discountedAmount), currency: price.grandTotal.currency } } : {}
71
74
  });
72
75
  }
73
76
  }
@@ -92,11 +95,37 @@ class MedusaCartFactory {
92
95
  return this.cartIdentifierSchema.parse(result);
93
96
  }
94
97
  /**
95
- * Extension point to control the parsing of a single cart item price
96
- * @param remoteItem
97
- * @param currency
98
- * @returns
98
+ * Sums each line item's and shipping method's adjustments by the promotion
99
+ * that caused them, so the resulting amount is the actual monetary total
100
+ * discounted by that promotion on this cart.
99
101
  */
102
+ groupAdjustmentsByPromotionId(data) {
103
+ const totals = /* @__PURE__ */ new Map();
104
+ const addAdjustments = (adjustments) => {
105
+ for (const adjustment of adjustments || []) {
106
+ if (!adjustment.promotion_id) {
107
+ continue;
108
+ }
109
+ totals.set(
110
+ adjustment.promotion_id,
111
+ (totals.get(adjustment.promotion_id) || 0) + adjustment.amount
112
+ );
113
+ }
114
+ };
115
+ for (const item of data.items || []) {
116
+ addAdjustments(item.adjustments);
117
+ }
118
+ for (const shippingMethod of data.shipping_methods || []) {
119
+ addAdjustments(shippingMethod.adjustments);
120
+ }
121
+ return totals;
122
+ }
123
+ /**
124
+ * Extension point to control the parsing of a single cart item price
125
+ * @param remoteItem
126
+ * @param currency
127
+ * @returns
128
+ */
100
129
  parseItemPrice(_context, remoteItem, currency) {
101
130
  return parseMedusaItemPrice(remoteItem, currency);
102
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/medusa",
3
- "version": "0.10.47",
3
+ "version": "0.10.49",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@medusajs/js-sdk": "^2.13.3",
13
- "@reactionary/core": "0.10.47",
13
+ "@reactionary/core": "0.10.49",
14
14
  "debug": "^4.4.3",
15
15
  "@medusajs/types": "^2.13.3",
16
16
  "zod": "4.1.9"
@@ -15,11 +15,17 @@ export declare class MedusaCartFactory<TCartSchema extends AnyCartSchema = typeo
15
15
  parseCart(context: RequestContext, data: StoreCart): z.output<TCartSchema>;
16
16
  parseCartIdentifier(_context: RequestContext, data: StoreCart): z.output<TCartIdentifierSchema>;
17
17
  /**
18
- * Extension point to control the parsing of a single cart item price
19
- * @param remoteItem
20
- * @param currency
21
- * @returns
18
+ * Sums each line item's and shipping method's adjustments by the promotion
19
+ * that caused them, so the resulting amount is the actual monetary total
20
+ * discounted by that promotion on this cart.
22
21
  */
22
+ protected groupAdjustmentsByPromotionId(data: StoreCart): Map<string, number>;
23
+ /**
24
+ * Extension point to control the parsing of a single cart item price
25
+ * @param remoteItem
26
+ * @param currency
27
+ * @returns
28
+ */
23
29
  protected parseItemPrice(_context: RequestContext, remoteItem: StoreCartLineItem, currency: Currency): ItemCostBreakdown;
24
30
  /**
25
31
  * Extension point to control the parsing of the cost breakdown of a cart