- Add TicketPdfGenerator service for creating PDF tickets with QR codes - Implement download_ticket action in TicketsController - Update ticket routes to support both ID and QR code access - Add to_pdf method to Ticket model using TicketPdfGenerator - Resolve conflicts between email notifications and PDF ticket features - Maintain backward compatibility with existing QR code routes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
123 lines
4.2 KiB
Ruby
123 lines
4.2 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_ticket ]
|
|
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
|
|
# Try to find by qr_code first (for backward compatibility)
|
|
if params[:qr_code].present?
|
|
@ticket = Ticket.joins(order: :user).includes(:event, :ticket_type, order: :user)
|
|
.find_by(tickets: { qr_code: params[:qr_code] })
|
|
else
|
|
# Find by ticket_id with user ownership check
|
|
@ticket = Ticket.joins(order: :user).includes(:event, :ticket_type, order: :user).find_by(
|
|
tickets: { id: params[:ticket_id] },
|
|
orders: { user_id: current_user.id }
|
|
)
|
|
end
|
|
|
|
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_ticket
|
|
# Find ticket and ensure it belongs to current user
|
|
@ticket = Ticket.joins(order: :user).includes(:event, :ticket_type, order: :user).find_by(
|
|
tickets: { id: params[:ticket_id] },
|
|
orders: { user_id: current_user.id }
|
|
)
|
|
|
|
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
|