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: ` -
-

Conta Super Admin Gerada

-

Sua conta de suporte (super_admin) foi criada no Fasto.

-
- Finalizar Cadastro + await transporter.sendMail({ + from: `"Fasto" <${process.env.MAIL_FROM || 'nao-responda@blyzer.com.br'}>`, + to: email, + subject: 'Conta Super Admin Criada - Fasto', + html: ` +
+

Conta Super Admin Gerada

+

Sua conta de suporte (super_admin) foi criada no Fasto.

+
+ Finalizar Cadastro +
+

Este link expira em 15 minutos.

-

Este link expira em 15 minutos.

-
- ` - }).catch(err => console.error("Failed to send super_admin email:", err)); + ` + }).catch(err => console.error("Failed to send super_admin email:", err)); + } + return; // Success, exit the retry loop + } catch (error) { + console.error(`Failed to provision super_admin (Attempt ${i + 1}/${retries}):`, error.message); + if (i < retries - 1) { + await new Promise(res => setTimeout(res, delay)); + } } - } catch (error) { - console.error('Failed to provision super_admin:', error); } }; diff --git a/docker-compose.yml b/docker-compose.yml index 6b1837a..47e820a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,13 @@ services: - DB_USER=${DB_USER:-root} - DB_PASSWORD=${DB_PASSWORD:-root_password} - DB_NAME=${DB_NAME:-agenciac_comia} + - SMTP_HOST=${SMTP_HOST} + - SMTP_PORT=${SMTP_PORT} + - SMTP_USER=${SMTP_USER} + - SMTP_PASS=${SMTP_PASS} + - MAIL_FROM=${MAIL_FROM} + - APP_URL=${APP_URL} + - JWT_SECRET=${JWT_SECRET} ports: - "3001:3001" deploy: