Skip to content

Discounts Domain

The discounts domain provides promotions, coupon codes, and discount calculations. It supports percentage and fixed-amount discounts, buy-X-get-Y rules, eligibility conditions, and usage limits.

Terminal window
npx @backcap/cli add discounts

A reusable discount campaign with rules, validity period, and stacking configuration.

import { Promotion } from "./domains/discounts/domain/entities/promotion.entity";
const promo = Promotion.create({
id: crypto.randomUUID(),
name: "Summer Sale",
status: "draft",
rules: [{ type: "percentage", value: 15 }],
conditions: [{ type: "min_order_amount", value: 5000 }],
validFrom: new Date("2026-06-01"),
validUntil: new Date("2026-08-31"),
maxUses: 1000,
maxUsesPerCustomer: 1,
stackable: false,
});
const active = promo.unwrap().activate();

Single-use or limited-use codes tied to a promotion.

import { CouponCode } from "./domains/discounts/domain/entities/coupon-code.entity";
const coupon = CouponCode.create({
id: crypto.randomUUID(),
code: "SUMMER15",
promotionId: "promo-1",
maxRedemptions: 100,
currentRedemptions: 0,
});
  • PromotionStatusdraft, active, inactive
  • ValidityPeriod — start/end date range with active check
  • UsageLimit — max uses globally and per customer
  • DiscountType — percentage, fixed amount, buy-X-get-Y
  • Money — amount with currency for calculations
Use CaseDescription
CreatePromotionCreate a discount promotion with rules and conditions
ActivatePromotionSet promotion status to active
DeactivatePromotionSet promotion status to inactive
GetPromotionRetrieve promotion details
ListPromotionsQuery promotions with filters
CreateCouponGenerate a coupon code for a promotion
ValidateCouponCheck if a coupon code is valid
RedeemCouponApply a coupon with usage tracking
ApplyDiscountCalculate discount for an order context
  • IPromotionRepositoryfindById, findAll, save
  • ICouponRepositoryfindByCode, findById, save
import { createDiscountsService } from "./domains/discounts/contracts";
const discounts = createDiscountsService({
promotionRepository: myPromotionRepo,
couponRepository: myCouponRepo,
});
const result = await discounts.applyDiscount({
couponCode: "SUMMER15",
orderAmount: 8999,
orderCurrency: "USD",
customerId: "cust-1",
});
EventPayload
PromotionCreatedpromotionId, name
PromotionActivatedpromotionId
PromotionDeactivatedpromotionId
CouponCreatedcouponId, code, promotionId
CouponRedeemedcouponId, code, customerId