diff --git a/backend/index.js b/backend/index.js index 4de1c3d..dbeac0e 100644 --- a/backend/index.js +++ b/backend/index.js @@ -609,50 +609,64 @@ if (process.env.NODE_ENV === 'production') { } // Auto-provision Super Admin -const provisionSuperAdmin = async () => { +const provisionSuperAdmin = async (retries = 5, delay = 5000) => { const email = 'suporte@blyzer.com.br'; - try { - // Ensure system tenant exists - await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']); - - const [existing] = await pool.query('SELECT id FROM users WHERE email = ?', [email]); - if (existing.length === 0) { - console.log('Provisioning default super_admin...'); - const uid = `u_${crypto.randomUUID().split('-')[0]}`; - const placeholderHash = 'pending_setup'; + + for (let i = 0; i < retries; i++) { + try { + // Test connection first + const connection = await pool.getConnection(); + connection.release(); + + // Ensure system tenant exists + await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']); - await pool.query( - 'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)', - [uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active'] - ); + const [existing] = await pool.query('SELECT id, password_hash FROM users WHERE email = ?', [email]); + if (existing.length === 0 || existing[0].password_hash === 'pending_setup') { + console.log('Provisioning default super_admin or resending email...'); + + if (existing.length === 0) { + const uid = `u_${crypto.randomUUID().split('-')[0]}`; + const placeholderHash = 'pending_setup'; + + await pool.query( + 'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)', + [uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active'] + ); + } - const token = crypto.randomBytes(32).toString('hex'); - await pool.query( - 'INSERT INTO password_resets (email, token, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 15 MINUTE))', - [email, token] - ); + const token = crypto.randomBytes(32).toString('hex'); + await pool.query( + 'INSERT INTO password_resets (email, token, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 15 MINUTE))', + [email, token] + ); - const setupLink = `${process.env.APP_URL || 'http://localhost:3001'}/#/reset-password?token=${token}`; - console.log(`\n\n=== SUPER ADMIN SETUP LINK ===\n${setupLink}\n==============================\n\n`); + const setupLink = `${process.env.APP_URL || 'http://localhost:3001'}/#/reset-password?token=${token}`; + console.log(`\n\n=== SUPER ADMIN SETUP LINK ===\n${setupLink}\n==============================\n\n`); - await transporter.sendMail({ - from: `"Fasto" <${process.env.MAIL_FROM || 'nao-responda@blyzer.com.br'}>`, - to: email, - subject: 'Conta Super Admin Criada - Fasto', - html: ` -
Sua conta de suporte (super_admin) foi criada no Fasto.
-Sua conta de suporte (super_admin) foi criada no Fasto.
+ +Este link expira em 15 minutos.
Este link expira em 15 minutos.
-