class Ticket < ApplicationRecord # === Constants === DRAFT_EXPIRY_TIME = 30.minutes MAX_PAYMENT_ATTEMPTS = 3 # === Associations === belongs_to :user belongs_to :ticket_type has_one :event, through: :ticket_type # === Validations === validates :qr_code, presence: true, uniqueness: true validates :user_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 validates :payment_attempts, presence: true, numericality: { greater_than_or_equal_to: 0 } # === Scopes === scope :draft, -> { where(status: "draft") } scope :active, -> { where(status: "active") } scope :expired_drafts, -> { draft.where("expires_at < ?", Time.current) } scope :can_retry_payment, -> { draft.where("payment_attempts < ? AND expires_at > ?", MAX_PAYMENT_ATTEMPTS, Time.current) } before_validation :set_price_from_ticket_type, on: :create before_validation :generate_qr_code, on: :create before_validation :set_draft_expiry, 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 # Check if ticket can be retried for payment def can_retry_payment? draft? && payment_attempts < MAX_PAYMENT_ATTEMPTS && !expired? end # Check if ticket is expired def expired? expires_at.present? && expires_at < Time.current end # Mark ticket as expired if it"s past expiry time def expire_if_overdue! return unless draft? && expired? update!(status: "expired") end # Increment payment attempt counter def increment_payment_attempt! update!( payment_attempts: payment_attempts + 1, last_payment_attempt_at: Time.current ) end # Check if draft is about to expire (within 5 minutes) def expiring_soon? return false unless draft? && expires_at.present? expires_at <= 5.minutes.from_now 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 end def set_draft_expiry return unless status == "draft" self.expires_at = DRAFT_EXPIRY_TIME.from_now if expires_at.blank? end def draft? status == "draft" end end