feat(discounts): define collection-only campaign discount policy
This commit is contained in:
parent
c8cf3515ba
commit
8431fbca4b
|
|
@ -0,0 +1,17 @@
|
||||||
|
# Campaign discounts (Phase 4D)
|
||||||
|
|
||||||
|
Campaign offers reuse existing coupons and exact checkout money rules. A campaign can have one coupon, created while the campaign is a draft. Coupon terms and association are immutable; the existing coupon status endpoint can pause/resume the code. Configure the campaign window first, then its coupon, then publish the updated campaign version.
|
||||||
|
|
||||||
|
`POST /api/v1/coupons` accepts the existing fixed/percentage coupon body plus optional `campaignId` and `campaignVersion`. Both must be supplied together. Linking requires `coupons.manage` and `campaigns.manage`, a draft in the caller's organization, and its current version. The coupon's start/end must fit inside the campaign window. Successful creation increments campaign version atomically, preventing publication of an offer not seen in the staff member's last read. Existing unlinked coupon requests retain their behavior.
|
||||||
|
|
||||||
|
Draft edits must keep the campaign window around its linked coupon. Terms cannot be replaced by another code in this milestone; create a new campaign/code for a different offer. The code remains the coupon creation identity; duplicate code or campaign association returns a conflict. No existing applied migration is edited.
|
||||||
|
|
||||||
|
Public campaign responses gain an optional `offer` with code, currency, kind, fixed amount/percentage basis points, minimum subtotal, and coupon start/end dates. It appears only when both the campaign and coupon are active and within their windows. Usage limits and per-customer eligibility remain authoritative at checkout; published terms do not promise a reservation or remaining use. Internal coupon IDs and usage counts are not public.
|
||||||
|
|
||||||
|
Customers continue submitting one `couponCode` to the existing checkout endpoint. A linked code additionally requires an active published campaign and a current collection. Only cart variants belonging to that collection contribute to the discount base and minimum-spend test. Other cart lines keep their full price. Percentage rounding and fixed-amount caps use the existing minor-unit arithmetic. No matching lines, draft/future/ended/cancelled campaigns, wrong currency, disabled/expired coupons, and exhausted limits fail before order/stock/cart writes.
|
||||||
|
|
||||||
|
New campaign-coupon order snapshots also retain campaign ID/slug, collection ID, campaign window, and exact eligible subtotal. These facts are frozen alongside coupon/price snapshots. Retrying a successful checkout returns its original order even after campaign cancellation or coupon disablement. New attempts use current eligibility. Order cancellation/expiry retain the existing usage-release behavior. Payment enablement, tax, shipping and gateway behavior do not change.
|
||||||
|
|
||||||
|
Acceptance: collection-only discounts in mixed carts; exact fixed/percentage amounts; scoped binding and version checks; window compatibility; public offer privacy; campaign/coupon cancellation gates; coupon quotas; checkout rollback/idempotency; immutable linkage and order evidence; unchanged generic coupons.
|
||||||
|
|
||||||
|
Deploy the additive migration before the application. Existing coupons receive a null campaign link and remain generic. New scoped foreign keys/unique constraints prevent cross-organization or duplicate binding; native guards freeze linked coupon terms and prevent invalid linked campaign rescheduling. Rollback to older code requires disabling campaign-linked coupons first so an older checkout cannot treat them as generic coupons. Preserve rows, order snapshots and audit history. No new dependencies or provider accounts are required.
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { minor } from '../checkout/money';
|
||||||
|
|
||||||
|
export type PricedCartLine = { variantId: string; lineTotal: string };
|
||||||
|
export function campaignEligibleSubtotal(
|
||||||
|
lines: readonly PricedCartLine[],
|
||||||
|
eligible: ReadonlySet<string>,
|
||||||
|
) {
|
||||||
|
return lines.reduce(
|
||||||
|
(sum, line) =>
|
||||||
|
sum + (eligible.has(line.variantId) ? minor(line.lineTotal) : 0n),
|
||||||
|
0n,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { campaignEligibleSubtotal } from '../src/coupons/campaign-coupon-policy';
|
||||||
|
import { discountFor } from '../src/checkout/money';
|
||||||
|
|
||||||
|
describe('campaign collection discount base', () => {
|
||||||
|
const lines = [
|
||||||
|
{ variantId: 'eligible', lineTotal: '10.10' },
|
||||||
|
{ variantId: 'outside', lineTotal: '89.90' },
|
||||||
|
{ variantId: 'eligible-second', lineTotal: '0.01' },
|
||||||
|
];
|
||||||
|
it('counts only collection merchandise with exact minor-unit precision', () => {
|
||||||
|
expect(
|
||||||
|
campaignEligibleSubtotal(lines, new Set(['eligible', 'eligible-second'])),
|
||||||
|
).toBe(1011n);
|
||||||
|
});
|
||||||
|
it('rounds a percentage on the eligible subtotal rather than the whole cart', () => {
|
||||||
|
const subtotal = campaignEligibleSubtotal(
|
||||||
|
lines,
|
||||||
|
new Set(['eligible', 'eligible-second']),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
discountFor(subtotal, {
|
||||||
|
kind: 'PERCENT',
|
||||||
|
amount: null,
|
||||||
|
percentBps: 5000,
|
||||||
|
}),
|
||||||
|
).toBe(506n);
|
||||||
|
});
|
||||||
|
it('caps a fixed discount at the eligible merchandise value', () => {
|
||||||
|
const subtotal = campaignEligibleSubtotal(lines, new Set(['eligible']));
|
||||||
|
expect(
|
||||||
|
discountFor(subtotal, {
|
||||||
|
kind: 'FIXED',
|
||||||
|
amount: '20.00',
|
||||||
|
percentBps: null,
|
||||||
|
}),
|
||||||
|
).toBe(1010n);
|
||||||
|
});
|
||||||
|
it('returns zero when the cart contains no campaign collection variants', () => {
|
||||||
|
expect(campaignEligibleSubtotal(lines, new Set())).toBe(0n);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue