# AUDIT-DDL-BACKEND.md
## Auditoría de Coherencia DDL ↔ Backend

**Fecha:** 2026-01-28
**Proyecto:** trading-platform
**Auditor:** Claude Code (Opus 4.5)
**Fase:** F0.3.1

---

## RESUMEN EJECUTIVO

| Métrica | Valor | Target | Gap |
|---------|-------|--------|-----|
| **Coherencia DDL-Backend** | 31% | 95% | -64pp |
| **Tablas DDL** | 93 | - | - |
| **Tablas con backend** | 37 | 93 | 56 faltantes |
| **Tablas orphan** | 53 | 0 | CRÍTICO |

---

## TABLA RESUMEN POR SCHEMA

| Schema | Tablas | Con Backend | % Coherencia | Estado |
|--------|--------|-------------|--------------|--------|
| **audit** | 7 | 1/7 | 14% | CRÍTICO |
| **auth** | 13 | 6/13 | 46% | CRÍTICO |
| **education** | 16 | 10/16 | 63% | MEDIO |
| **financial** | 10 | 4/10 | 40% | CRÍTICO |
| **investment** | 9 | 5/9 | 56% | CRÍTICO |
| **llm** | 5 | 1/5 | 20% | ORPHAN |
| **market_data** | 4 | 1/4 | 25% | CRÍTICO |
| **ml** | 11 | 6/11 | 55% | CRÍTICO |
| **portfolio** | 5 | 3/5 | 60% | CRÍTICO |
| **trading** | 10 | 5/10 | 50% | CRÍTICO |
| **TOTAL** | **93** | **37** | **31%** | **CRÍTICO** |

---

## CLASIFICACIÓN DE GAPS

### A. ORPHAN TABLES (53 tablas sin tipos/servicios)

**Schema: auth (8 tablas)**
- email_verifications
- phone_verifications
- password_reset_tokens
- auth_logs
- login_attempts
- rate_limiting_config
- notifications
- user_push_tokens

**Schema: education (6 tablas)**
- categories (parcial)
- videos
- progress
- review_helpful_votes

**Schema: trading (5 tablas)**
- symbols
- bots
- signals
- trading_metrics
- paper_balances

**Schema: investment (4 tablas)**
- risk_questionnaire
- withdrawal_requests
- daily_performance
- distribution_history

**Schema: financial (6 tablas)**
- invoices
- payment_methods
- wallet_audit_log
- currency_exchange_rates
- wallet_limits
- customers

**Schema: portfolio (2 tablas)**
- rebalance_history
- portfolio_snapshots

**Schema: market_data (3 tablas)**
- tickers
- ohlcv_5m
- ohlcv_15m

**Schema: ml (5 tablas)**
- model_versions
- prediction_outcomes
- feature_store
- llm_predictions
- llm_prediction_outcomes
- llm_decisions
- risk_events

**Schema: llm (4 tablas - desconectadas)**
- conversations (tipo existe, servicio no usa BD)
- messages (tipo existe, servicio no usa BD)
- user_preferences (tipo existe, servicio no usa BD)
- user_memory (tipo existe, servicio no usa BD)

**Schema: audit (6 tablas)**
- security_events
- system_events
- trading_audit
- api_request_logs
- data_access_logs
- compliance_logs

---

## FIELD MISMATCHES DETECTADOS

### auth.sessions
| DDL Campo | Backend Campo | Estado |
|-----------|---------------|--------|
| session_token | NO EXISTE | MISSING |
| is_active | NO EXISTE | MISSING |
| device_type | NO EXISTE | MISSING |
| device_name | NO EXISTE | MISSING |

### education.categories
| DDL Campo | Backend Campo | Estado |
|-----------|---------------|--------|
| display_order | sortOrder | RENAME |
| icon_url | icon | TYPE DIFF |
| color | NO EXISTE | MISSING |

### financial.wallets
| DDL Campo | Backend Campo | Estado |
|-----------|---------------|--------|
| balance DECIMAL(20,8) | balance: number | PRECISION LOSS |

---

## PRIORIZACIÓN DE FIXES

### P0 CRÍTICO (Bloquea APIs)
1. **LLM desconexión:** Conectar llm.service.ts a BD (5 tablas)
2. **Auth gaps:** email_verifications, phone_verifications, password_reset_tokens
3. **Trading symbols:** Crear tipo y servicio (requisito para watchlists)
4. **Market data OHLCV:** Completar tipos

### P1 ALTA (Funcionalidad principal)
1. **Education videos:** Servicio dedicado
2. **Investment risks:** Tipo risk_questionnaire
3. **Audit completo:** security_events, api_request_logs
4. **Portfolio snapshots:** Tipo portfolio_snapshots

### P2 MEDIA (Mejora coherencia)
1. Notification system (auth.notifications)
2. Currency rates
3. Trading bots
4. ML model versions

### P3 BAJA (Consistencia)
1. Wallet limits, customers, compliance logs

---

## RECOMENDACIONES

### Inmediatas (Semana 1)
1. Crear script de auditoría automático
2. Conectar LLM a BD (crítico)
3. Crear tipos faltantes auth (5 tablas)

### Corto plazo (Semana 2-3)
1. Implementar education.videos
2. Completar trading.symbols
3. Crear investment.risk_questionnaire

### Mediano plazo (Semana 4+)
1. Estandarizar naming DDL→Backend
2. Crear repository pattern para todas las tablas
3. Implementar tipos ACID para DECIMAL/UUID

---

*Generado: 2026-01-28 | Sistema SIMCO v4.0.0*
