- Previously ticket was displayed using id which is too easy to find - Now the URL takes ``qr_code`` field as parameters
77 lines
1.9 KiB
Ruby
Executable File
77 lines
1.9 KiB
Ruby
Executable File
class Ticket < ApplicationRecord
|
|
# === Associations ===
|
|
belongs_to :order
|
|
belongs_to :ticket_type
|
|
has_one :event, through: :ticket_type
|
|
has_one :user, through: :order
|
|
|
|
# === Validations ===
|
|
validates :qr_code, presence: true, uniqueness: true
|
|
validates :order_id, presence: true
|
|
validates :ticket_type_id, presence: true
|
|
validates :price_cents, presence: true, numericality: { greater_than: 0 }
|
|
validates :status, presence: true, inclusion: { in: %w[draft active used expired refunded] }
|
|
validates :first_name, presence: true
|
|
validates :last_name, presence: true
|
|
|
|
# === Scopes ===
|
|
scope :draft, -> { where(status: "draft") }
|
|
scope :active, -> { where(status: "active") }
|
|
scope :expired_drafts, -> { joins(:order).where(status: "draft").where("orders.expires_at < ?", Time.current) }
|
|
|
|
before_validation :set_price_from_ticket_type, on: :create
|
|
before_validation :generate_qr_code, on: :create
|
|
|
|
# Generate PDF ticket
|
|
def to_pdf
|
|
TicketPdfGenerator.new(self).generate
|
|
end
|
|
|
|
# Price in euros (formatted)
|
|
def price_euros
|
|
price_cents / 100.0
|
|
end
|
|
|
|
# Delegate payment methods to order
|
|
def can_retry_payment?
|
|
order.can_retry_payment?
|
|
end
|
|
|
|
def expired?
|
|
order.expired?
|
|
end
|
|
|
|
def expiring_soon?
|
|
order.expiring_soon?
|
|
end
|
|
|
|
# Mark ticket as expired if it's past expiry time
|
|
def expire_if_overdue!
|
|
order.expire_if_overdue!
|
|
end
|
|
|
|
private
|
|
|
|
def set_price_from_ticket_type
|
|
return unless ticket_type
|
|
self.price_cents = ticket_type.price_cents
|
|
end
|
|
|
|
def generate_qr_code
|
|
return if qr_code.present?
|
|
|
|
loop do
|
|
self.qr_code = SecureRandom.uuid
|
|
break unless Ticket.exists?(qr_code: qr_code)
|
|
end
|
|
rescue => e
|
|
Rails.logger.error "Failed to generate QR code for ticket: #{e.message}"
|
|
# Generate a simple fallback QR code
|
|
self.qr_code = "#{id || 'temp'}-#{Time.current.to_i}-#{SecureRandom.hex(4)}"
|
|
end
|
|
|
|
def draft?
|
|
status == "draft"
|
|
end
|
|
end
|