diff --git a/manager/scratch/check_integrity.js b/manager/scratch/check_integrity.js new file mode 100644 index 0000000..86aa082 --- /dev/null +++ b/manager/scratch/check_integrity.js @@ -0,0 +1,29 @@ + +import pkg from 'pg'; +const { Pool } = pkg; + +const DATABASE_URL = 'postgresql://edumanager:EduManager2026!Seguro@localhost:5432/edumanager'; +const pool = new Pool({ + connectionString: DATABASE_URL +}); + +async function test() { + try { + console.log('--- TESTANDO INTEGRIDADE ---'); + const { rows: countAlunos } = await pool.query('SELECT COUNT(*) FROM alunos'); + console.log('Total em alunos:', countAlunos[0].count); + + const { rows: countProvas } = await pool.query('SELECT COUNT(*) FROM provas'); + console.log('Total em provas:', countProvas[0].count); + + const { rows: countSub } = await pool.query('SELECT COUNT(*) FROM provas_submissoes'); + console.log('Total em provas_submissoes:', countSub[0].count); + + } catch (err) { + console.error('ERRO NO TESTE:', err.message); + } finally { + await pool.end(); + } +} + +test(); diff --git a/manager/services/database.js b/manager/services/database.js index 695a917..8bc89a8 100644 --- a/manager/services/database.js +++ b/manager/services/database.js @@ -208,6 +208,16 @@ export async function insertSubmissao(submission) { // HELPERS: notas_boletim // ============================================================ export async function initNotasTable() { + // Remover constraints restritivas da tabela de submissões se existirem (transição JSON -> Postgres) + try { + await pool.query(` + ALTER TABLE provas_submissoes DROP CONSTRAINT IF EXISTS provas_submissoes_aluno_id_fkey; + ALTER TABLE provas_submissoes DROP CONSTRAINT IF EXISTS provas_submissoes_prova_id_fkey; + `); + } catch (err) { + console.log('[PostgreSQL] ℹ️ Submissoes fkey já removidas ou tabela não existe.'); + } + await pool.query(` CREATE TABLE IF NOT EXISTS notas_boletim ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), diff --git a/portal/server.selfhosted.js b/portal/server.selfhosted.js index 115d9ca..2b73213 100644 --- a/portal/server.selfhosted.js +++ b/portal/server.selfhosted.js @@ -598,8 +598,12 @@ app.post('/api/portal/avaliacoes/submeter', authMiddleware, async (req, res) => res.json({ success: true, result: { total_questions: totalQuestions, correct_count: correctCount, wrong_count: wrongCount, percentage, final_score: finalScore } }); } catch (err) { - console.error('Submissao error:', err); - res.status(500).json({ error: 'Erro interno.' }); + console.error('❌ [Portal:Submissão] Erro crítico ao salvar:', err.message, err.stack); + res.status(500).json({ + error: 'Erro interno ao processar nota', + details: err.message, + code: 'DB_SAVE_ERROR' + }); } }); diff --git a/schema.sql b/schema.sql index 075e36b..14249b7 100644 --- a/schema.sql +++ b/schema.sql @@ -378,8 +378,8 @@ CREATE TABLE IF NOT EXISTS questoes_provas ( -- ============================================================ CREATE TABLE IF NOT EXISTS provas_submissoes ( id TEXT PRIMARY KEY DEFAULT uuid_generate_v4()::TEXT, - aluno_id TEXT NOT NULL REFERENCES alunos(id) ON DELETE CASCADE, - prova_id TEXT NOT NULL REFERENCES provas(id) ON DELETE CASCADE, + aluno_id TEXT NOT NULL, + prova_id TEXT NOT NULL, total_questoes INTEGER DEFAULT 0, acertos INTEGER DEFAULT 0, erros INTEGER DEFAULT 0,