/admin/coupons. They support percentage and fixed-amount discounts with optional minimum purchase requirements, maximum usage caps, and validity windows.
Coupons Table Fields
Discount Types
| Type | Value | Example |
|---|---|---|
| Percentage | 'percentage' | discount_value: 15 → 15% off the subtotal |
| Fixed Amount | 'fixed' | discount_value: 50 → $50 MXN off the subtotal |
CRUD Functions
getAllCoupons
used_count.
createCoupon
CouponFormData is identical to AdminCoupon minus the used_count (initialized to 0 on creation).
updateCoupon
code is the primary key. Changing the code requires deleting and recreating the coupon.
deleteCoupon
deleteCoupon performs a soft delete — it sets is_active = false rather than deleting the row. This preserves the coupon’s used_count history and any order records that reference the code.Coupon Validation Flow (Storefront)
In the storefront checkout, coupons are validated viacoupons.service.ts:
Database lookup
The service queries the
coupons table for a row matching code where is_active = true.Business rule checks
The service validates:
valid_from≤ current time (if set)valid_until≥ current time (if set)used_count<max_uses(ifmax_usesis not null)- Cart subtotal ≥
min_purchase
SQL: Create a Test Coupon
AI Coupon Generation
generateCouponMagic
marketing-intelligence edge function with action: 'generate_coupon'. Returns an AI-suggested coupon code, discount value, and description tuned for the specified business goal:
| Goal | Intent |
|---|---|
conversion | Convert first-time visitors or cart abandoners |
retention | Win back lapsed customers |
clearance | Move excess inventory quickly |
forecastCouponImpact
marketing-intelligence with action: 'forecast_impact'. Analyzes the coupon’s parameters against historical order data to forecast estimated customer reach, potential revenue impact, and a strategic recommendation before the coupon goes live.