@wtree/payload-ecommerce-coupon 3.78.0 → 3.78.2
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,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CollectionConfig } from "payload";
|
|
2
2
|
import type { SanitizedCouponPluginOptions } from "../types";
|
|
3
3
|
export declare const createReferralProgramsCollection: (pluginConfig: SanitizedCouponPluginOptions) => CollectionConfig;
|
|
4
4
|
//# sourceMappingURL=createReferralProgramsCollection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createReferralProgramsCollection.d.ts","sourceRoot":"","sources":["../../src/collections/createReferralProgramsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"createReferralProgramsCollection.d.ts","sourceRoot":"","sources":["../../src/collections/createReferralProgramsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE1D,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AA0B7D,eAAO,MAAM,gCAAgC,GAC3C,cAAc,4BAA4B,KACzC,gBAgYF,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
enumerable: true
|
|
23
23
|
}) : target, mod));
|
|
24
24
|
//#endregion
|
|
25
|
+
let payload = require("payload");
|
|
25
26
|
//#region src/collections/createCouponsCollection.ts
|
|
26
27
|
const createCouponsCollection = (pluginConfig) => {
|
|
27
28
|
const { collections, access, defaultCurrency, adminGroups, integration } = pluginConfig;
|
|
@@ -452,26 +453,21 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
452
453
|
delete: access.isAdmin || (() => false)
|
|
453
454
|
},
|
|
454
455
|
hooks: { beforeChange: [({ data }) => {
|
|
455
|
-
if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new
|
|
456
|
+
if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new payload.APIError("At least one commission rule is required", 400);
|
|
456
457
|
const maxAmount = toNumber(data.maxAmount);
|
|
457
|
-
if (maxAmount != null && maxAmount < 0) throw new
|
|
458
|
+
if (maxAmount != null && maxAmount < 0) throw new payload.APIError("Max Amount must be a non-negative number", 400);
|
|
458
459
|
const minOrderAmount = toNumber(data.minOrderAmount);
|
|
459
|
-
if (minOrderAmount != null && minOrderAmount < 0) throw new
|
|
460
|
+
if (minOrderAmount != null && minOrderAmount < 0) throw new payload.APIError("Minimum Order Amount must be a non-negative number", 400);
|
|
460
461
|
data.maxAmount = maxAmount ?? null;
|
|
461
462
|
data.minOrderAmount = minOrderAmount ?? null;
|
|
462
463
|
data.commissionRules = data.commissionRules.map((rule, index) => {
|
|
463
464
|
const r = rule;
|
|
464
|
-
if (!r.totalCommission) throw new
|
|
465
|
-
if (!r.totalCommission.type || !allowedTotalCommissionTypes.includes(r.totalCommission.type)) throw new
|
|
465
|
+
if (!r.totalCommission) throw new payload.APIError(`Commission rule ${index + 1}: Total Commission is required`, 400);
|
|
466
|
+
if (!r.totalCommission.type || !allowedTotalCommissionTypes.includes(r.totalCommission.type)) throw new payload.APIError(`Commission rule ${index + 1}: Total Commission type must be one of ${allowedTotalCommissionTypes.join(", ")}`, 400);
|
|
466
467
|
const type = r.totalCommission.type;
|
|
467
|
-
const totalValue = toNumber(r.totalCommission.value);
|
|
468
|
-
if (type === "percentage") {
|
|
469
|
-
if (totalValue == null || totalValue < 0) throw new Error(`Commission rule ${index + 1}: Total Commission value must be a non-negative number`);
|
|
470
|
-
if (totalValue > 100) throw new Error(`Commission rule ${index + 1}: Percentage Total Commission cannot exceed 100`);
|
|
471
|
-
}
|
|
472
468
|
const appliesTo = r.appliesTo ?? "all";
|
|
473
|
-
if (appliesTo === "products" && (!r.products || r.products.length === 0)) throw new
|
|
474
|
-
if ((appliesTo === "segments" || appliesTo === "categories") && (!r.categories || r.categories.length === 0) && (!r.tags || r.tags.length === 0)) throw new
|
|
469
|
+
if (appliesTo === "products" && (!r.products || r.products.length === 0)) throw new payload.APIError(`Commission rule ${index + 1}: At least one product is required`, 400);
|
|
470
|
+
if ((appliesTo === "segments" || appliesTo === "categories") && (!r.categories || r.categories.length === 0) && (!r.tags || r.tags.length === 0)) throw new payload.APIError(`Commission rule ${index + 1}: At least one category or tag is required`, 400);
|
|
475
471
|
let partnerSplit;
|
|
476
472
|
let customerSplit;
|
|
477
473
|
let partnerPercent = null;
|
|
@@ -482,11 +478,11 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
482
478
|
if (type === "percentage") {
|
|
483
479
|
const partnerPctInput = toNumber(r.partnerPercent) ?? toNumber(r.partnerSplit);
|
|
484
480
|
const customerPctInput = toNumber(r.customerPercent) ?? toNumber(r.customerSplit);
|
|
485
|
-
if (partnerPctInput == null || partnerPctInput < 0 || partnerPctInput > 100) throw new
|
|
486
|
-
if (customerPctInput != null && (customerPctInput < 0 || customerPctInput > 100)) throw new
|
|
481
|
+
if (partnerPctInput == null || partnerPctInput < 0 || partnerPctInput > 100) throw new payload.APIError(`Commission rule ${index + 1}: Partner Split must be between 0 and 100`, 400);
|
|
482
|
+
if (customerPctInput != null && (customerPctInput < 0 || customerPctInput > 100)) throw new payload.APIError(`Commission rule ${index + 1}: Customer percentage must be between 0 and 100`, 400);
|
|
487
483
|
const customerPctComputed = customerPctInput != null ? customerPctInput : 100 - partnerPctInput;
|
|
488
484
|
const percentTotal = partnerPctInput + customerPctComputed;
|
|
489
|
-
if (percentTotal > 100) throw new
|
|
485
|
+
if (percentTotal > 100) throw new payload.APIError(`Commission rule ${index + 1}: Partner percentage + Customer percentage cannot exceed 100`, 400);
|
|
490
486
|
if (percentTotal > 50) splitWarning = `High total split configured: ${percentTotal}% (partner + customer).`;
|
|
491
487
|
partnerPercent = partnerPctInput;
|
|
492
488
|
customerPercent = customerPctComputed;
|
|
@@ -500,30 +496,26 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
500
496
|
const hasNewFixedInputs = partnerAmountInput != null || customerAmountInput != null;
|
|
501
497
|
const hasLegacyFixedInputs = legacyPartnerSplitInput != null || legacyCustomerSplitInput != null;
|
|
502
498
|
if (hasNewFixedInputs) {
|
|
503
|
-
if (partnerAmountInput == null || partnerAmountInput < 0) throw new
|
|
504
|
-
if (customerAmountInput == null || customerAmountInput < 0) throw new
|
|
499
|
+
if (partnerAmountInput == null || partnerAmountInput < 0) throw new payload.APIError(`Commission rule ${index + 1}: Partner fixed amount must be a non-negative number`, 400);
|
|
500
|
+
if (customerAmountInput == null || customerAmountInput < 0) throw new payload.APIError(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`, 400);
|
|
505
501
|
partnerAmount = partnerAmountInput;
|
|
506
502
|
customerAmount = customerAmountInput;
|
|
507
503
|
partnerSplit = toCents(partnerAmountInput);
|
|
508
504
|
customerSplit = toCents(customerAmountInput);
|
|
509
505
|
} else if (hasLegacyFixedInputs) {
|
|
510
|
-
if (legacyPartnerSplitInput == null || legacyPartnerSplitInput < 0) throw new
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
if (resolvedLegacyCustomerSplit == null || resolvedLegacyCustomerSplit < 0) throw new Error(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`);
|
|
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);
|
|
507
|
+
const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? 100 - legacyPartnerSplitInput;
|
|
508
|
+
if (resolvedLegacyCustomerSplit == null || resolvedLegacyCustomerSplit < 0) throw new payload.APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`, 400);
|
|
514
509
|
partnerSplit = legacyPartnerSplitInput;
|
|
515
510
|
customerSplit = resolvedLegacyCustomerSplit;
|
|
516
511
|
partnerAmount = null;
|
|
517
512
|
customerAmount = null;
|
|
518
|
-
} else throw new
|
|
513
|
+
} else throw new payload.APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be provided`, 400);
|
|
519
514
|
}
|
|
520
515
|
return {
|
|
521
516
|
...rule,
|
|
522
517
|
appliesTo: appliesTo === "categories" ? "segments" : appliesTo,
|
|
523
|
-
totalCommission: {
|
|
524
|
-
type,
|
|
525
|
-
value: type === "percentage" ? totalValue : null
|
|
526
|
-
},
|
|
518
|
+
totalCommission: { type },
|
|
527
519
|
partnerPercent,
|
|
528
520
|
customerPercent,
|
|
529
521
|
partnerAmount,
|
|
@@ -687,16 +679,6 @@ const createReferralProgramsCollection = (pluginConfig) => {
|
|
|
687
679
|
hidden: true,
|
|
688
680
|
description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount in cents."
|
|
689
681
|
}
|
|
690
|
-
},
|
|
691
|
-
{
|
|
692
|
-
name: "splitWarning",
|
|
693
|
-
type: "text",
|
|
694
|
-
virtual: true,
|
|
695
|
-
admin: {
|
|
696
|
-
readOnly: true,
|
|
697
|
-
condition: (_, siblingData) => siblingData?.totalCommission?.type === "percentage",
|
|
698
|
-
description: "Non-blocking warning shown when partnerPercent + customerPercent is greater than 50%."
|
|
699
|
-
}
|
|
700
682
|
}
|
|
701
683
|
]
|
|
702
684
|
}
|