refactor: Clean up TicketsController after order migration
- Remove unused 'new' and 'create' methods moved to OrdersController - Update controller documentation to reflect new purpose as legacy redirect handler - Remove unused private methods (ticket_params) - Keep only legacy redirect methods for backward compatibility - Update before_actions to match remaining functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,99 +1,11 @@
|
|||||||
# Manage tickets creation
|
# Legacy tickets controller - redirects to new order system
|
||||||
#
|
#
|
||||||
# This controller permit users to create a new ticket for an event,
|
# This controller now primarily handles legacy redirects and backward compatibility
|
||||||
# complete their details and proceed to payment
|
# Most ticket creation functionality has been moved to OrdersController
|
||||||
class TicketsController < ApplicationController
|
class TicketsController < ApplicationController
|
||||||
before_action :authenticate_user!, only: [ :new, :payment_success, :payment_cancel ]
|
before_action :authenticate_user!, only: [ :payment_success, :payment_cancel ]
|
||||||
before_action :set_event, only: [ :new, :create ]
|
before_action :set_event, only: [ :checkout, :retry_payment ]
|
||||||
|
|
||||||
# Handle new ticket creation
|
|
||||||
#
|
|
||||||
# Once user selected ticket types he wans for an event
|
|
||||||
# he cames here where he can complete his details (first_name, last_name)
|
|
||||||
# for each ticket ordered
|
|
||||||
def new
|
|
||||||
@cart_data = session[:pending_cart] || {}
|
|
||||||
|
|
||||||
if @cart_data.empty?
|
|
||||||
redirect_to event_path(@event.slug, @event), alert: "Veuillez d'abord sélectionner vos billets sur la page de l'événement"
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
# Build list of tickets requiring names
|
|
||||||
@tickets_needing_names = []
|
|
||||||
@cart_data.each do |ticket_type_id, item|
|
|
||||||
ticket_type = @event.ticket_types.find_by(id: ticket_type_id)
|
|
||||||
next unless ticket_type
|
|
||||||
|
|
||||||
quantity = item["quantity"].to_i
|
|
||||||
next if quantity <= 0
|
|
||||||
|
|
||||||
quantity.times do |i|
|
|
||||||
@tickets_needing_names << {
|
|
||||||
ticket_type_id: ticket_type.id,
|
|
||||||
ticket_type_name: ticket_type.name,
|
|
||||||
index: i
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Create a new order with tickets
|
|
||||||
#
|
|
||||||
# Here a new order is created with associated tickets in draft state.
|
|
||||||
# When user is ready they can proceed to payment via the order checkout
|
|
||||||
def create
|
|
||||||
@cart_data = session[:pending_cart] || {}
|
|
||||||
|
|
||||||
if @cart_data.empty?
|
|
||||||
redirect_to event_path(@event.slug, @event), alert: "Aucun billet sélectionné"
|
|
||||||
return
|
|
||||||
end
|
|
||||||
success = false
|
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
@order = current_user.orders.create!(event: @event, status: "draft")
|
|
||||||
|
|
||||||
ticket_params[:tickets_attributes]&.each do |index, ticket_attrs|
|
|
||||||
next if ticket_attrs[:first_name].blank? || ticket_attrs[:last_name].blank?
|
|
||||||
|
|
||||||
ticket_type = @event.ticket_types.find(ticket_attrs[:ticket_type_id])
|
|
||||||
|
|
||||||
ticket = @order.tickets.build(
|
|
||||||
ticket_type: ticket_type,
|
|
||||||
first_name: ticket_attrs[:first_name],
|
|
||||||
last_name: ticket_attrs[:last_name],
|
|
||||||
status: "draft"
|
|
||||||
)
|
|
||||||
|
|
||||||
unless ticket.save
|
|
||||||
flash[:alert] = "Erreur lors de la création des billets: #{ticket.errors.full_messages.join(', ')}"
|
|
||||||
raise ActiveRecord::Rollback
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if @order.tickets.present?
|
|
||||||
@order.calculate_total!
|
|
||||||
success = true
|
|
||||||
else
|
|
||||||
flash[:alert] = "Aucun billet valide créé"
|
|
||||||
raise ActiveRecord::Rollback
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Handle redirects outside transaction
|
|
||||||
if success
|
|
||||||
session[:draft_order_id] = @order.id
|
|
||||||
session.delete(:pending_cart)
|
|
||||||
redirect_to checkout_order_path(@order)
|
|
||||||
else
|
|
||||||
redirect_to ticket_new_path
|
|
||||||
end
|
|
||||||
rescue => e
|
|
||||||
error_message = e.message.present? ? e.message : "Erreur inconnue"
|
|
||||||
flash[:alert] = "Une erreur est survenue: #{error_message}"
|
|
||||||
redirect_to ticket_new_path
|
|
||||||
end
|
|
||||||
|
|
||||||
# Redirect to order-based checkout
|
# Redirect to order-based checkout
|
||||||
def checkout
|
def checkout
|
||||||
@@ -162,9 +74,6 @@ class TicketsController < ApplicationController
|
|||||||
redirect_to events_path, alert: "Événement non trouvé"
|
redirect_to events_path, alert: "Événement non trouvé"
|
||||||
end
|
end
|
||||||
|
|
||||||
def ticket_params
|
|
||||||
params.permit(tickets_attributes: [ :ticket_type_id, :first_name, :last_name ])
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_stripe_session
|
def create_stripe_session
|
||||||
line_items = @tickets.map do |ticket|
|
line_items = @tickets.map do |ticket|
|
||||||
|
|||||||
Reference in New Issue
Block a user