@wtree/payload-ecommerce-coupon 3.78.5 → 3.78.6

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":"createReferralProgramsCollection.d.ts","sourceRoot":"","sources":["../../src/collections/createReferralProgramsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAEzD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AA0B5D,eAAO,MAAM,gCAAgC,GAC3C,cAAc,4BAA4B,KACzC,gBAwZF,CAAA"}
1
+ {"version":3,"file":"createReferralProgramsCollection.d.ts","sourceRoot":"","sources":["../../src/collections/createReferralProgramsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAEzD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AAsB5D,eAAO,MAAM,gCAAgC,GAC3C,cAAc,4BAA4B,KACzC,gBA0ZF,CAAA"}
package/dist/index.js CHANGED
@@ -427,9 +427,6 @@ const createReferralCodesCollection = (pluginConfig) => {
427
427
  function toNumber(value) {
428
428
  return typeof value === "number" && Number.isFinite(value) ? value : null;
429
429
  }
430
- function toCents(value) {
431
- return Math.round(value * 100);
432
- }
433
430
  const createReferralProgramsCollection = (pluginConfig) => {
434
431
  const { collections, access, adminGroups, referralConfig, integration } = pluginConfig;
435
432
  const allowedTotalCommissionTypes = referralConfig.allowedTotalCommissionTypes;
@@ -460,8 +457,8 @@ const createReferralProgramsCollection = (pluginConfig) => {
460
457
  if (maxCustomerDiscountPerOrder != null && maxCustomerDiscountPerOrder < 0) throw new payload.APIError("Maximum discount for customer per order must be a non-negative number", 400);
461
458
  const minOrderAmount = toNumber(data.minOrderAmount);
462
459
  if (minOrderAmount != null && minOrderAmount < 0) throw new payload.APIError("Minimum Order Amount must be a non-negative number", 400);
463
- data.maxPartnerCommissionPerOrder = maxPartnerCommissionPerOrder != null ? toCents(maxPartnerCommissionPerOrder) : null;
464
- data.maxCustomerDiscountPerOrder = maxCustomerDiscountPerOrder != null ? toCents(maxCustomerDiscountPerOrder) : null;
460
+ data.maxPartnerCommissionPerOrder = maxPartnerCommissionPerOrder != null ? maxPartnerCommissionPerOrder : null;
461
+ data.maxCustomerDiscountPerOrder = maxCustomerDiscountPerOrder != null ? maxCustomerDiscountPerOrder : null;
465
462
  data.minOrderAmount = minOrderAmount ?? null;
466
463
  data.commissionRules = data.commissionRules.map((rule, index) => {
467
464
  const r = rule;
@@ -503,8 +500,8 @@ const createReferralProgramsCollection = (pluginConfig) => {
503
500
  if (customerAmountInput == null || customerAmountInput < 0) throw new payload.APIError(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`, 400);
504
501
  partnerAmount = partnerAmountInput;
505
502
  customerAmount = customerAmountInput;
506
- partnerSplit = toCents(partnerAmountInput);
507
- customerSplit = toCents(customerAmountInput);
503
+ partnerSplit = partnerAmountInput;
504
+ customerSplit = customerAmountInput;
508
505
  } else if (hasLegacyFixedInputs) {
509
506
  if (legacyPartnerSplitInput == null || legacyPartnerSplitInput < 0) throw new payload.APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`, 400);
510
507
  const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? 100 - legacyPartnerSplitInput;
@@ -518,7 +515,11 @@ const createReferralProgramsCollection = (pluginConfig) => {
518
515
  return {
519
516
  ...rule,
520
517
  appliesTo: appliesTo === "categories" ? "segments" : appliesTo,
521
- totalCommission: { type },
518
+ totalCommission: {
519
+ type,
520
+ ...typeof r.totalCommission.value === "number" ? { value: r.totalCommission.value } : {},
521
+ ...typeof r.totalCommission.maxAmount === "number" ? { maxAmount: r.totalCommission.maxAmount } : {}
522
+ },
522
523
  partnerPercent,
523
524
  customerPercent,
524
525
  partnerAmount,
@@ -547,13 +548,13 @@ const createReferralProgramsCollection = (pluginConfig) => {
547
548
  name: "maxPartnerCommissionPerOrder",
548
549
  type: "number",
549
550
  min: 0,
550
- admin: { description: "Maximum commission per order for partner. Leave empty for no cap. Enter value in currency units (will be converted to cents)." }
551
+ admin: { description: "Maximum commission per order for partner. Leave empty for no cap." }
551
552
  },
552
553
  {
553
554
  name: "maxCustomerDiscountPerOrder",
554
555
  type: "number",
555
556
  min: 0,
556
- admin: { description: "Maximum customer discount per order. Leave empty for no cap. Enter value in currency units (will be converted to cents)." }
557
+ admin: { description: "Maximum customer discount per order. Leave empty for no cap." }
557
558
  },
558
559
  {
559
560
  name: "minOrderAmount",
@@ -677,7 +678,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
677
678
  min: 0,
678
679
  admin: {
679
680
  hidden: true,
680
- description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount in cents."
681
+ description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount."
681
682
  }
682
683
  },
683
684
  {
@@ -686,7 +687,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
686
687
  min: 0,
687
688
  admin: {
688
689
  hidden: true,
689
- description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount in cents."
690
+ description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount."
690
691
  }
691
692
  }
692
693
  ]
@@ -793,15 +794,52 @@ function calculateItemRewardByRule({ rule, itemTotal, quantity, allowedTotalComm
793
794
  customer
794
795
  };
795
796
  }
796
- const splits = getRuleSplits(rule);
797
- if (!splits) return null;
798
797
  let totalPot = 0;
799
- if (rule.totalCommission.type === "percentage") totalPot = itemTotal * rule.totalCommission.value / 100;
800
- else totalPot = rule.totalCommission.value * quantity;
798
+ if (rule.totalCommission.type === "percentage") {
799
+ const commissionValue = typeof rule.totalCommission.value === "number" && Number.isFinite(rule.totalCommission.value) ? rule.totalCommission.value : null;
800
+ if (commissionValue == null) {
801
+ const partnerPercentInput = typeof rule.partnerPercent === "number" ? rule.partnerPercent : typeof rule.partnerSplit === "number" ? rule.partnerSplit : null;
802
+ if (partnerPercentInput == null || partnerPercentInput < 0 || partnerPercentInput > 100) return null;
803
+ const customerPercentInput = typeof rule.customerPercent === "number" ? rule.customerPercent : typeof rule.customerSplit === "number" ? rule.customerSplit : 100 - partnerPercentInput;
804
+ if (customerPercentInput == null || customerPercentInput < 0 || customerPercentInput > 100) return null;
805
+ const partner = itemTotal * partnerPercentInput / 100;
806
+ const customer = itemTotal * customerPercentInput / 100;
807
+ if (resolvedMaxAmount != null) {
808
+ const maxPotForLine = resolvedMaxAmount * quantity;
809
+ const totalForLine = partner + customer;
810
+ if (totalForLine > maxPotForLine && totalForLine > 0) {
811
+ const ratio = maxPotForLine / totalForLine;
812
+ return {
813
+ partner: Math.floor(partner * ratio),
814
+ customer: Math.floor(customer * ratio)
815
+ };
816
+ }
817
+ }
818
+ return {
819
+ partner: Math.floor(partner),
820
+ customer: Math.floor(customer)
821
+ };
822
+ }
823
+ totalPot = itemTotal * commissionValue / 100;
824
+ } else {
825
+ const splits = getRuleSplits(rule);
826
+ if (!splits) return null;
827
+ totalPot = rule.totalCommission.value * quantity;
828
+ if (resolvedMaxAmount != null) {
829
+ const maxPotForLine = resolvedMaxAmount * quantity;
830
+ if (totalPot > maxPotForLine) totalPot = maxPotForLine;
831
+ }
832
+ return {
833
+ partner: Math.floor(totalPot * splits.partnerSplit / 100),
834
+ customer: Math.floor(totalPot * splits.customerSplit / 100)
835
+ };
836
+ }
801
837
  if (resolvedMaxAmount != null) {
802
838
  const maxPotForLine = resolvedMaxAmount * quantity;
803
839
  if (totalPot > maxPotForLine) totalPot = maxPotForLine;
804
840
  }
841
+ const splits = getRuleSplits(rule);
842
+ if (!splits) return null;
805
843
  return {
806
844
  partner: Math.floor(totalPot * splits.partnerSplit / 100),
807
845
  customer: Math.floor(totalPot * splits.customerSplit / 100)
@@ -836,7 +874,8 @@ function selectBestRuleForItem({ rules, item, itemTotal, quantity, cartTotal, mi
836
874
  const eligibleRules = rules.filter((rule) => {
837
875
  if (!(rule?.totalCommission?.type ? allowedTypes.has(rule.totalCommission.type) : true)) return false;
838
876
  const resolvedMinOrderAmount = typeof minOrderAmount === "number" && Number.isFinite(minOrderAmount) ? minOrderAmount : typeof rule?.minOrderAmount === "number" && Number.isFinite(rule.minOrderAmount) ? rule.minOrderAmount : null;
839
- if (resolvedMinOrderAmount != null) return cartTotal >= resolvedMinOrderAmount;
877
+ const shouldApplyMinOrder = rule?.totalCommission?.type !== "fixed";
878
+ if (resolvedMinOrderAmount != null && shouldApplyMinOrder) return cartTotal >= resolvedMinOrderAmount;
840
879
  return true;
841
880
  });
842
881
  const productId = relationId$5(item.product);
@@ -889,7 +928,10 @@ function getProgramMinimumOrderAmount({ program, allowedTotalCommissionTypes })
889
928
  const rules = Array.isArray(program?.commissionRules) ? program.commissionRules : [];
890
929
  if (!rules.length) return null;
891
930
  const allowedTypes = allowedCommissionTypesSet(allowedTotalCommissionTypes);
892
- if (typeof program?.minOrderAmount === "number" && Number.isFinite(program.minOrderAmount)) return program.minOrderAmount;
931
+ if (rules.some((rule) => {
932
+ if (rule?.totalCommission?.type) return rule.totalCommission.type === "percentage" && allowedTypes.has(rule.totalCommission.type);
933
+ return true;
934
+ }) && typeof program?.minOrderAmount === "number" && Number.isFinite(program.minOrderAmount)) return program.minOrderAmount;
893
935
  const minValues = rules.filter((rule) => {
894
936
  if (rule?.totalCommission?.type) return allowedTypes.has(rule.totalCommission.type);
895
937
  return true;