- Rename download_ticket action to download for consistency - Use QR code lookup consistently in both show and download actions - Simplify routes to use QR code pattern for both viewing and downloading - Remove complex dual-lookup logic in favor of consistent QR code access - Clean up route constraints and duplicate route definitions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
113 lines
3.8 KiB
Ruby
113 lines
3.8 KiB
Ruby
# Legacy tickets controller - redirects to new order system
|
|
#
|
|
# This controller now primarily handles legacy redirects and backward compatibility
|
|
# Most ticket creation functionality has been moved to OrdersController
|
|
class TicketsController < ApplicationController
|
|
before_action :authenticate_user!, only: [ :payment_success, :payment_cancel, :show, :download ]
|
|
before_action :set_event, only: [ :checkout, :retry_payment ]
|
|
|
|
|
|
# Redirect to order-based checkout
|
|
def checkout
|
|
# Check for draft order
|
|
if session[:draft_order_id].present?
|
|
order = current_user.orders.find_by(id: session[:draft_order_id], status: "draft")
|
|
if order.present?
|
|
redirect_to order_checkout_path(order)
|
|
return
|
|
end
|
|
end
|
|
|
|
# No order found
|
|
@event = Event.includes(:ticket_types).find(params[:id])
|
|
redirect_to event_path(@event.slug, @event), alert: "Aucun billet en attente de paiement"
|
|
end
|
|
|
|
# Redirect to order-based payment success
|
|
def payment_success
|
|
redirect_to order_payment_success_path(session_id: params[:session_id])
|
|
end
|
|
|
|
# Redirect to order-based payment cancel
|
|
def payment_cancel
|
|
redirect_to order_payment_cancel_path
|
|
end
|
|
|
|
# Redirect retry payment to order system
|
|
def retry_payment
|
|
@event = Event.includes(:ticket_types).find(params[:id])
|
|
|
|
# Look for draft order for this event
|
|
order = current_user.orders.find_by(event: @event, status: "draft")
|
|
|
|
if order&.can_retry_payment?
|
|
redirect_to retry_payment_order_path(order)
|
|
else
|
|
redirect_to event_path(@event.slug, @event),
|
|
alert: "Aucune commande disponible pour un nouveau paiement"
|
|
end
|
|
end
|
|
|
|
# Display ticket details
|
|
def show
|
|
# Find ticket by qr code id
|
|
@ticket = Ticket.joins(order: :user).includes(:event, :ticket_type, order: :user)
|
|
.find_by(tickets: { qr_code: params[:qr_code] })
|
|
|
|
if @ticket.nil?
|
|
redirect_to dashboard_path, alert: "Billet non trouvé"
|
|
return
|
|
end
|
|
@event = @ticket.event
|
|
rescue ActiveRecord::RecordNotFound
|
|
redirect_to dashboard_path, alert: "Billet non trouvé"
|
|
end
|
|
|
|
# Download PDF ticket - only accessible by ticket owner
|
|
# User must be authenticated to download ticket
|
|
# TODO: change ID to an unique identifier (UUID)
|
|
def download
|
|
# Find ticket by qr code id
|
|
@ticket = Ticket.joins(order: :user).includes(:event, :ticket_type, order: :user)
|
|
.find_by(tickets: { qr_code: params[:qr_code] })
|
|
|
|
if @ticket.nil?
|
|
redirect_to dashboard_path, alert: "Billet non trouvé ou vous n'avez pas l'autorisation d'accéder à ce billet"
|
|
return
|
|
end
|
|
|
|
# Generate PDF
|
|
pdf_content = @ticket.to_pdf
|
|
|
|
# Send PDF as download
|
|
send_data pdf_content,
|
|
filename: "ticket_#{@ticket.id}_#{@ticket.event.name.parameterize}.pdf",
|
|
type: "application/pdf",
|
|
disposition: "attachment"
|
|
rescue ActiveRecord::RecordNotFound
|
|
redirect_to dashboard_path, alert: "Billet non trouvé"
|
|
rescue => e
|
|
Rails.logger.error "Error generating ticket PDF: #{e.message}"
|
|
redirect_to dashboard_path, alert: "Erreur lors de la génération du billet"
|
|
end
|
|
private
|
|
|
|
def set_event
|
|
event_id = params[:id] || session[:event_id]
|
|
|
|
Rails.logger.debug "TicketsController#set_event - params[:id]: #{params[:id].inspect}, session[:event_id]: #{session[:event_id].inspect}"
|
|
|
|
unless event_id
|
|
Rails.logger.error "TicketsController#set_event - No event ID found"
|
|
redirect_to events_path, alert: "Aucun événement spécifié"
|
|
return
|
|
end
|
|
|
|
@event = Event.includes(:ticket_types).find(event_id)
|
|
Rails.logger.debug "TicketsController#set_event - Found event: #{@event.id} - #{@event.name}"
|
|
rescue ActiveRecord::RecordNotFound
|
|
Rails.logger.error "TicketsController#set_event - Event not found with ID: #{event_id}"
|
|
redirect_to events_path, alert: "Événement non trouvé"
|
|
end
|
|
end
|