manicanldes-backend/docs/campaign-discounts-api.md

3.5 KiB

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/coupon windows, eligible variant IDs, 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.