class Order < ApplicationRecord # === Constants === DRAFT_EXPIRY_TIME = 30.minutes MAX_PAYMENT_ATTEMPTS = 3 # === Associations === belongs_to :user belongs_to :event has_many :tickets, dependent: :destroy # === Validations === validates :user_id, presence: true validates :event_id, presence: true validates :status, presence: true, inclusion: { in: %w[draft pending_payment paid completed cancelled expired] } validates :total_amount_cents, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :payment_attempts, presence: true, numericality: { greater_than_or_equal_to: 0 } # === Scopes === scope :draft, -> { where(status: "draft") } scope :active, -> { where(status: %w[paid completed]) } 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_expiry, on: :create # === Instance Methods === # Total amount in euros (formatted) def total_amount_euros total_amount_cents / 100.0 end # Check if order can be retried for payment def can_retry_payment? draft? && payment_attempts < MAX_PAYMENT_ATTEMPTS && !expired? end # Check if order is expired def expired? expires_at.present? && expires_at < Time.current end # Mark order 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 # Mark order as paid and activate all tickets def mark_as_paid! transaction do update!(status: "paid") tickets.update_all(status: "active") end end # Calculate total from tickets def calculate_total! update!(total_amount_cents: tickets.sum(:price_cents)) end private def set_expiry return unless status == "draft" self.expires_at = DRAFT_EXPIRY_TIME.from_now if expires_at.blank? end def draft? status == "draft" end end