// Script para criar tabelas MDM const { query } = require('./config/database'); async function setupMDM() { try { console.log('🔧 Configurando Sistema MDM...\n'); // 1. Criar tabela policies console.log('[1/5] Criando tabela policies...'); await query(` CREATE TABLE IF NOT EXISTS policies ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, type VARCHAR(50) NOT NULL, config JSONB NOT NULL DEFAULT '{}', enabled BOOLEAN DEFAULT true, schedule VARCHAR(100), priority INTEGER DEFAULT 5, created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ) `); console.log(' ✅ policies criada'); // 2. Criar tabela device_policies console.log('[2/5] Criando tabela device_policies...'); await query(` CREATE TABLE IF NOT EXISTS device_policies ( id SERIAL PRIMARY KEY, device_id VARCHAR(255) NOT NULL, policy_id INTEGER NOT NULL, assigned_at TIMESTAMP DEFAULT NOW(), UNIQUE(device_id, policy_id) ) `); console.log(' ✅ device_policies criada'); // 3. Criar tabela policy_commands console.log('[3/5] Criando tabela policy_commands...'); await query(` CREATE TABLE IF NOT EXISTS policy_commands ( id SERIAL PRIMARY KEY, device_id VARCHAR(255) NOT NULL, policy_id INTEGER NOT NULL, command_type VARCHAR(50) NOT NULL, command_data JSONB NOT NULL DEFAULT '{}', status VARCHAR(20) DEFAULT 'pending', priority INTEGER DEFAULT 5, created_at TIMESTAMP DEFAULT NOW(), sent_at TIMESTAMP, completed_at TIMESTAMP, result JSONB, error_message TEXT, retry_count INTEGER DEFAULT 0, max_retries INTEGER DEFAULT 3 ) `); console.log(' ✅ policy_commands criada'); // 4. Criar tabela policy_executions console.log('[4/5] Criando tabela policy_executions...'); await query(` CREATE TABLE IF NOT EXISTS policy_executions ( id SERIAL PRIMARY KEY, device_id VARCHAR(255) NOT NULL, policy_id INTEGER NOT NULL, command_id INTEGER, status VARCHAR(20) NOT NULL, executed_at TIMESTAMP DEFAULT NOW(), duration_seconds INTEGER, result JSONB, error_message TEXT, details TEXT ) `); console.log(' ✅ policy_executions criada'); // 5. Criar tabela policy_templates console.log('[5/5] Criando tabela policy_templates...'); await query(` CREATE TABLE IF NOT EXISTS policy_templates ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, category VARCHAR(50), type VARCHAR(50) NOT NULL, default_config JSONB NOT NULL DEFAULT '{}', icon VARCHAR(100), created_at TIMESTAMP DEFAULT NOW() ) `); console.log(' ✅ policy_templates criada'); // Inserir templates console.log('\n📦 Inserindo templates pré-configurados...'); const templates = [ { name: 'Forçar Windows Update', category: 'updates', type: 'windows_update', config: { action: 'force_check_and_install', reboot_if_required: true, reboot_delay_minutes: 30 }}, { name: 'Instalar Software', category: 'software', type: 'software_install', config: { software_name: '', download_url: '', install_args: '/S' }}, { name: 'Executar Script PowerShell', category: 'configuration', type: 'powershell_script', config: { script: '', run_as_admin: true, timeout_minutes: 30 }}, { name: 'Reiniciar Dispositivo', category: 'maintenance', type: 'reboot', config: { delay_minutes: 5, force: false }}, { name: 'Limpar Arquivos Temporários', category: 'maintenance', type: 'cleanup', config: { clear_temp: true, clear_browser_cache: true }} ]; for (const t of templates) { await query(` INSERT INTO policy_templates (name, category, type, default_config) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING `, [t.name, t.category, t.type, JSON.stringify(t.config)]); } const count = await query('SELECT COUNT(*) FROM policy_templates'); console.log(' ✅', count.rows[0].count, 'templates disponíveis'); console.log('\n🎉 Sistema MDM configurado com sucesso!'); console.log(''); console.log('Teste agora:'); console.log(' curl http://localhost:3005/api/mdm/templates'); console.log(' curl http://localhost:3005/api/policies'); process.exit(0); } catch (err) { console.error('\n❌ Erro:', err.message); console.error(err.stack); process.exit(1); } } setupMDM();