fix: remoção de constraints restritivas e melhoria de logs de submissão

This commit is contained in:
Sidney 2026-05-01 14:24:06 -03:00
parent 0e9a809cd7
commit dffc7b8903
4 changed files with 47 additions and 4 deletions

View File

@ -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();

View File

@ -208,6 +208,16 @@ export async function insertSubmissao(submission) {
// HELPERS: notas_boletim // HELPERS: notas_boletim
// ============================================================ // ============================================================
export async function initNotasTable() { 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(` await pool.query(`
CREATE TABLE IF NOT EXISTS notas_boletim ( CREATE TABLE IF NOT EXISTS notas_boletim (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

View File

@ -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 } }); res.json({ success: true, result: { total_questions: totalQuestions, correct_count: correctCount, wrong_count: wrongCount, percentage, final_score: finalScore } });
} catch (err) { } catch (err) {
console.error('Submissao error:', err); console.error('❌ [Portal:Submissão] Erro crítico ao salvar:', err.message, err.stack);
res.status(500).json({ error: 'Erro interno.' }); res.status(500).json({
error: 'Erro interno ao processar nota',
details: err.message,
code: 'DB_SAVE_ERROR'
});
} }
}); });

View File

@ -378,8 +378,8 @@ CREATE TABLE IF NOT EXISTS questoes_provas (
-- ============================================================ -- ============================================================
CREATE TABLE IF NOT EXISTS provas_submissoes ( CREATE TABLE IF NOT EXISTS provas_submissoes (
id TEXT PRIMARY KEY DEFAULT uuid_generate_v4()::TEXT, id TEXT PRIMARY KEY DEFAULT uuid_generate_v4()::TEXT,
aluno_id TEXT NOT NULL REFERENCES alunos(id) ON DELETE CASCADE, aluno_id TEXT NOT NULL,
prova_id TEXT NOT NULL REFERENCES provas(id) ON DELETE CASCADE, prova_id TEXT NOT NULL,
total_questoes INTEGER DEFAULT 0, total_questoes INTEGER DEFAULT 0,
acertos INTEGER DEFAULT 0, acertos INTEGER DEFAULT 0,
erros INTEGER DEFAULT 0, erros INTEGER DEFAULT 0,