Procedimiento de activacion

This commit is contained in:
2026-03-06 12:11:17 +01:00
parent 37e41a0130
commit 221abe0d33
16 changed files with 375 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
CREATE EXTENSION pgcrypto; -- para los random bytes
-- 1. Función de generacion de uuidv7 copiada porque no esta en postgre 19
-- 1. Función de generacion de uuidv7 copiada de github porque no está en postgre 16
CREATE OR REPLACE FUNCTION
uuid_generate_v7()
RETURNS
@@ -44,6 +44,8 @@ CREATE TABLE activation_codes (
card_id UUID REFERENCES payment_cards(card_id), -- Una tarjeta, maximo un un código activo borrar o solo con expires_at?
code_hash TEXT NOT NULL, -- Guardar el código hasheado, el original se imprime y se manda
is_used BOOLEAN DEFAULT FALSE,
is_blocked BOOLEAN DEFAULT FALSE,
failed_attempts INT DEFAULT 0,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -59,3 +61,27 @@ CREATE TABLE activation_logs (
geo_location VARCHAR(100), -- Opcional: Ciudad/País derivado de IP
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE OR REPLACE FUNCTION log_activation_attempt()
RETURNS TRIGGER AS $$
BEGIN
-- Si el intento falló (esto lo controlas desde tu lógica de UPDATE)
-- Hay que barajar si es viable hacer los updates de failed_attempts desde aquí
-- y nó desde código.
IF NEW.failed_attempts > OLD.failed_attempts THEN
INSERT INTO activation_logs (card_id, action_type, created_at)
VALUES (NEW.card_id, 'FAILED_ATTEMPT', NOW());
END IF;
-- Bloqueo automático si llega al límite
IF NEW.failed_attempts >= 3 THEN
NEW.is_blocked := TRUE;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_check_attempts
BEFORE UPDATE ON activation_codes
FOR EACH ROW EXECUTE FUNCTION log_activation_attempt();