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