completed SMS gateway project
This commit is contained in:
143
db/seeds.rb
Normal file
143
db/seeds.rb
Normal file
@@ -0,0 +1,143 @@
|
||||
# This file should ensure the existence of records required to run the application in every environment (production,
|
||||
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
|
||||
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
|
||||
|
||||
puts "🌱 Seeding database..."
|
||||
|
||||
# Create default admin user
|
||||
puts "\n👤 Creating admin user..."
|
||||
|
||||
unless AdminUser.exists?(email: "admin@example.com")
|
||||
admin = AdminUser.create!(
|
||||
email: "admin@example.com",
|
||||
password: "password123",
|
||||
name: "Admin User"
|
||||
)
|
||||
puts "✅ Created Admin User"
|
||||
puts " Email: admin@example.com"
|
||||
puts " Password: password123"
|
||||
puts " ⚠️ IMPORTANT: Change this password in production!"
|
||||
end
|
||||
|
||||
# Create sample API keys for testing
|
||||
puts "\n📝 Creating API keys..."
|
||||
|
||||
api_key_results = []
|
||||
|
||||
unless ApiKey.exists?(name: "Development API Key")
|
||||
result = ApiKey.generate!(
|
||||
name: "Development API Key",
|
||||
permissions: {
|
||||
"send_sms" => true,
|
||||
"receive_sms" => true,
|
||||
"manage_otp" => true
|
||||
}
|
||||
)
|
||||
api_key_results << result
|
||||
puts "✅ Created API Key: #{result[:raw_key]}"
|
||||
end
|
||||
|
||||
unless ApiKey.exists?(name: "Admin API Key")
|
||||
result = ApiKey.generate!(
|
||||
name: "Admin API Key",
|
||||
permissions: {
|
||||
"send_sms" => true,
|
||||
"receive_sms" => true,
|
||||
"manage_otp" => true,
|
||||
"admin" => true
|
||||
}
|
||||
)
|
||||
api_key_results << result
|
||||
puts "✅ Created Admin API Key: #{result[:raw_key]}"
|
||||
end
|
||||
|
||||
# Create sample gateway devices
|
||||
puts "\n📱 Creating sample gateway devices..."
|
||||
|
||||
unless Gateway.exists?(device_id: "test-gateway-001")
|
||||
gateway1 = Gateway.new(
|
||||
device_id: "test-gateway-001",
|
||||
name: "Test Gateway 1",
|
||||
status: "offline",
|
||||
priority: 1,
|
||||
active: true
|
||||
)
|
||||
raw_key = gateway1.generate_api_key!
|
||||
puts "✅ Created Gateway: #{gateway1.name}"
|
||||
puts " Device ID: #{gateway1.device_id}"
|
||||
puts " API Key: #{raw_key}"
|
||||
end
|
||||
|
||||
unless Gateway.exists?(device_id: "test-gateway-002")
|
||||
gateway2 = Gateway.new(
|
||||
device_id: "test-gateway-002",
|
||||
name: "Test Gateway 2",
|
||||
status: "offline",
|
||||
priority: 2,
|
||||
active: true
|
||||
)
|
||||
raw_key = gateway2.generate_api_key!
|
||||
puts "✅ Created Gateway: #{gateway2.name}"
|
||||
puts " Device ID: #{gateway2.device_id}"
|
||||
puts " API Key: #{raw_key}"
|
||||
end
|
||||
|
||||
# Create sample webhook configurations
|
||||
puts "\n🔗 Creating webhook configurations..."
|
||||
|
||||
WebhookConfig.find_or_create_by!(name: "SMS Received Webhook") do |w|
|
||||
w.url = "https://example.com/webhooks/sms-received"
|
||||
w.event_type = "sms_received"
|
||||
w.secret_key = SecureRandom.hex(32)
|
||||
w.active = false # Disabled by default
|
||||
w.timeout = 30
|
||||
w.retry_count = 3
|
||||
end
|
||||
puts "✅ Created webhook: SMS Received Webhook"
|
||||
|
||||
WebhookConfig.find_or_create_by!(name: "SMS Sent Webhook") do |w|
|
||||
w.url = "https://example.com/webhooks/sms-sent"
|
||||
w.event_type = "sms_sent"
|
||||
w.secret_key = SecureRandom.hex(32)
|
||||
w.active = false # Disabled by default
|
||||
w.timeout = 30
|
||||
w.retry_count = 3
|
||||
end
|
||||
puts "✅ Created webhook: SMS Sent Webhook"
|
||||
|
||||
WebhookConfig.find_or_create_by!(name: "SMS Failed Webhook") do |w|
|
||||
w.url = "https://example.com/webhooks/sms-failed"
|
||||
w.event_type = "sms_failed"
|
||||
w.secret_key = SecureRandom.hex(32)
|
||||
w.active = false # Disabled by default
|
||||
w.timeout = 30
|
||||
w.retry_count = 3
|
||||
end
|
||||
puts "✅ Created webhook: SMS Failed Webhook"
|
||||
|
||||
puts "\n✨ Seeding completed!"
|
||||
puts "\n" + "=" * 80
|
||||
puts "🔑 API KEYS (Save these - they won't be shown again!)"
|
||||
puts "=" * 80
|
||||
|
||||
api_key_results.each do |result|
|
||||
puts "\nName: #{result[:api_key].name}"
|
||||
puts "Key: #{result[:raw_key]}"
|
||||
puts "Permissions: #{result[:api_key].permissions}"
|
||||
end
|
||||
|
||||
puts "\n" + "=" * 80
|
||||
puts "📊 Database Summary"
|
||||
puts "=" * 80
|
||||
puts "Admins: #{AdminUser.count}"
|
||||
puts "Gateways: #{Gateway.count}"
|
||||
puts "API Keys: #{ApiKey.count}"
|
||||
puts "Webhooks: #{WebhookConfig.count}"
|
||||
puts "SMS Messages: #{SmsMessage.count}"
|
||||
puts "OTP Codes: #{OtpCode.count}"
|
||||
puts "=" * 80
|
||||
puts "\n🔐 Admin Login"
|
||||
puts "URL: http://localhost:3000/admin/login"
|
||||
puts "Email: admin@example.com"
|
||||
puts "Password: password123"
|
||||
puts "=" * 80
|
||||
Reference in New Issue
Block a user