@wtree/payload-ecommerce-coupon 3.78.5 → 3.78.7
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;
|
|
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;AA+B5D,eAAO,MAAM,gCAAgC,GAC3C,cAAc,4BAA4B,KACzC,gBAgaF,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -423,12 +423,22 @@ const createReferralCodesCollection = (pluginConfig) => {
|
|
|
423
423
|
};
|
|
424
424
|
};
|
|
425
425
|
//#endregion
|
|
426
|
+
//#region src/utilities/roundTo2.ts
|
|
427
|
+
/**
|
|
428
|
+
* Rounds a number to 2 decimal places (standard for monetary values).
|
|
429
|
+
*/
|
|
430
|
+
function roundTo2(value) {
|
|
431
|
+
return Math.round(value * 100) / 100;
|
|
432
|
+
}
|
|
433
|
+
//#endregion
|
|
426
434
|
//#region src/collections/createReferralProgramsCollection.ts
|
|
427
435
|
function toNumber(value) {
|
|
428
436
|
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
429
437
|
}
|
|
430
|
-
function
|
|
431
|
-
|
|
438
|
+
function normalizeLegacyX100Money(value) {
|
|
439
|
+
if (value == null) return null;
|
|
440
|
+
if (Number.isInteger(value) && Math.abs(value) >= 1e3) return roundTo2(value / 100);
|
|
441
|
+
return value;
|
|
432
442
|
}
|
|
433
443
|
const createReferralProgramsCollection = (pluginConfig) => {
|
|
434
444
|
const { collections, access, adminGroups, referralConfig, integration } = pluginConfig;
|
|
@@ -454,14 +464,14 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
454
464
|
},
|
|
455
465
|
hooks: { beforeChange: [({ data }) => {
|
|
456
466
|
if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new payload.APIError("At least one commission rule is required", 400);
|
|
457
|
-
const maxPartnerCommissionPerOrder = toNumber(data.maxPartnerCommissionPerOrder);
|
|
467
|
+
const maxPartnerCommissionPerOrder = normalizeLegacyX100Money(toNumber(data.maxPartnerCommissionPerOrder));
|
|
458
468
|
if (maxPartnerCommissionPerOrder != null && maxPartnerCommissionPerOrder < 0) throw new payload.APIError("Maximum commission per order for partner must be a non-negative number", 400);
|
|
459
|
-
const maxCustomerDiscountPerOrder = toNumber(data.maxCustomerDiscountPerOrder);
|
|
469
|
+
const maxCustomerDiscountPerOrder = normalizeLegacyX100Money(toNumber(data.maxCustomerDiscountPerOrder));
|
|
460
470
|
if (maxCustomerDiscountPerOrder != null && maxCustomerDiscountPerOrder < 0) throw new payload.APIError("Maximum discount for customer per order must be a non-negative number", 400);
|
|
461
|
-
const minOrderAmount = toNumber(data.minOrderAmount);
|
|
471
|
+
const minOrderAmount = normalizeLegacyX100Money(toNumber(data.minOrderAmount));
|
|
462
472
|
if (minOrderAmount != null && minOrderAmount < 0) throw new payload.APIError("Minimum Order Amount must be a non-negative number", 400);
|
|
463
|
-
data.maxPartnerCommissionPerOrder = maxPartnerCommissionPerOrder != null ?
|
|
464
|
-
data.maxCustomerDiscountPerOrder = maxCustomerDiscountPerOrder != null ?
|
|
473
|
+
data.maxPartnerCommissionPerOrder = maxPartnerCommissionPerOrder != null ? maxPartnerCommissionPerOrder : null;
|
|
474
|
+
data.maxCustomerDiscountPerOrder = maxCustomerDiscountPerOrder != null ? maxCustomerDiscountPerOrder : null;
|
|
465
475
|
data.minOrderAmount = minOrderAmount ?? null;
|
|
466
476
|
data.commissionRules = data.commissionRules.map((rule, index) => {
|
|
467
477
|
const r = rule;
|
|
@@ -492,8 +502,8 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
492
502
|
partnerSplit = partnerPctInput;
|
|
493
503
|
customerSplit = customerPctComputed;
|
|
494
504
|
} else {
|
|
495
|
-
const partnerAmountInput = toNumber(r.partnerAmount);
|
|
496
|
-
const customerAmountInput = toNumber(r.customerAmount);
|
|
505
|
+
const partnerAmountInput = normalizeLegacyX100Money(toNumber(r.partnerAmount));
|
|
506
|
+
const customerAmountInput = normalizeLegacyX100Money(toNumber(r.customerAmount));
|
|
497
507
|
const legacyPartnerSplitInput = toNumber(r.partnerSplit);
|
|
498
508
|
const legacyCustomerSplitInput = toNumber(r.customerSplit);
|
|
499
509
|
const hasNewFixedInputs = partnerAmountInput != null || customerAmountInput != null;
|
|
@@ -503,8 +513,8 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
503
513
|
if (customerAmountInput == null || customerAmountInput < 0) throw new payload.APIError(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`, 400);
|
|
504
514
|
partnerAmount = partnerAmountInput;
|
|
505
515
|
customerAmount = customerAmountInput;
|
|
506
|
-
partnerSplit =
|
|
507
|
-
customerSplit =
|
|
516
|
+
partnerSplit = partnerAmountInput;
|
|
517
|
+
customerSplit = customerAmountInput;
|
|
508
518
|
} else if (hasLegacyFixedInputs) {
|
|
509
519
|
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
520
|
const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? 100 - legacyPartnerSplitInput;
|
|
@@ -518,7 +528,11 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
518
528
|
return {
|
|
519
529
|
...rule,
|
|
520
530
|
appliesTo: appliesTo === "categories" ? "segments" : appliesTo,
|
|
521
|
-
totalCommission: {
|
|
531
|
+
totalCommission: {
|
|
532
|
+
type,
|
|
533
|
+
...typeof r.totalCommission.value === "number" ? { value: r.totalCommission.value } : {},
|
|
534
|
+
...typeof r.totalCommission.maxAmount === "number" ? { maxAmount: r.totalCommission.maxAmount } : {}
|
|
535
|
+
},
|
|
522
536
|
partnerPercent,
|
|
523
537
|
customerPercent,
|
|
524
538
|
partnerAmount,
|
|
@@ -547,19 +561,19 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
547
561
|
name: "maxPartnerCommissionPerOrder",
|
|
548
562
|
type: "number",
|
|
549
563
|
min: 0,
|
|
550
|
-
admin: { description: "Maximum commission per order for partner. Leave empty for no cap.
|
|
564
|
+
admin: { description: "Maximum commission per order for partner (normal currency value, ). Leave empty for no cap." }
|
|
551
565
|
},
|
|
552
566
|
{
|
|
553
567
|
name: "maxCustomerDiscountPerOrder",
|
|
554
568
|
type: "number",
|
|
555
569
|
min: 0,
|
|
556
|
-
admin: { description: "Maximum customer discount per order. Leave empty for no cap.
|
|
570
|
+
admin: { description: "Maximum customer discount per order (normal currency value, ). Leave empty for no cap." }
|
|
557
571
|
},
|
|
558
572
|
{
|
|
559
573
|
name: "minOrderAmount",
|
|
560
574
|
type: "number",
|
|
561
575
|
min: 0,
|
|
562
|
-
admin: { description: "Minimum cart subtotal required for this program. Leave empty for no minimum." }
|
|
576
|
+
admin: { description: "Minimum cart subtotal required for this program (normal currency value, ). Leave empty for no minimum." }
|
|
563
577
|
},
|
|
564
578
|
{
|
|
565
579
|
name: "commissionRules",
|
|
@@ -659,7 +673,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
659
673
|
min: 0,
|
|
660
674
|
admin: {
|
|
661
675
|
condition: (_, siblingData) => siblingData?.totalCommission?.type === "fixed",
|
|
662
|
-
description: "Fixed partner commission amount per item."
|
|
676
|
+
description: "Fixed partner commission amount per item (normal currency value)."
|
|
663
677
|
}
|
|
664
678
|
},
|
|
665
679
|
{
|
|
@@ -668,7 +682,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
668
682
|
min: 0,
|
|
669
683
|
admin: {
|
|
670
684
|
condition: (_, siblingData) => siblingData?.totalCommission?.type === "fixed",
|
|
671
|
-
description: "Fixed customer discount amount per item."
|
|
685
|
+
description: "Fixed customer discount amount per item (normal currency value)."
|
|
672
686
|
}
|
|
673
687
|
},
|
|
674
688
|
{
|
|
@@ -677,7 +691,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
677
691
|
min: 0,
|
|
678
692
|
admin: {
|
|
679
693
|
hidden: true,
|
|
680
|
-
description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount
|
|
694
|
+
description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount."
|
|
681
695
|
}
|
|
682
696
|
},
|
|
683
697
|
{
|
|
@@ -686,7 +700,7 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
686
700
|
min: 0,
|
|
687
701
|
admin: {
|
|
688
702
|
hidden: true,
|
|
689
|
-
description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount
|
|
703
|
+
description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount."
|
|
690
704
|
}
|
|
691
705
|
}
|
|
692
706
|
]
|
|
@@ -695,14 +709,6 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
695
709
|
timestamps: true
|
|
696
710
|
};
|
|
697
711
|
};
|
|
698
|
-
//#endregion
|
|
699
|
-
//#region src/utilities/roundTo2.ts
|
|
700
|
-
/**
|
|
701
|
-
* Rounds a number to 2 decimal places (standard for monetary values).
|
|
702
|
-
*/
|
|
703
|
-
function roundTo2(value) {
|
|
704
|
-
return Math.round(value * 100) / 100;
|
|
705
|
-
}
|
|
706
712
|
function normalizeCurrencyCode(currencyCode) {
|
|
707
713
|
if (!currencyCode) return "AED";
|
|
708
714
|
return currencyCode.toUpperCase();
|
|
@@ -793,15 +799,52 @@ function calculateItemRewardByRule({ rule, itemTotal, quantity, allowedTotalComm
|
|
|
793
799
|
customer
|
|
794
800
|
};
|
|
795
801
|
}
|
|
796
|
-
const splits = getRuleSplits(rule);
|
|
797
|
-
if (!splits) return null;
|
|
798
802
|
let totalPot = 0;
|
|
799
|
-
if (rule.totalCommission.type === "percentage")
|
|
800
|
-
|
|
803
|
+
if (rule.totalCommission.type === "percentage") {
|
|
804
|
+
const commissionValue = typeof rule.totalCommission.value === "number" && Number.isFinite(rule.totalCommission.value) ? rule.totalCommission.value : null;
|
|
805
|
+
if (commissionValue == null) {
|
|
806
|
+
const partnerPercentInput = typeof rule.partnerPercent === "number" ? rule.partnerPercent : typeof rule.partnerSplit === "number" ? rule.partnerSplit : null;
|
|
807
|
+
if (partnerPercentInput == null || partnerPercentInput < 0 || partnerPercentInput > 100) return null;
|
|
808
|
+
const customerPercentInput = typeof rule.customerPercent === "number" ? rule.customerPercent : typeof rule.customerSplit === "number" ? rule.customerSplit : 100 - partnerPercentInput;
|
|
809
|
+
if (customerPercentInput == null || customerPercentInput < 0 || customerPercentInput > 100) return null;
|
|
810
|
+
const partner = itemTotal * partnerPercentInput / 100;
|
|
811
|
+
const customer = itemTotal * customerPercentInput / 100;
|
|
812
|
+
if (resolvedMaxAmount != null) {
|
|
813
|
+
const maxPotForLine = resolvedMaxAmount * quantity;
|
|
814
|
+
const totalForLine = partner + customer;
|
|
815
|
+
if (totalForLine > maxPotForLine && totalForLine > 0) {
|
|
816
|
+
const ratio = maxPotForLine / totalForLine;
|
|
817
|
+
return {
|
|
818
|
+
partner: Math.floor(partner * ratio),
|
|
819
|
+
customer: Math.floor(customer * ratio)
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return {
|
|
824
|
+
partner: Math.floor(partner),
|
|
825
|
+
customer: Math.floor(customer)
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
totalPot = itemTotal * commissionValue / 100;
|
|
829
|
+
} else {
|
|
830
|
+
const splits = getRuleSplits(rule);
|
|
831
|
+
if (!splits) return null;
|
|
832
|
+
totalPot = rule.totalCommission.value * quantity;
|
|
833
|
+
if (resolvedMaxAmount != null) {
|
|
834
|
+
const maxPotForLine = resolvedMaxAmount * quantity;
|
|
835
|
+
if (totalPot > maxPotForLine) totalPot = maxPotForLine;
|
|
836
|
+
}
|
|
837
|
+
return {
|
|
838
|
+
partner: Math.floor(totalPot * splits.partnerSplit / 100),
|
|
839
|
+
customer: Math.floor(totalPot * splits.customerSplit / 100)
|
|
840
|
+
};
|
|
841
|
+
}
|
|
801
842
|
if (resolvedMaxAmount != null) {
|
|
802
843
|
const maxPotForLine = resolvedMaxAmount * quantity;
|
|
803
844
|
if (totalPot > maxPotForLine) totalPot = maxPotForLine;
|
|
804
845
|
}
|
|
846
|
+
const splits = getRuleSplits(rule);
|
|
847
|
+
if (!splits) return null;
|
|
805
848
|
return {
|
|
806
849
|
partner: Math.floor(totalPot * splits.partnerSplit / 100),
|
|
807
850
|
customer: Math.floor(totalPot * splits.customerSplit / 100)
|
|
@@ -886,10 +929,10 @@ function selectBestRuleForItem({ rules, item, itemTotal, quantity, cartTotal, mi
|
|
|
886
929
|
return best;
|
|
887
930
|
}
|
|
888
931
|
function getProgramMinimumOrderAmount({ program, allowedTotalCommissionTypes }) {
|
|
932
|
+
if (typeof program?.minOrderAmount === "number" && Number.isFinite(program.minOrderAmount)) return program.minOrderAmount;
|
|
889
933
|
const rules = Array.isArray(program?.commissionRules) ? program.commissionRules : [];
|
|
890
934
|
if (!rules.length) return null;
|
|
891
935
|
const allowedTypes = allowedCommissionTypesSet(allowedTotalCommissionTypes);
|
|
892
|
-
if (typeof program?.minOrderAmount === "number" && Number.isFinite(program.minOrderAmount)) return program.minOrderAmount;
|
|
893
936
|
const minValues = rules.filter((rule) => {
|
|
894
937
|
if (rule?.totalCommission?.type) return allowedTypes.has(rule.totalCommission.type);
|
|
895
938
|
return true;
|