@wtree/payload-ecommerce-coupon 3.78.9 → 3.78.10
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.
- package/dist/collections/createReferralProgramsCollection.d.ts.map +1 -1
- package/dist/index.js +81 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -459,102 +459,88 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
459
459
|
update: access.isAdmin || (() => false),
|
|
460
460
|
delete: access.isAdmin || (() => false)
|
|
461
461
|
},
|
|
462
|
-
hooks: {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
if (
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
splitWarning
|
|
541
|
-
};
|
|
542
|
-
});
|
|
543
|
-
return data;
|
|
544
|
-
}],
|
|
545
|
-
afterRead: [({ doc }) => {
|
|
546
|
-
if (!doc) return doc;
|
|
547
|
-
const unscale = (value) => {
|
|
548
|
-
const n = toNumber(value);
|
|
549
|
-
if (n == null) return null;
|
|
550
|
-
return Math.round(n / 100 * 100) / 100;
|
|
462
|
+
hooks: { beforeChange: [({ data }) => {
|
|
463
|
+
if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new APIError("At least one commission rule is required", 400);
|
|
464
|
+
const rawMaxPartner = toNumber(data.maxPartnerCommissionPerOrder);
|
|
465
|
+
if (rawMaxPartner != null && rawMaxPartner < 0) throw new APIError("Maximum commission per order for partner must be a non-negative number", 400);
|
|
466
|
+
const rawMaxCustomer = toNumber(data.maxCustomerDiscountPerOrder);
|
|
467
|
+
if (rawMaxCustomer != null && rawMaxCustomer < 0) throw new APIError("Maximum discount for customer per order must be a non-negative number", 400);
|
|
468
|
+
const rawMinOrder = toNumber(data.minOrderAmount);
|
|
469
|
+
if (rawMinOrder != null && rawMinOrder < 0) throw new APIError("Minimum Order Amount must be a non-negative number", 400);
|
|
470
|
+
data.maxPartnerCommissionPerOrder = rawMaxPartner ?? null;
|
|
471
|
+
data.maxCustomerDiscountPerOrder = rawMaxCustomer ?? null;
|
|
472
|
+
data.minOrderAmount = rawMinOrder ?? null;
|
|
473
|
+
data.commissionRules = data.commissionRules.map((rule, index) => {
|
|
474
|
+
const r = rule;
|
|
475
|
+
if (!r.totalCommission) throw new APIError(`Commission rule ${index + 1}: Total Commission is required`, 400);
|
|
476
|
+
if (!r.totalCommission.type || !allowedTotalCommissionTypes.includes(r.totalCommission.type)) throw new APIError(`Commission rule ${index + 1}: Total Commission type must be one of ${allowedTotalCommissionTypes.join(", ")}`, 400);
|
|
477
|
+
const type = r.totalCommission.type;
|
|
478
|
+
const appliesTo = r.appliesTo ?? "all";
|
|
479
|
+
if (appliesTo === "products" && (!r.products || r.products.length === 0)) throw new APIError(`Commission rule ${index + 1}: At least one product is required`, 400);
|
|
480
|
+
if ((appliesTo === "segments" || appliesTo === "categories") && (!r.categories || r.categories.length === 0) && (!r.tags || r.tags.length === 0)) throw new APIError(`Commission rule ${index + 1}: At least one category or tag is required`, 400);
|
|
481
|
+
let partnerSplit;
|
|
482
|
+
let customerSplit;
|
|
483
|
+
let partnerPercent = null;
|
|
484
|
+
let customerPercent = null;
|
|
485
|
+
let partnerAmount = null;
|
|
486
|
+
let customerAmount = null;
|
|
487
|
+
let splitWarning = null;
|
|
488
|
+
if (type === "percentage") {
|
|
489
|
+
const partnerPctInput = toNumber(r.partnerPercent) ?? toNumber(r.partnerSplit);
|
|
490
|
+
const customerPctInput = toNumber(r.customerPercent) ?? toNumber(r.customerSplit);
|
|
491
|
+
if (partnerPctInput == null || partnerPctInput < 0 || partnerPctInput > 100) throw new APIError(`Commission rule ${index + 1}: Partner Split must be between 0 and 100`, 400);
|
|
492
|
+
if (customerPctInput != null && (customerPctInput < 0 || customerPctInput > 100)) throw new APIError(`Commission rule ${index + 1}: Customer percentage must be between 0 and 100`, 400);
|
|
493
|
+
const customerPctComputed = customerPctInput != null ? customerPctInput : 100 - partnerPctInput;
|
|
494
|
+
const percentTotal = partnerPctInput + customerPctComputed;
|
|
495
|
+
if (percentTotal > 100) throw new APIError(`Commission rule ${index + 1}: Partner percentage + Customer percentage cannot exceed 100`, 400);
|
|
496
|
+
if (percentTotal > 50) splitWarning = `High total split configured: ${percentTotal}% (partner + customer).`;
|
|
497
|
+
partnerPercent = partnerPctInput;
|
|
498
|
+
customerPercent = customerPctComputed;
|
|
499
|
+
partnerSplit = partnerPctInput;
|
|
500
|
+
customerSplit = customerPctComputed;
|
|
501
|
+
} else {
|
|
502
|
+
const partnerAmountInput = toNumber(r.partnerAmount);
|
|
503
|
+
const customerAmountInput = toNumber(r.customerAmount);
|
|
504
|
+
const legacyPartnerSplitInput = toNumber(r.partnerSplit);
|
|
505
|
+
const legacyCustomerSplitInput = toNumber(r.customerSplit);
|
|
506
|
+
const hasNewFixedInputs = partnerAmountInput != null || customerAmountInput != null;
|
|
507
|
+
const hasLegacyFixedInputs = legacyPartnerSplitInput != null || legacyCustomerSplitInput != null;
|
|
508
|
+
if (hasNewFixedInputs) {
|
|
509
|
+
if (partnerAmountInput == null || partnerAmountInput < 0) throw new APIError(`Commission rule ${index + 1}: Partner fixed amount must be a non-negative number`, 400);
|
|
510
|
+
if (customerAmountInput == null || customerAmountInput < 0) throw new APIError(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`, 400);
|
|
511
|
+
partnerAmount = partnerAmountInput;
|
|
512
|
+
customerAmount = customerAmountInput;
|
|
513
|
+
partnerSplit = partnerAmountInput;
|
|
514
|
+
customerSplit = customerAmountInput;
|
|
515
|
+
} else if (hasLegacyFixedInputs) {
|
|
516
|
+
if (legacyPartnerSplitInput == null || legacyPartnerSplitInput < 0) throw new APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`, 400);
|
|
517
|
+
const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? 100 - legacyPartnerSplitInput;
|
|
518
|
+
if (resolvedLegacyCustomerSplit == null || resolvedLegacyCustomerSplit < 0) throw new APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`, 400);
|
|
519
|
+
partnerSplit = legacyPartnerSplitInput;
|
|
520
|
+
customerSplit = resolvedLegacyCustomerSplit;
|
|
521
|
+
partnerAmount = null;
|
|
522
|
+
customerAmount = null;
|
|
523
|
+
} else throw new APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be provided`, 400);
|
|
524
|
+
}
|
|
525
|
+
return {
|
|
526
|
+
...rule,
|
|
527
|
+
appliesTo: appliesTo === "categories" ? "segments" : appliesTo,
|
|
528
|
+
totalCommission: {
|
|
529
|
+
type,
|
|
530
|
+
...typeof r.totalCommission.value === "number" ? { value: r.totalCommission.value } : {},
|
|
531
|
+
...typeof r.totalCommission.maxAmount === "number" ? { maxAmount: r.totalCommission.maxAmount } : {}
|
|
532
|
+
},
|
|
533
|
+
partnerPercent,
|
|
534
|
+
customerPercent,
|
|
535
|
+
partnerAmount,
|
|
536
|
+
customerAmount,
|
|
537
|
+
partnerSplit,
|
|
538
|
+
customerSplit,
|
|
539
|
+
splitWarning
|
|
551
540
|
};
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
return doc;
|
|
556
|
-
}]
|
|
557
|
-
},
|
|
541
|
+
});
|
|
542
|
+
return data;
|
|
543
|
+
}] },
|
|
558
544
|
fields: [
|
|
559
545
|
{
|
|
560
546
|
name: "name",
|