const bcrypt = require('bcryptjs'); const { User, Device, AlertRule, ManagementUser, Tenant } = require('./models'); const { createInitialManagementUser } = require('./scripts/seed-management-users'); async function seedDatabase() { try { console.log('🌱 Seeding database...'); // First, create management users (platform admins) await createInitialManagementUser(); // Create default tenant "uamils-ab" let defaultTenant = await Tenant.findOne({ where: { slug: 'uamils-ab' } }); if (!defaultTenant) { defaultTenant = await Tenant.create({ name: 'UAMILS AB', slug: 'uamils-ab', domain: 'uamils-ab.dev.uggla.uamils.com', subscription_type: 'enterprise', is_active: true, auth_provider: 'local' }); console.log('✅ Default tenant "uamils-ab" created'); } else { console.log('✅ Default tenant "uamils-ab" already exists'); } // Check if admin user exists (legacy tenant admin) const existingAdmin = await User.findOne({ where: { username: 'admin', tenant_id: defaultTenant.id } }); if (!existingAdmin) { // Create default admin user for uamils-ab tenant const adminPassword = await bcrypt.hash('admin123', 10); await User.create({ username: 'admin', email: 'admin@uamils.com', password_hash: adminPassword, first_name: 'System', last_name: 'Administrator', role: 'admin', is_active: true, sms_alerts_enabled: false, email_alerts_enabled: false, timezone: 'Europe/Stockholm', tenant_id: defaultTenant.id }); console.log('✅ Admin user created for uamils-ab tenant (username: admin, password: admin123)'); } else { console.log('✅ Admin user already exists for uamils-ab tenant'); } // Check if tumanovsky user exists const existingTumanovsky = await User.findOne({ where: { username: 'tumanovsky' } }); if (!existingTumanovsky) { // Create tumanovsky admin user const tumanovskyPassword = await bcrypt.hash('tumanovsky', 10); await User.create({ username: 'tumanovsky', email: 'tumanovsky@dronedetection.local', password_hash: tumanovskyPassword, first_name: 'Tumanovsky', last_name: 'Admin', role: 'admin', is_active: true, sms_alerts_enabled: true, email_alerts_enabled: true, timezone: 'Europe/Stockholm' }); console.log('✅ Tumanovsky admin user created (username: tumanovsky, password: tumanovsky)'); } else { console.log('✅ Tumanovsky user already exists'); } // Create sample devices if none exist const deviceCount = await Device.count(); if (deviceCount === 0) { // REMOVED: Automatic device creation // No longer creating sample devices automatically console.log('📝 No sample devices created - add devices manually through the UI'); } else { console.log('✅ Devices already exist'); } // Get admin user for alert rules const adminUser = await User.findOne({ where: { username: 'admin' } }); // Create location-specific alert rules const alertRuleCount = await AlertRule.count(); if (alertRuleCount === 0) { // REMOVED: Automatic alert rule creation // No longer creating sample alert rules automatically console.log('📝 No sample alert rules created - add alert rules manually through the UI'); } else { console.log('✅ Alert rules already exist'); } console.log('🌱 Database seeding completed'); } catch (error) { console.error('❌ Database seeding failed:', error); throw error; } } module.exports = seedDatabase;