@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.
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { APIError } from "payload";
1
2
  //#region \0rolldown/runtime.js
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
@@ -451,26 +452,21 @@ const createReferralProgramsCollection = (pluginConfig) => {
451
452
  delete: access.isAdmin || (() => false)
452
453
  },
453
454
  hooks: { beforeChange: [({ data }) => {
454
- if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new Error("At least one commission rule is required");
455
+ if (!data.commissionRules || !Array.isArray(data.commissionRules) || data.commissionRules.length === 0) throw new APIError("At least one commission rule is required", 400);
455
456
  const maxAmount = toNumber(data.maxAmount);
456
- if (maxAmount != null && maxAmount < 0) throw new Error("Max Amount must be a non-negative number");
457
+ if (maxAmount != null && maxAmount < 0) throw new APIError("Max Amount must be a non-negative number", 400);
457
458
  const minOrderAmount = toNumber(data.minOrderAmount);
458
- if (minOrderAmount != null && minOrderAmount < 0) throw new Error("Minimum Order Amount must be a non-negative number");
459
+ if (minOrderAmount != null && minOrderAmount < 0) throw new APIError("Minimum Order Amount must be a non-negative number", 400);
459
460
  data.maxAmount = maxAmount ?? null;
460
461
  data.minOrderAmount = minOrderAmount ?? null;
461
462
  data.commissionRules = data.commissionRules.map((rule, index) => {
462
463
  const r = rule;
463
- if (!r.totalCommission) throw new Error(`Commission rule ${index + 1}: Total Commission is required`);
464
- if (!r.totalCommission.type || !allowedTotalCommissionTypes.includes(r.totalCommission.type)) throw new Error(`Commission rule ${index + 1}: Total Commission type must be one of ${allowedTotalCommissionTypes.join(", ")}`);
464
+ if (!r.totalCommission) throw new APIError(`Commission rule ${index + 1}: Total Commission is required`, 400);
465
+ 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);
465
466
  const type = r.totalCommission.type;
466
- const totalValue = toNumber(r.totalCommission.value);
467
- if (type === "percentage") {
468
- if (totalValue == null || totalValue < 0) throw new Error(`Commission rule ${index + 1}: Total Commission value must be a non-negative number`);
469
- if (totalValue > 100) throw new Error(`Commission rule ${index + 1}: Percentage Total Commission cannot exceed 100`);
470
- }
471
467
  const appliesTo = r.appliesTo ?? "all";
472
- if (appliesTo === "products" && (!r.products || r.products.length === 0)) throw new Error(`Commission rule ${index + 1}: At least one product is required`);
473
- if ((appliesTo === "segments" || appliesTo === "categories") && (!r.categories || r.categories.length === 0) && (!r.tags || r.tags.length === 0)) throw new Error(`Commission rule ${index + 1}: At least one category or tag is required`);
468
+ if (appliesTo === "products" && (!r.products || r.products.length === 0)) throw new APIError(`Commission rule ${index + 1}: At least one product is required`, 400);
469
+ 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);
474
470
  let partnerSplit;
475
471
  let customerSplit;
476
472
  let partnerPercent = null;
@@ -481,11 +477,11 @@ const createReferralProgramsCollection = (pluginConfig) => {
481
477
  if (type === "percentage") {
482
478
  const partnerPctInput = toNumber(r.partnerPercent) ?? toNumber(r.partnerSplit);
483
479
  const customerPctInput = toNumber(r.customerPercent) ?? toNumber(r.customerSplit);
484
- if (partnerPctInput == null || partnerPctInput < 0 || partnerPctInput > 100) throw new Error(`Commission rule ${index + 1}: Partner Split must be between 0 and 100`);
485
- if (customerPctInput != null && (customerPctInput < 0 || customerPctInput > 100)) throw new Error(`Commission rule ${index + 1}: Customer percentage must be between 0 and 100`);
480
+ if (partnerPctInput == null || partnerPctInput < 0 || partnerPctInput > 100) throw new APIError(`Commission rule ${index + 1}: Partner Split must be between 0 and 100`, 400);
481
+ if (customerPctInput != null && (customerPctInput < 0 || customerPctInput > 100)) throw new APIError(`Commission rule ${index + 1}: Customer percentage must be between 0 and 100`, 400);
486
482
  const customerPctComputed = customerPctInput != null ? customerPctInput : 100 - partnerPctInput;
487
483
  const percentTotal = partnerPctInput + customerPctComputed;
488
- if (percentTotal > 100) throw new Error(`Commission rule ${index + 1}: Partner percentage + Customer percentage cannot exceed 100`);
484
+ if (percentTotal > 100) throw new APIError(`Commission rule ${index + 1}: Partner percentage + Customer percentage cannot exceed 100`, 400);
489
485
  if (percentTotal > 50) splitWarning = `High total split configured: ${percentTotal}% (partner + customer).`;
490
486
  partnerPercent = partnerPctInput;
491
487
  customerPercent = customerPctComputed;
@@ -499,30 +495,26 @@ const createReferralProgramsCollection = (pluginConfig) => {
499
495
  const hasNewFixedInputs = partnerAmountInput != null || customerAmountInput != null;
500
496
  const hasLegacyFixedInputs = legacyPartnerSplitInput != null || legacyCustomerSplitInput != null;
501
497
  if (hasNewFixedInputs) {
502
- if (partnerAmountInput == null || partnerAmountInput < 0) throw new Error(`Commission rule ${index + 1}: Partner fixed amount must be a non-negative number`);
503
- if (customerAmountInput == null || customerAmountInput < 0) throw new Error(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`);
498
+ if (partnerAmountInput == null || partnerAmountInput < 0) throw new APIError(`Commission rule ${index + 1}: Partner fixed amount must be a non-negative number`, 400);
499
+ if (customerAmountInput == null || customerAmountInput < 0) throw new APIError(`Commission rule ${index + 1}: Customer fixed amount must be a non-negative number`, 400);
504
500
  partnerAmount = partnerAmountInput;
505
501
  customerAmount = customerAmountInput;
506
502
  partnerSplit = toCents(partnerAmountInput);
507
503
  customerSplit = toCents(customerAmountInput);
508
504
  } else if (hasLegacyFixedInputs) {
509
- if (legacyPartnerSplitInput == null || legacyPartnerSplitInput < 0) throw new Error(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be non-negative numbers`);
510
- const legacyHasTotalValue = toNumber(r.totalCommission?.value) != null;
511
- const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? (legacyHasTotalValue ? 100 - legacyPartnerSplitInput : null);
512
- 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`);
505
+ 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);
506
+ const resolvedLegacyCustomerSplit = legacyCustomerSplitInput ?? 100 - legacyPartnerSplitInput;
507
+ 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);
513
508
  partnerSplit = legacyPartnerSplitInput;
514
509
  customerSplit = resolvedLegacyCustomerSplit;
515
510
  partnerAmount = null;
516
511
  customerAmount = null;
517
- } else throw new Error(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be provided`);
512
+ } else throw new APIError(`Commission rule ${index + 1}: For fixed commissions, both partner and customer values must be provided`, 400);
518
513
  }
519
514
  return {
520
515
  ...rule,
521
516
  appliesTo: appliesTo === "categories" ? "segments" : appliesTo,
522
- totalCommission: {
523
- type,
524
- value: type === "percentage" ? totalValue : null
525
- },
517
+ totalCommission: { type },
526
518
  partnerPercent,
527
519
  customerPercent,
528
520
  partnerAmount,
@@ -686,16 +678,6 @@ const createReferralProgramsCollection = (pluginConfig) => {
686
678
  hidden: true,
687
679
  description: "Canonical storage field. Percentage mode: percent. Fixed mode: amount in cents."
688
680
  }
689
- },
690
- {
691
- name: "splitWarning",
692
- type: "text",
693
- virtual: true,
694
- admin: {
695
- readOnly: true,
696
- condition: (_, siblingData) => siblingData?.totalCommission?.type === "percentage",
697
- description: "Non-blocking warning shown when partnerPercent + customerPercent is greater than 50%."
698
- }
699
681
  }
700
682
  ]
701
683
  }