Promo codes; purchases off in production
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
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
-- 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'));
|
||||
@@ -104,6 +104,15 @@ pub fn random_token(bytes: usize) -> String {
|
||||
URL_SAFE_NO_PAD.encode(buf)
|
||||
}
|
||||
|
||||
/// Letters and digits that cannot be mixed up when typed (no 0/O, 1/I/L).
|
||||
pub const CODE_ALPHABET: &[u8] = b"23456789ABCDEFGHJKMNPQRSTUVWXYZ";
|
||||
|
||||
/// A random code of `len` characters from [`CODE_ALPHABET`] (about 5 bits each).
|
||||
pub fn random_code(len: usize) -> String {
|
||||
let mut rng = rand::rng();
|
||||
(0..len).map(|_| CODE_ALPHABET[rand::Rng::random_range(&mut rng, 0..CODE_ALPHABET.len())] as char).collect()
|
||||
}
|
||||
|
||||
/// Stored form of a secret token.
|
||||
pub fn token_hash(token: &str) -> Vec<u8> {
|
||||
Sha256::digest(token.as_bytes()).to_vec()
|
||||
|
||||
Reference in New Issue
Block a user