@reactionary/feeds 0.10.48 → 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.
Files changed (2) hide show
  1. package/cli.cjs +105 -9
  2. package/package.json +9 -9
package/cli.cjs CHANGED
@@ -112881,17 +112881,38 @@ var init_cart_factory2 = __esm({
112881
112881
  }
112882
112882
  };
112883
112883
  const localeString = getLanguageCodeFromLocale(context2.languageContext.locale) || "en";
112884
+ const discountedAmountByCartDiscountId = this.groupDiscountedAmountsByCartDiscountId(data);
112884
112885
  const appliedPromotions = [];
112886
+ const codeClaimedCartDiscountIds = /* @__PURE__ */ new Set();
112885
112887
  if (data.discountCodes) {
112886
112888
  for (const promo of data.discountCodes) {
112889
+ const cartDiscountIds = promo.discountCode.obj?.cartDiscounts.map((ref) => ref.id) || [];
112890
+ cartDiscountIds.forEach((id) => codeClaimedCartDiscountIds.add(id));
112891
+ const discountedCentAmount = cartDiscountIds.reduce(
112892
+ (sum, id) => sum + (discountedAmountByCartDiscountId.get(id)?.centAmount || 0),
112893
+ 0
112894
+ );
112887
112895
  appliedPromotions.push({
112888
112896
  code: promo.discountCode.obj?.code || "",
112889
112897
  isCouponCode: true,
112890
112898
  name: promo.discountCode.obj?.name?.[localeString] || "",
112891
- description: promo.discountCode.obj?.description?.[localeString] || ""
112899
+ description: promo.discountCode.obj?.description?.[localeString] || "",
112900
+ ...discountedCentAmount > 0 ? { amount: { value: discountedCentAmount / 100, currency } } : {}
112892
112901
  });
112893
112902
  }
112894
112903
  }
