Admins make promo codes for a package (Admin → Promókódok: batch, note, copy all), list them filtered (unused by default, used, revoked; by package; by code, note or redeemer) and withdraw unused ones. A logged-in user redeems a code once (header link, /bevaltas?kod=... prefilled, packages page, and the analysis and question offers point there) and gets the package as an order with source 'promo'. Failed redemptions are rate limited. Production purchases are off: MOCK_PAYMENTS now defaults to false in the deploy compose, so package cards say 'Hamarosan'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
24 lines
1.1 KiB
SQL
24 lines
1.1 KiB
SQL
-- Promo codes: an admin makes them for a package, a user redeems one once
|
|
-- and gets the package (a `package_orders` row with source `promo`).
|
|
CREATE TABLE promo_codes (
|
|
id uuid PRIMARY KEY,
|
|
-- Upper case, without separators (shown in groups of four).
|
|
code text NOT NULL UNIQUE CHECK (code ~ '^[2-9A-HJKMNP-Z]{8,32}$'),
|
|
package text NOT NULL,
|
|
-- Who it is for or which campaign (admin only).
|
|
note text,
|
|
created_by uuid REFERENCES users (id) ON DELETE SET NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
redeemed_by uuid REFERENCES users (id) ON DELETE SET NULL,
|
|
redeemed_at timestamptz,
|
|
order_id uuid REFERENCES package_orders (id) ON DELETE SET NULL,
|
|
-- An unused code can be withdrawn.
|
|
revoked_at timestamptz,
|
|
CHECK (redeemed_at IS NULL OR revoked_at IS NULL)
|
|
);
|
|
CREATE INDEX promo_codes_created_idx ON promo_codes (created_at DESC);
|
|
|
|
ALTER TABLE package_orders DROP CONSTRAINT package_orders_source_check;
|
|
ALTER TABLE package_orders ADD CONSTRAINT package_orders_source_check
|
|
CHECK (source IN ('admin', 'mock', 'stripe', 'promo'));
|