import pg from 'pg'; const pool = new pg.Pool({ user: 'edumanager', host: '150.230.87.131', database: 'edumanager', password: 'EduManager2026!Seguro', port: 5432, }); async function main() { console.log('šŸ”„ Iniciando correção de justificativas e limpeza de duplicidades no VPS...'); const evillaId = '311709fb-68ab-4168-8684-887b5ec2d731'; const evillaLessonId = 'ca727723-e854-4503-b032-b811e06cfcdf'; const evillaJustId = 'att-just-1777745007528'; const lohannyId = '05de4757-d36f-4de6-9e57-5792d13ad3e7'; const lohannyLessonId = '080469ea-58fc-4231-a016-fd35b99c0dce'; const lohannyJustId = 'att-just-1777143386721'; // ========================================== // 1. CORREƇƃO NO BANCO DE DADOS (SQL) // ========================================== const client = await pool.connect(); try { await client.query('BEGIN'); // Evilla: Deletar duplicidades no SQL const delEvilla = await client.query( 'DELETE FROM frequencias WHERE aluno_id = $1 AND DATE(data) = \'2026-05-02\' AND id <> $2', [evillaId, evillaJustId] ); console.log(`[SQL] Deletadas ${delEvilla.rowCount} duplicidades para Evilla em 02/05/2026.`); // Evilla: Corrigir fuso da justificativa e setar aula_id no SQL const updEvilla = await client.query( 'UPDATE frequencias SET data = \'2026-05-02 14:00:00\', aula_id = $1 WHERE id = $2', [evillaLessonId, evillaJustId] ); console.log(`[SQL] Justificativa da Evilla corrigida para 14:00 (aula: ${evillaLessonId}).`); // Maria Lohanny: Deletar duplicidades no SQL const delLohanny = await client.query( 'DELETE FROM frequencias WHERE aluno_id = $1 AND DATE(data) = \'2026-04-25\' AND id <> $2', [lohannyId, lohannyJustId] ); console.log(`[SQL] Deletadas ${delLohanny.rowCount} duplicidades para Maria Lohanny em 25/04/2026.`); // Maria Lohanny: Corrigir fuso da justificativa e setar aula_id no SQL const updLohanny = await client.query( 'UPDATE frequencias SET data = \'2026-04-25 14:00:00\', aula_id = $1 WHERE id = $2', [lohannyLessonId, lohannyJustId] ); console.log(`[SQL] Justificativa da Maria Lohanny corrigida para 14:00 (aula: ${lohannyLessonId}).`); await client.query('COMMIT'); console.log('[SQL] Transação finalizada com sucesso!'); } catch (err) { await client.query('ROLLBACK'); console.error('[SQL] āŒ Erro ao atualizar tabelas:', err); } // ========================================== // 2. CORREƇƃO NO CACHE LEGADO (JSON NA TABELA school_data) // ========================================== try { const { rows } = await client.query('SELECT data FROM school_data WHERE id = 1'); const schoolData = rows[0]?.data || {}; if (schoolData && Array.isArray(schoolData.attendance)) { console.log(`[JSON] Analisando array de presenƧa (${schoolData.attendance.length} registros)...`); // Remover duplicidades const initialLength = schoolData.attendance.length; schoolData.attendance = schoolData.attendance.filter(a => { if (a.id === evillaJustId || a.id === lohannyJustId) return true; if (a.studentId === evillaId && a.date && a.date.startsWith('2026-05-02')) { return false; } if (a.studentId === lohannyId && a.date && a.date.startsWith('2026-04-25')) { return false; } return true; }); console.log(`[JSON] Removidos ${initialLength - schoolData.attendance.length} registros duplicados.`); // Corrigir fuso horĆ”rio das justificativas no JSON let fixedCount = 0; for (const a of schoolData.attendance) { if (a.id === evillaJustId) { a.date = '2026-05-02T14:00:00'; a.lessonId = evillaLessonId; fixedCount++; } if (a.id === lohannyJustId) { a.date = '2026-04-25T14:00:00'; a.lessonId = lohannyLessonId; fixedCount++; } } console.log(`[JSON] Corrigidas ${fixedCount} justificativas no JSON.`); // Salvar de volta na tabela school_data await client.query('UPDATE school_data SET data = $1, updated_at = NOW() WHERE id = 1', [JSON.stringify(schoolData)]); console.log('[JSON] Tabela school_data atualizada com sucesso!'); } else { console.warn('[JSON] Dados de frequĆŖncia nĆ£o localizados no school_data.'); } } catch (err) { console.error('[JSON] āŒ Erro ao atualizar cache JSON:', err); } finally { client.release(); } console.log('āœ… Correção de justificativas concluĆ­da com sucesso!'); pool.end(); } main().catch(err => { console.error('āŒ Erro inesperado:', err); pool.end(); });