112904
+ for (const [cartDiscountId, applied] of discountedAmountByCartDiscountId) {
112905
+ if (codeClaimedCartDiscountIds.has(cartDiscountId)) {
112906
+ continue;
112907
+ }
112908
+ appliedPromotions.push({
112909
+ code: "",
112910
+ isCouponCode: false,
112911
+ name: applied.cartDiscount?.name?.[localeString] || "",
112912
+ description: applied.cartDiscount?.description?.[localeString] || "",
112913
+ ...applied.centAmount > 0 ? { amount: { value: applied.centAmount / 100, currency } } : {}
112914
+ });
112915
+ }
112895
112916
  const result = {
112896
112917
  identifier,
112897
112918
  user: {
@@ -112906,6 +112927,46 @@ var init_cart_factory2 = __esm({
112906
112927
  };
112907
112928
  return this.cartSchema.parse(result);
112908
112929
  }
112930
+ /**
112931
+ * commercetools links a discount code to the amount it discounted only
112932
+ * indirectly: `discountCode.obj.cartDiscounts` gives the CartDiscount ids a
112933
+ * code triggers, while the actual discounted amounts are reported per line
112934
+ * item (`discountedPricePerQuantity[].discountedPrice.includedDiscounts`)
112935
+ * and on the cart total (`discountOnTotalPrice.includedDiscounts`), each
112936
+ * keyed by that same CartDiscount id. Sum both sources by CartDiscount id
112937
+ * so a discount code's total can be looked up by joining on its ids, and
112938
+ * keep the expanded CartDiscount (when available) so automatically applied
112939
+ * discounts — those not claimed by any code — can be surfaced by name.
112940
+ */
112941
+ groupDiscountedAmountsByCartDiscountId(data) {
112942
+ const totals = /* @__PURE__ */ new Map();
112943
+ const add = (cartDiscountId, centAmount, cartDiscount) => {
112944
+ const entry = totals.get(cartDiscountId) || { centAmount: 0 };
112945
+ entry.centAmount += centAmount;
112946
+ entry.cartDiscount = entry.cartDiscount || cartDiscount;
112947
+ totals.set(cartDiscountId, entry);
112948
+ };
112949
+ const expandedCartDiscount = (reference) => reference.typeId === "cart-discount" ? reference.obj : void 0;
112950
+ for (const lineItem of data.lineItems) {
112951
+ for (const discPrQty of lineItem.discountedPricePerQuantity || []) {
112952
+ for (const included of discPrQty.discountedPrice?.includedDiscounts || []) {
112953
+ add(
112954
+ included.discount.id,
112955
+ included.discountedAmount.centAmount * discPrQty.quantity,
112956
+ expandedCartDiscount(included.discount)
112957
+ );
112958
+ }
112959
+ }
112960
+ }
112961
+ for (const included of data.discountOnTotalPrice?.includedDiscounts || []) {
112962
+ add(
112963
+ included.discount.id,
112964
+ included.discountedAmount.centAmount,
112965
+ expandedCartDiscount(included.discount)
112966
+ );
112967
+ }
112968
+ return totals;
112969
+ }
112909
112970
  parseCartItem(lineItem) {
112910
112971
  const unitPrice = lineItem.price.value.centAmount;
112911
112972
  const totalPrice = lineItem.totalPrice.centAmount || 0;
@@ -114242,7 +114303,11 @@ var init_cart_capability2 = __esm({
114242
114303
  CommercetoolsCartCapability = class extends CartCapability {
114243
114304
  config;
114244
114305
  commercetools;
114245
- expandedCartFields = ["discountCodes[*].discountCode"];
114306
+ expandedCartFields = [
114307
+ "discountCodes[*].discountCode",
114308
+ "lineItems[*].discountedPricePerQuantity[*].discountedPrice.includedDiscounts[*].discount",
114309
+ "discountOnTotalPrice.includedDiscounts[*].discount"
114310
+ ];
114246
114311
  factory;
114247
114312
  constructor(config3, cache, context2, commercetools, factory) {
114248
114313
  super(cache, context2);
@@ -121686,7 +121751,9 @@ var init_cart_capability3 = __esm({
121686
121751
  code: payload.couponCode,
121687
121752
  isCouponCode: true,
121688
121753
  name: `Promotion for ${payload.couponCode}`,
121689
- description: `Description for promotion with code ${payload.couponCode}`
121754
+ description: `Description for promotion with code ${payload.couponCode}`,
121755
+ amount: { value: 10, currency: cart.price.grandTotal.currency }
121756
+ // For simplicity, every coupon gives a $10 discount
121690
121757
  });
121691
121758
  cart.price.totalDiscount.value += 10;
121692
121759
  cart.price.grandTotal.value -= 10;
@@ -137800,7 +137867,7 @@ var init_cart_capability5 = __esm({
137800
137867
  *
137801
137868
  * example: this.includedFields = [includedFields, '+discounts.*'].join(',');
137802
137869
  */
137803
- includedFields = ["+items.*"].join(",");
137870
+ includedFields = ["+items.*", "+items.adjustments.*", "+shipping_methods.adjustments.*"].join(",");
137804
137871
  async listCarts(payload) {
137805
137872
  const client = await this.getClient();
137806
137873
  const sessionData = this.medusaApi.getSessionData();
@@ -161521,6 +161588,7 @@ var init_cart_factory5 = __esm({
161521
161588
  for (const remoteItem of allItems) {
161522
161589
  items.push(this.parseCartItem(context2, remoteItem, price.grandTotal.currency));
161523
161590
  }
161591
+ const adjustmentsByPromotionId = this.groupAdjustmentsByPromotionId(data);
161524
161592
  const appliedPromotions = [];
161525
161593
  if (data.promotions) {
161526
161594
  for (const promo of data.promotions) {
@@ -161532,11 +161600,13 @@ var init_cart_factory5 = __esm({
161532
161600
  if (promo.application_method?.type === "fixed") {
161533
161601
  promoDescription = `-${promo.application_method.value} ${price.grandTotal.currency}`;
161534
161602
  }
161603
+ const discountedAmount = adjustmentsByPromotionId.get(promo.id);
161535
161604
  appliedPromotions.push({
161536
161605
  code: promo.code || "",
161537
161606
  isCouponCode: promo.is_automatic ? false : true,
161538
161607
  name: promotionName || promoDescription,
161539
- description: promoDescription
161608
+ description: promoDescription,
161609
+ ...discountedAmount !== void 0 ? { amount: { value: Math.abs(discountedAmount), currency: price.grandTotal.currency } } : {}
161540
161610
  });
161541
161611
  }
161542
161612
  }
@@ -161561,11 +161631,37 @@ var init_cart_factory5 = __esm({
161561
161631
  return this.cartIdentifierSchema.parse(result);
161562
161632
  }
161563
161633
  /**
161564
- * Extension point to control the parsing of a single cart item price
161565
- * @param remoteItem
161566
- * @param currency
161567
- * @returns
161634
+ * Sums each line item's and shipping method's adjustments by the promotion
161635
+ * that caused them, so the resulting amount is the actual monetary total
161636
+ * discounted by that promotion on this cart.
161568
161637
  */
161638
+ groupAdjustmentsByPromotionId(data) {
161639
+ const totals = /* @__PURE__ */ new Map();
161640
+ const addAdjustments = (adjustments) => {
161641
+ for (const adjustment of adjustments || []) {
161642
+ if (!adjustment.promotion_id) {
161643
+ continue;
161644
+ }
161645
+ totals.set(
161646
+ adjustment.promotion_id,
161647
+ (totals.get(adjustment.promotion_id) || 0) + adjustment.amount
161648
+ );
161649
+ }
161650
+ };
161651
+ for (const item of data.items || []) {
161652
+ addAdjustments(item.adjustments);
161653
+ }
161654
+ for (const shippingMethod of data.shipping_methods || []) {
161655
+ addAdjustments(shippingMethod.adjustments);
161656
+ }
161657
+ return totals;
161658
+ }
161659
+ /**
161660
+ * Extension point to control the parsing of a single cart item price
161661
+ * @param remoteItem
161662
+ * @param currency
161663
+ * @returns
161664
+ */
161569
161665
  parseItemPrice(_context, remoteItem, currency) {
161570
161666
  return parseMedusaItemPrice(remoteItem, currency);
161571
161667
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/feeds",
3
- "version": "0.10.48",
3
+ "version": "0.10.49",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",
@@ -12,14 +12,14 @@
12
12
  "url": "git+https://github.com/Solteq/reactionary.git"
13
13
  },
14
14
  "dependencies": {
15
- "@reactionary/algolia": "0.10.48",
16
- "@reactionary/commercetools": "0.10.48",
17
- "@reactionary/core": "0.10.48",
18
- "@reactionary/fake": "0.10.48",
19
- "@reactionary/magento": "0.10.48",
20
- "@reactionary/medusa": "0.10.48",
21
- "@reactionary/meilisearch": "0.10.48",
22
- "@reactionary/unomi": "0.10.48",
15
+ "@reactionary/algolia": "0.10.49",
16
+ "@reactionary/commercetools": "0.10.49",
17
+ "@reactionary/core": "0.10.49",
18
+ "@reactionary/fake": "0.10.49",
19
+ "@reactionary/magento": "0.10.49",
20
+ "@reactionary/medusa": "0.10.49",
21
+ "@reactionary/meilisearch": "0.10.49",
22
+ "@reactionary/unomi": "0.10.49",
23
23
  "commander": "^15.0.0",
24
24
  "dotenv": "^17.2.2",
25
25
  "sitemap": "^9.0.1",