Files
evig/scripts/db/migrations/115_promo_codes.sql
Mao NakamotoandClaude Opus 4.8 f1d552f62a feat(promo): discount/gift-code foundation — schema + engine + seeds
Phase 1 slice 1 of the promo/gift-code system. Adds the redeemable-code
rail without wiring it into checkout yet (that's the next slice):

- Migration 115: promo_codes (typed: percent | fixed | gift_card, with
  scope, usage/per-user limits, validity window, gift-card balance) +
  promo_code_redemptions ledger. Type/scope are app enums, not DB CHECK
  lists (repo rule); only true invariants are CHECK-constrained. Seeds
  revamp100 (CHF 100 off) + revamp100p (100% off). Replays cleanly +
  idempotent (verified in a throwaway pg16).
- Pure discount engine (src/config/promo-codes.ts, 9 unit tests): caps
  fixed/gift-card at the order total (never negative), percent floors,
  enforces scope/min-order/expiry/limits.
- Redemption service (validate → compute; record consumes atomically,
  decrementing gift-card balance under a tx so limited codes can't
  overspend).

Distinct from the referral `coupons` table (per-user auto-rewards).
Next: admin issuance UI + checkout redemption (incl. 100%-off →
skip-gateway) across marketplace/workshop/appointment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 12:07:33 +02:00

59 lines
3.2 KiB
SQL

-- 115_promo_codes.sql
-- Admin-issued discount + gift-card codes (redeemable at checkout across flows).
-- Distinct from the referral `coupons` table (per-user auto-issued rewards):
-- promo_codes are shareable, multi-use, typed, scoped, and limit-controlled.
--
-- Type/scope are app-level enums validated via zod/config (src/config/promo-codes.ts),
-- NOT SQL CHECK lists (per the repo rule — hand-synced CHECK enums drift). Only
-- true invariants (ranges, money non-negativity) are enforced here.
CREATE TABLE IF NOT EXISTS promo_codes (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
code varchar(64) NOT NULL UNIQUE,
type varchar(20) NOT NULL, -- 'percent' | 'fixed' | 'gift_card'
percent integer, -- type=percent: 1..100
amount_cents integer, -- type=fixed/gift_card: value in cents
balance_cents integer, -- gift_card: remaining balance
scope varchar(20) NOT NULL DEFAULT 'all', -- all|marketplace|membership|workshop|service
min_order_cents integer NOT NULL DEFAULT 0,
max_redemptions integer, -- null = unlimited
per_user_limit integer, -- null = unlimited
redeemed_count integer NOT NULL DEFAULT 0,
valid_from timestamptz,
valid_until timestamptz,
is_active boolean NOT NULL DEFAULT true,
created_by uuid REFERENCES users(id) ON DELETE SET NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT promo_percent_range CHECK (percent IS NULL OR (percent >= 1 AND percent <= 100)),
CONSTRAINT promo_amount_nonneg CHECK (amount_cents IS NULL OR amount_cents >= 0),
CONSTRAINT promo_balance_nonneg CHECK (balance_cents IS NULL OR balance_cents >= 0),
CONSTRAINT promo_min_order_nonneg CHECK (min_order_cents >= 0),
CONSTRAINT promo_redeemed_nonneg CHECK (redeemed_count >= 0)
);
CREATE TABLE IF NOT EXISTS promo_code_redemptions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
promo_code_id uuid NOT NULL REFERENCES promo_codes(id) ON DELETE CASCADE,
user_id uuid REFERENCES users(id) ON DELETE SET NULL,
order_ref varchar(128), -- order / registration / appointment id
scope varchar(20) NOT NULL,
amount_discounted_cents integer NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT promo_redemption_amount_nonneg CHECK (amount_discounted_cents >= 0)
);
CREATE INDEX IF NOT EXISTS idx_promo_redemptions_code ON promo_code_redemptions(promo_code_id);
CREATE INDEX IF NOT EXISTS idx_promo_redemptions_user ON promo_code_redemptions(user_id);
-- Default codes: revamp100p = 100% off, revamp100 = CHF 100 off (capped at total).
INSERT INTO promo_codes (code, type, percent, is_active)
VALUES ('revamp100p', 'percent', 100, true)
ON CONFLICT (code) DO NOTHING;
INSERT INTO promo_codes (code, type, amount_cents, is_active)
VALUES ('revamp100', 'fixed', 10000, true)
ON CONFLICT (code) DO NOTHING;