@wtree/payload-ecommerce-coupon 3.72.5 → 3.76.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"recalculateCart.d.ts","sourceRoot":"","sources":["../../src/hooks/recalculateCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAA;AACzD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AAO5D,eAAO,MAAM,mBAAmB,GAC7B,cAAc,4BAA4B,KAAG,0BAoL7C,CAAA"}
1
+ {"version":3,"file":"recalculateCart.d.ts","sourceRoot":"","sources":["../../src/hooks/recalculateCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAA;AACzD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AAQ5D,eAAO,MAAM,mBAAmB,GAC7B,cAAc,4BAA4B,KAAG,0BAyL7C,CAAA"}
package/dist/index.js CHANGED
@@ -567,6 +567,43 @@ function roundTo2(value) {
567
567
  return Math.round(value * 100) / 100;
568
568
  }
569
569
 
570
+ //#endregion
571
+ //#region src/utilities/pricing.ts
572
+ const DEFAULT_PRICE_CURRENCY = "AED";
573
+ function normalizeCurrencyCode(currencyCode) {
574
+ if (!currencyCode) return DEFAULT_PRICE_CURRENCY;
575
+ return currencyCode.toUpperCase();
576
+ }
577
+ function readNumberField(entity, key) {
578
+ if (!entity || typeof entity !== "object") return void 0;
579
+ const value = entity[key];
580
+ return typeof value === "number" ? value : void 0;
581
+ }
582
+ function getPriceFieldKey(currencyCode) {
583
+ return `priceIn${normalizeCurrencyCode(currencyCode)}`;
584
+ }
585
+ function readMoneyField(entity, currencyCode, defaultCurrencyCode = DEFAULT_PRICE_CURRENCY) {
586
+ if (!entity) return void 0;
587
+ const primaryField = getPriceFieldKey(currencyCode);
588
+ const primary = readNumberField(entity, primaryField);
589
+ if (typeof primary === "number") return primary;
590
+ const fallbackField = getPriceFieldKey(defaultCurrencyCode);
591
+ if (fallbackField !== primaryField) {
592
+ const fallback = readNumberField(entity, fallbackField);
593
+ if (typeof fallback === "number") return fallback;
594
+ }
595
+ return typeof entity.price === "number" ? entity.price : void 0;
596
+ }
597
+ function resolveMoneyField(entity, currencyCode, defaultCurrencyCode = DEFAULT_PRICE_CURRENCY) {
598
+ return readMoneyField(entity, currencyCode, defaultCurrencyCode) ?? 0;
599
+ }
600
+ function getCartItemUnitPrice({ item, product, variant, currencyCode, defaultCurrencyCode = DEFAULT_PRICE_CURRENCY }) {
601
+ if (typeof item?.price === "number") return item.price;
602
+ if (typeof item?.unitPrice === "number") return item.unitPrice;
603
+ if (variant) return resolveMoneyField(variant, currencyCode, defaultCurrencyCode);
604
+ return resolveMoneyField(product, currencyCode, defaultCurrencyCode);
605
+ }
606
+
570
607
  //#endregion
571
608
  //#region src/utilities/calculateValues.ts
572
609
  function calculateCouponDiscount({ coupon, cartTotal }) {
@@ -580,7 +617,7 @@ function calculateCouponDiscount({ coupon, cartTotal }) {
580
617
  }
581
618
  return roundTo2(discount);
582
619
  }
583
- function calculateCommissionAndDiscount({ cartItems, program }) {
620
+ function calculateCommissionAndDiscount({ cartItems, program, currencyCode = "AED" }) {
584
621
  const rules = program.commissionRules || [];
585
622
  if (rules.length === 0) return {
586
623
  partnerCommission: 0,
@@ -591,9 +628,12 @@ function calculateCommissionAndDiscount({ cartItems, program }) {
591
628
  for (const item of cartItems) {
592
629
  const rule = findApplicableCommissionRule(rules, item);
593
630
  if (!rule) continue;
594
- const product = typeof item.product === "object" ? item.product : {};
595
- const variant = typeof item.variant === "object" ? item.variant : {};
596
- const itemPrice = item.price ?? item.unitPrice ?? product.price ?? product.priceInAED ?? variant.price ?? variant.priceInAED ?? 0;
631
+ const itemPrice = getCartItemUnitPrice({
632
+ item,
633
+ product: typeof item.product === "object" ? item.product : {},
634
+ variant: typeof item.variant === "object" ? item.variant : {},
635
+ currencyCode
636
+ });
597
637
  const quantity = item.quantity ?? 1;
598
638
  const itemTotal = itemPrice * quantity;
599
639
  let itemPartner;
@@ -1272,7 +1312,12 @@ const recalculateCartHook = (pluginConfig) => async ({ data, req, originalDoc })
1272
1312
  const enrichedItems = effectiveItems.map((item) => {
1273
1313
  const productId = typeof item.product === "string" ? item.product : item.product?.id;
1274
1314
  const product = productsMap.get(productId) || {};
1275
- const itemPrice = item.price ?? item.unitPrice ?? product.price ?? product.priceInAED ?? 0;
1315
+ const itemPrice = getCartItemUnitPrice({
1316
+ item,
1317
+ product,
1318
+ variant: typeof item.variant === "object" ? item.variant : void 0,
1319
+ currencyCode: pluginConfig.defaultCurrency
1320
+ });
1276
1321
  calculatedSubtotal += itemPrice * (item.quantity ?? 1);
1277
1322
  console.log("[RecalculateCart] Item processed", {
1278
1323
  productId,
@@ -1299,7 +1344,8 @@ const recalculateCartHook = (pluginConfig) => async ({ data, req, originalDoc })
1299
1344
  if (program) {
1300
1345
  const { partnerCommission, customerDiscount } = calculateCommissionAndDiscount({
1301
1346
  cartItems: enrichedItems,
1302
- program
1347
+ program,
1348
+ currencyCode: pluginConfig.defaultCurrency
1303
1349
  });
1304
1350
  const roundedCustomerDiscount = roundTo2(customerDiscount);
1305
1351
  data.partnerCommission = roundTo2(partnerCommission);