85 lines
2.4 KiB
JavaScript
85 lines
2.4 KiB
JavaScript
const bcrypt = require('bcryptjs');
|
|
const { User, Device } = require('./models');
|
|
|
|
async function seedDatabase() {
|
|
try {
|
|
console.log('🌱 Seeding database...');
|
|
|
|
// Check if admin user exists
|
|
const existingAdmin = await User.findOne({ where: { username: 'admin' } });
|
|
|
|
if (!existingAdmin) {
|
|
// Create default admin user
|
|
const adminPassword = await bcrypt.hash('admin123', 10);
|
|
|
|
await User.create({
|
|
username: 'admin',
|
|
email: 'admin@dronedetection.local',
|
|
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'
|
|
});
|
|
|
|
console.log('✅ Admin user created (username: admin, password: admin123)');
|
|
} else {
|
|
console.log('✅ Admin user already exists');
|
|
}
|
|
|
|
// Create sample devices if none exist
|
|
const deviceCount = await Device.count();
|
|
if (deviceCount === 0) {
|
|
await Device.bulkCreate([
|
|
{
|
|
id: 1,
|
|
name: 'Arlanda Airport Detector',
|
|
geo_lat: 59.6519,
|
|
geo_lon: 17.9186,
|
|
location_description: 'Arlanda Airport Security Zone',
|
|
is_active: true,
|
|
last_heartbeat: new Date(),
|
|
heartbeat_interval: 300,
|
|
firmware_version: '1.0.0'
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'Musk Naval Base Detector',
|
|
geo_lat: 59.2753,
|
|
geo_lon: 18.2649,
|
|
location_description: 'Musk Naval Base Perimeter',
|
|
is_active: true,
|
|
last_heartbeat: new Date(),
|
|
heartbeat_interval: 300,
|
|
firmware_version: '1.0.0'
|
|
},
|
|
{
|
|
id: 3,
|
|
name: 'Royal Castle Detector',
|
|
geo_lat: 59.3268,
|
|
geo_lon: 18.0717,
|
|
location_description: 'Royal Castle Security Zone',
|
|
is_active: true,
|
|
last_heartbeat: new Date(),
|
|
heartbeat_interval: 300,
|
|
firmware_version: '1.0.0'
|
|
}
|
|
]);
|
|
|
|
console.log('✅ Three detector devices created (Arlanda, Naval Base, Royal Castle)');
|
|
} else {
|
|
console.log('✅ Devices already exist');
|
|
}
|
|
|
|
console.log('🌱 Database seeding completed');
|
|
} catch (error) {
|
|
console.error('❌ Database seeding failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
module.exports = seedDatabase;
|