const mysql = require('mysql2/promise'); const dbConfig = { host: 'localhost', user: 'root', password: '', database: 'sis_odonto' }; async function fix() { try { const c = await mysql.createConnection(dbConfig); console.log("Connected for fixing collations..."); const tables = [ 'usuarios', 'clinicas', 'vinculos', 'dentistas', 'agendamentos', 'pacientes', 'financeiro', 'planos', 'especialidades', 'procedimentos', 'procedimentos_valores_planos', 'guias_odontologicas', 'gto_builders', 'gto_items', 'notificacoes', 'settings' ]; for (const t of tables) { try { // Remove FK checks to avoid issues during alter await c.query('SET FOREIGN_KEY_CHECKS = 0'); await c.query(`ALTER TABLE ${t} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`); await c.query('SET FOREIGN_KEY_CHECKS = 1'); console.log(`[OK] ${t} normalized.`); } catch (err) { console.log(`[SKIP] ${t}: ${err.message}`); await c.query('SET FOREIGN_KEY_CHECKS = 1'); } } // Link unassigned dentists (professionals) to the main clinic await c.query('UPDATE dentistas SET clinica_id = "c1" WHERE clinica_id IS NULL OR clinica_id = ""'); console.log("All dentists linked to c1 (Matriz)."); // Now get the division const [division] = await c.query(` SELECT c.nome_fantasia as Clinica, u.nome as Nome, v.role as Papel, u.email as Email FROM vinculos v JOIN usuarios u ON v.usuario_id = u.id JOIN clinicas c ON v.clinica_id = c.id WHERE v.role IN ('dentista', 'donoclinica') ORDER BY c.nome_fantasia `); console.log("\n--- DIVISÃO DE DENTISTAS/DONOS POR CLÍNICA ---"); console.table(division); const [professionals] = await c.query(` SELECT d.nome, c.nome_fantasia as clinica FROM dentistas d JOIN clinicas c ON d.clinica_id = c.id `); console.log("\n--- REGISTROS PROFISSIONAIS (LISTA DE DENTISTAS) ---"); console.table(professionals); await c.end(); } catch (err) { console.error("Fix failed:", err.message); } } fix();