class Dsn < ApplicationRecord validates :key, presence: true, uniqueness: true validates :name, presence: true before_validation :generate_key, on: :create scope :enabled, -> { where(enabled: true) } def self.authenticate(key) enabled.find_by(key: key) end def full_dsn_url # Generate a complete DSN URL like Sentry does # Format: https://{key}@{domain}/api/events domain = ENV['BAFFLE_HOST'] || Rails.application.config.action_mailer.default_url_options[:host] || ENV['RAILS_HOST'] || 'localhost:3000' protocol = Rails.env.development? ? 'http' : 'https' "#{protocol}://#{key}@#{domain}/api/events" end def api_endpoint_url # Just the API endpoint URL (without key) domain = ENV['BAFFLE_HOST'] || Rails.application.config.action_mailer.default_url_options[:host] || ENV['RAILS_HOST'] || 'localhost:3000' protocol = Rails.env.development? ? 'http' : 'https' "#{protocol}://#{domain}/api/events" end private def generate_key self.key ||= SecureRandom.hex(32) end end