trading-platform-database-v2/ddl/schemas/investment/tables/02-risk_questionnaire.sql
rckrdmrd 45e77e9a9c feat: Initial commit - Database schemas and scripts
DDL schemas for Trading Platform:
- User management
- Authentication
- Payments
- Education
- ML predictions
- Trading data

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-18 04:30:23 -06:00

64 lines
2.6 KiB
SQL

-- =====================================================
-- INVESTMENT SCHEMA - RISK QUESTIONNAIRE TABLE
-- =====================================================
-- Description: Risk assessment questionnaire (15 questions)
-- Schema: investment
-- Author: Database Agent
-- Date: 2025-12-06
-- =====================================================
CREATE TABLE investment.risk_questionnaire (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
-- Usuario
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
-- Respuestas (15 preguntas)
responses JSONB NOT NULL, -- [{question_id, answer, score}]
-- Resultado
total_score INTEGER NOT NULL CHECK (total_score >= 0 AND total_score <= 100),
calculated_profile investment.risk_profile NOT NULL,
-- Recomendación de agente
recommended_agent investment.trading_agent,
-- Validez
completed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL, -- Válido por 1 año
-- Note: is_expired removed - compute dynamically as (expires_at < NOW())
-- Metadata
ip_address INET,
user_agent TEXT,
completion_time_seconds INTEGER, -- Tiempo que tardó en completar
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Índices
CREATE INDEX idx_questionnaire_user ON investment.risk_questionnaire(user_id);
CREATE INDEX idx_questionnaire_profile ON investment.risk_questionnaire(calculated_profile);
-- Index for valid questionnaires (without time-based predicate for immutability)
CREATE INDEX idx_questionnaire_valid ON investment.risk_questionnaire(user_id, expires_at DESC);
-- Comentarios
COMMENT ON TABLE investment.risk_questionnaire IS 'Risk assessment questionnaire responses (valid for 1 year)';
COMMENT ON COLUMN investment.risk_questionnaire.responses IS 'Array of question responses with scores: [{question_id, answer, score}]';
COMMENT ON COLUMN investment.risk_questionnaire.total_score IS 'Sum of all question scores (0-100)';
COMMENT ON COLUMN investment.risk_questionnaire.calculated_profile IS 'Risk profile calculated from total_score';
COMMENT ON COLUMN investment.risk_questionnaire.recommended_agent IS 'Trading agent recommendation based on risk profile';
COMMENT ON COLUMN investment.risk_questionnaire.expires_at IS 'Questionnaire expires after 1 year, user must retake';
-- Ejemplo de estructura de responses JSONB:
COMMENT ON COLUMN investment.risk_questionnaire.responses IS
'Example: [
{"question_id": "Q1", "answer": "A", "score": 5},
{"question_id": "Q2", "answer": "B", "score": 10},
...
]
Scoring logic:
- Conservative (0-40): Atlas agent recommended
- Moderate (41-70): Orion agent recommended
- Aggressive (71-100): Nova agent recommended';