develop #3

Merged
kbe merged 227 commits from develop into main 2025-09-16 14:35:23 +00:00
12 changed files with 338 additions and 24 deletions
Showing only changes of commit 1acc3e09d4 - Show all commits

View File

@@ -29,6 +29,11 @@ class PagesController < ApplicationController
.distinct .distinct
.limit(5) .limit(5)
# Draft tickets that can be retried
@draft_tickets = current_user.tickets.includes(:ticket_type, :event)
.can_retry_payment
.order(:expires_at)
# Events sections # Events sections
@today_events = Event.published.where("DATE(start_time) = ?", Date.current).order(start_time: :asc) @today_events = Event.published.where("DATE(start_time) = ?", Date.current).order(start_time: :asc)
@tomorrow_events = Event.published.where("DATE(start_time) = ?", Date.current + 1).order(start_time: :asc) @tomorrow_events = Event.published.where("DATE(start_time) = ?", Date.current + 1).order(start_time: :asc)

View File

@@ -106,6 +106,36 @@ class TicketsController < ApplicationController
@tickets = current_user.tickets.includes(:ticket_type) @tickets = current_user.tickets.includes(:ticket_type)
.where(id: draft_ticket_ids, status: "draft") .where(id: draft_ticket_ids, status: "draft")
# Check for expired tickets and clean them up
expired_tickets = @tickets.select(&:expired?)
if expired_tickets.any?
expired_tickets.each(&:expire_if_overdue!)
@tickets = @tickets.reject(&:expired?)
if @tickets.empty?
session.delete(:draft_ticket_ids)
redirect_to event_path(@event.slug, @event), alert: "Vos billets ont expiré. Veuillez recommencer votre commande."
return
end
flash[:notice] = "Certains billets ont expiré et ont été supprimés de votre commande."
end
# Check if tickets can still be retried
non_retryable_tickets = @tickets.reject(&:can_retry_payment?)
if non_retryable_tickets.any?
non_retryable_tickets.each(&:expire_if_overdue!)
@tickets = @tickets.select(&:can_retry_payment?)
if @tickets.empty?
session.delete(:draft_ticket_ids)
redirect_to event_path(@event.slug, @event), alert: "Nombre maximum de tentatives de paiement atteint. Veuillez recommencer votre commande."
return
end
flash[:notice] = "Certains billets ont atteint le nombre maximum de tentatives de paiement."
end
if @tickets.empty? if @tickets.empty?
redirect_to event_path(@event.slug, @event), alert: "Billets non trouvés ou déjà traités" redirect_to event_path(@event.slug, @event), alert: "Billets non trouvés ou déjà traités"
return return
@@ -113,10 +143,16 @@ class TicketsController < ApplicationController
@total_amount = @tickets.sum(&:price_cents) @total_amount = @tickets.sum(&:price_cents)
# Check for expiring soon tickets
@expiring_soon = @tickets.any?(&:expiring_soon?)
# Create Stripe checkout session if Stripe is configured # Create Stripe checkout session if Stripe is configured
if Rails.application.config.stripe[:secret_key].present? if Rails.application.config.stripe[:secret_key].present?
begin begin
@checkout_session = create_stripe_session @checkout_session = create_stripe_session
# Only increment payment attempts after successfully creating the session
@tickets.each(&:increment_payment_attempt!)
rescue => e rescue => e
error_message = e.message.present? ? e.message : "Erreur Stripe inconnue" error_message = e.message.present? ? e.message : "Erreur Stripe inconnue"
Rails.logger.error "Stripe checkout session creation failed: #{error_message}" Rails.logger.error "Stripe checkout session creation failed: #{error_message}"
@@ -138,9 +174,6 @@ class TicketsController < ApplicationController
return return
end end
# Stripe is now initialized at application startup, no need to initialize here
Rails.logger.debug "Payment success - Using globally initialized Stripe"
begin begin
stripe_session = Stripe::Checkout::Session.retrieve(session_id) stripe_session = Stripe::Checkout::Session.retrieve(session_id)
@@ -148,7 +181,7 @@ class TicketsController < ApplicationController
# Get event_id and ticket_ids from session metadata # Get event_id and ticket_ids from session metadata
event_id = stripe_session.metadata["event_id"] event_id = stripe_session.metadata["event_id"]
ticket_ids_data = stripe_session.metadata["ticket_ids"] ticket_ids_data = stripe_session.metadata["ticket_ids"]
unless event_id.present? && ticket_ids_data.present? unless event_id.present? && ticket_ids_data.present?
redirect_to dashboard_path, alert: "Informations de commande manquantes" redirect_to dashboard_path, alert: "Informations de commande manquantes"
return return
@@ -158,14 +191,14 @@ class TicketsController < ApplicationController
@event = Event.find(event_id) @event = Event.find(event_id)
ticket_ids = ticket_ids_data.split(",") ticket_ids = ticket_ids_data.split(",")
@tickets = current_user.tickets.where(id: ticket_ids, status: "draft") @tickets = current_user.tickets.where(id: ticket_ids, status: "draft")
if @tickets.empty? if @tickets.empty?
redirect_to dashboard_path, alert: "Billets non trouvés" redirect_to dashboard_path, alert: "Billets non trouvés"
return return
end end
@tickets.update_all(status: "active") @tickets.update_all(status: "active")
# Send confirmation emails # Send confirmation emails
@tickets.each do |ticket| @tickets.each do |ticket|
TicketMailer.purchase_confirmation(ticket).deliver_now TicketMailer.purchase_confirmation(ticket).deliver_now
@@ -192,16 +225,51 @@ class TicketsController < ApplicationController
# Handle payment failure/cancellation # Handle payment failure/cancellation
def payment_cancel def payment_cancel
redirect_to dashboard_path, alert: "Le paiement a été annulé" # Keep draft tickets for potential retry, just redirect back to checkout
draft_ticket_ids = session[:draft_ticket_ids] || []
if draft_ticket_ids.any?
tickets = current_user.tickets.where(id: draft_ticket_ids, status: "draft")
retryable_tickets = tickets.select(&:can_retry_payment?)
if retryable_tickets.any?
event = retryable_tickets.first.event
redirect_to ticket_checkout_path(event.slug, event.id),
alert: "Le paiement a été annulé. Vous pouvez réessayer."
else
session.delete(:draft_ticket_ids)
redirect_to dashboard_path, alert: "Le paiement a été annulé et vos billets ont expiré."
end
else
redirect_to dashboard_path, alert: "Le paiement a été annulé"
end
end end
def show # Allow users to retry payment for failed/cancelled payments
@ticket = current_user.tickets.includes(:ticket_type, :event).find(params[:ticket_id]) def retry_payment
@event = @ticket.event @event = Event.includes(:ticket_types).find(params[:id])
rescue ActiveRecord::RecordNotFound ticket_ids = params[:ticket_ids]&.split(',') || []
redirect_to dashboard_path, alert: "Billet non trouvé"
@tickets = current_user.tickets.where(id: ticket_ids)
.select(&:can_retry_payment?)
if @tickets.empty?
redirect_to event_path(@event.slug, @event),
alert: "Aucun billet disponible pour un nouveau paiement"
return
end
# Set session for checkout
session[:draft_ticket_ids] = @tickets.map(&:id)
redirect_to ticket_checkout_path(@event.slug, @event.id)
end end
def show
@ticket = current_user.tickets.includes(:ticket_type, :event).find(params[:ticket_id])
@event = @ticket.event
rescue ActiveRecord::RecordNotFound
redirect_to dashboard_path, alert: "Billet non trouvé"
end
private private
def set_event def set_event

View File

@@ -0,0 +1,15 @@
class CleanupExpiredDraftsJob < ApplicationJob
queue_as :default
def perform
expired_count = 0
Ticket.expired_drafts.find_each do |ticket|
Rails.logger.info "Expiring draft ticket #{ticket.id} for user #{ticket.user_id}"
ticket.expire_if_overdue!
expired_count += 1
end
Rails.logger.info "Expired #{expired_count} draft tickets" if expired_count > 0
end
end

View File

@@ -1,4 +1,8 @@
class Ticket < ApplicationRecord class Ticket < ApplicationRecord
# === Constants ===
DRAFT_EXPIRY_TIME = 30.minutes
MAX_PAYMENT_ATTEMPTS = 3
# === Associations === # === Associations ===
belongs_to :user belongs_to :user
belongs_to :ticket_type belongs_to :ticket_type
@@ -12,9 +16,17 @@ class Ticket < ApplicationRecord
validates :status, presence: true, inclusion: { in: %w[draft active used expired refunded] } validates :status, presence: true, inclusion: { in: %w[draft active used expired refunded] }
validates :first_name, presence: true validates :first_name, presence: true
validates :last_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 :set_price_from_ticket_type, on: :create
before_validation :generate_qr_code, on: :create before_validation :generate_qr_code, on: :create
before_validation :set_draft_expiry, on: :create
# Generate PDF ticket # Generate PDF ticket
def to_pdf def to_pdf
@@ -26,6 +38,38 @@ class Ticket < ApplicationRecord
price_cents / 100.0 price_cents / 100.0
end 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 private
def set_price_from_ticket_type def set_price_from_ticket_type
@@ -41,4 +85,14 @@ class Ticket < ApplicationRecord
break unless Ticket.exists?(qr_code: qr_code) break unless Ticket.exists?(qr_code: qr_code)
end end
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 end

View File

@@ -29,18 +29,18 @@
<div data-header-target="userMenu" class="absolute right-0 mt-2 w-48 rounded-md shadow-lg z-50 hidden"> <div data-header-target="userMenu" class="absolute right-0 mt-2 w-48 rounded-md shadow-lg z-50 hidden">
<%= link_to t("header.profile"), edit_user_registration_path, <%= link_to t("header.profile"), edit_user_registration_path,
class: "block px-4 py-2 text-sm bg-black text-gray-100 hover:bg-purple-700 first:rounded-t-md" %> class: "block px-4 py-2 text-sm text-gray-100 hover:bg-purple-700 first:rounded-t-md" %>
<%= link_to t("header.reservations"), "#", <%= link_to t("header.reservations"), "#",
class: "block px-4 py-2 text-sm bg-black text-gray-100 hover:bg-purple-700" %> class: "block px-4 py-2 text-sm text-gray-100 hover:bg-purple-700" %>
<%= link_to t("header.logout"), destroy_user_session_path, <%= link_to t("header.logout"), destroy_user_session_path,
data: { controller: "logout", action: "click->logout#signOut", data: { controller: "logout", action: "click->logout#signOut",
logout_url_value: destroy_user_session_path, redirect_url_value: "/", turbo: false }, logout_url_value: destroy_user_session_path, redirect_url_value: "/", turbo: false },
class: "block px-4 py-2 text-sm bg-black text-gray-100 hover:bg-purple-700 last:rounded-b-md" %> class: "block px-4 py-2 text-sm text-gray-100 hover:bg-purple-700 last:rounded-b-md" %>
</div> </div>
</div> </div>
<% else %> <% else %>
<%= link_to t("header.login"), new_user_session_path, <%= link_to t("header.login"), new_user_session_path,
class: "bg-black text-gray-100 hover:text-purple-200 px-3 py-2 rounded-md text-sm font-medium transition-colors duration-200" %> class: "text-gray-100 hover:text-purple-200 px-3 py-2 rounded-md text-sm font-medium transition-colors duration-200" %>
<%= link_to t("header.register"), new_user_registration_path, <%= link_to t("header.register"), new_user_registration_path,
class: "bg-purple-600 text-white font-medium py-2 px-4 rounded-lg hover:bg-purple-700 transition-colors duration-200" %> class: "bg-purple-600 text-white font-medium py-2 px-4 rounded-lg hover:bg-purple-700 transition-colors duration-200" %>
<% end %> <% end %>
@@ -63,9 +63,9 @@
<div data-header-target="mobileMenu" class="hidden sm:hidden"> <div data-header-target="mobileMenu" class="hidden sm:hidden">
<div class="px-2 pt-2 pb-3 space-y-1"> <div class="px-2 pt-2 pb-3 space-y-1">
<%= link_to t("header.parties"), events_path, <%= link_to t("header.parties"), events_path,
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
<%= link_to t("header.concerts"), "#", <%= link_to t("header.concerts"), "#",
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
</div> </div>
<div class="pt-4 pb-3 border-t border-gray-700"> <div class="pt-4 pb-3 border-t border-gray-700">
@@ -77,17 +77,17 @@
</div> </div>
<div class="px-2 space-y-1"> <div class="px-2 space-y-1">
<%= link_to t("header.profile"), edit_user_registration_path, <%= link_to t("header.profile"), edit_user_registration_path,
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
<%= link_to t("header.reservations"), "#", <%= link_to t("header.reservations"), "#",
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
<%= link_to t("header.logout"), destroy_user_session_path, <%= link_to t("header.logout"), destroy_user_session_path,
data: { controller: "logout", action: "click->logout#signOut", logout_url_value: destroy_user_session_path, login_url_value: new_user_session_path, turbo: false }, data: { controller: "logout", action: "click->logout#signOut", logout_url_value: destroy_user_session_path, login_url_value: new_user_session_path, turbo: false },
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
</div> </div>
<% else %> <% else %>
<div class="px-2 space-y-1"> <div class="px-2 space-y-1">
<%= link_to t("header.login"), new_user_session_path, <%= link_to t("header.login"), new_user_session_path,
class: "block px-3 py-2 rounded-md text-base font-medium bg-black text-gray-100 hover:text-purple-200 hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium text-gray-100 hover:text-purple-200 hover:bg-purple-700" %>
<%= link_to t("header.register"), new_user_registration_path, <%= link_to t("header.register"), new_user_registration_path,
class: "block px-3 py-2 rounded-md text-base font-medium bg-purple-600 text-white hover:bg-purple-700" %> class: "block px-3 py-2 rounded-md text-base font-medium bg-purple-600 text-white hover:bg-purple-700" %>
</div> </div>

View File

@@ -15,6 +15,78 @@
</div> </div>
</div> </div>
<!-- Draft tickets needing payment -->
<% if @draft_tickets.any? %>
<div class="card hover-lift mb-8 border-orange-200 bg-orange-50">
<div class="card-header bg-orange-100 rounded-lg">
<div class="mx-4 py-4">
<h2 class="text-2xl font-bold text-orange-900 flex items-center">
<svg class="w-6 h-6 mr-2 text-orange-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
Billets en attente de paiement
</h2>
<p class="text-orange-700 mt-1">Vous avez des billets qui nécessitent un paiement</p>
</div>
</div>
<div class="card-body">
<div class="space-y-4">
<% @draft_tickets.group_by(&:event).each do |event, tickets| %>
<div class="bg-white rounded-lg p-4 border border-orange-200">
<div class="flex items-start justify-between mb-3">
<div>
<h3 class="font-semibold text-gray-900"><%= event.name %></h3>
<p class="text-sm text-gray-600">
<svg class="w-4 h-4 inline mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
</svg>
<%= event.start_time.strftime("%d %B %Y à %H:%M") %>
</p>
</div>
<span class="text-sm font-medium text-orange-600 bg-orange-100 px-2 py-1 rounded-full">
<%= tickets.count %> billet<%= 's' if tickets.count > 1 %>
</span>
</div>
<div class="grid gap-2 mb-4">
<% tickets.each do |ticket| %>
<div class="flex items-center justify-between text-sm bg-gray-50 rounded p-2">
<div>
<span class="font-medium"><%= ticket.ticket_type.name %></span>
<span class="text-gray-600">- <%= ticket.first_name %> <%= ticket.last_name %></span>
</div>
<div class="flex items-center space-x-2">
<span class="text-gray-600">Expire <%= time_ago_in_words(ticket.expires_at) %></span>
<span class="font-medium text-gray-900"><%= number_to_currency(ticket.price_euros, unit: "€") %></span>
</div>
</div>
<% end %>
</div>
<div class="flex items-center justify-between">
<div class="text-sm text-gray-600">
<% max_attempts = tickets.map(&:payment_attempts).max %>
Tentatives: <%= max_attempts %>/3
<% if tickets.any?(&:expiring_soon?) %>
<span class="text-orange-600 font-medium ml-2">⚠️ Expire bientôt</span>
<% end %>
</div>
<%= form_tag ticket_retry_payment_path(event.slug, event.id), method: :post do %>
<%= hidden_field_tag :ticket_ids, tickets.map(&:id).join(',') %>
<%= submit_tag "Reprendre le paiement",
class: "inline-flex items-center px-4 py-2 bg-orange-600 text-white text-sm font-medium rounded-lg hover:bg-orange-700 transition-colors duration-200" %>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
<!-- User's booked events --> <!-- User's booked events -->
<div class="card hover-lift mb-8"> <div class="card hover-lift mb-8">
<div class="card-header"> <div class="card-header">

View File

@@ -31,6 +31,45 @@
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<!-- Order Summary --> <!-- Order Summary -->
<div class="bg-white rounded-2xl shadow-xl p-6 md:p-8 h-fit"> <div class="bg-white rounded-2xl shadow-xl p-6 md:p-8 h-fit">
<!-- Warning for expiring tickets -->
<% if @expiring_soon %>
<div class="mb-6 bg-orange-50 border border-orange-200 rounded-lg p-4">
<div class="flex items-start">
<svg class="w-5 h-5 text-orange-600 mr-2 mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<div>
<h3 class="font-medium text-orange-800 mb-1">Attention - Billets bientôt expirés</h3>
<p class="text-orange-700 text-sm">Vos billets vont expirer dans quelques minutes. Veuillez procéder rapidement au paiement pour éviter leur suppression automatique.</p>
</div>
</div>
</div>
<% end %>
<!-- Payment attempts warning -->
<% max_attempts = @tickets.map(&:payment_attempts).max %>
<% if max_attempts >= 0 %>
<% current_attempt = max_attempts + 1 %>
<div class="mb-6 bg-yellow-50 border border-yellow-200 rounded-lg p-4">
<div class="flex items-start">
<svg class="w-5 h-5 text-yellow-600 mr-2 mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<div>
<h3 class="font-medium text-yellow-800 mb-1">Tentative de paiement <%= current_attempt %>/3</h3>
<p class="text-yellow-700 text-sm">
<% remaining_attempts = 3 - current_attempt %>
<% if remaining_attempts > 0 %>
Il vous reste <%= remaining_attempts %> tentative<%= 's' if remaining_attempts > 1 %> après celle-ci.
<% else %>
Ceci est votre dernière tentative de paiement.
<% end %>
</p>
</div>
</div>
</div>
<% end %>
<div class="text-center mb-6"> <div class="text-center mb-6">
<div class="mx-auto bg-green-100 rounded-full p-3 w-16 h-16 flex items-center justify-center mb-4"> <div class="mx-auto bg-green-100 rounded-full p-3 w-16 h-16 flex items-center justify-center mb-4">
<svg class="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg class="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">

View File

@@ -0,0 +1,23 @@
# Schedule regular cleanup of expired draft tickets
#
# This will run every 10 minutes to clean up expired draft tickets
# If you're using a more sophisticated scheduler like sidekiq or whenever,
# you can move this logic there.
Rails.application.config.after_initialize do
# Only run in production and development, not in test
unless Rails.env.test?
# Schedule the cleanup job to run every 10 minutes
Thread.new do
loop do
begin
CleanupExpiredDraftsJob.perform_later
rescue => e
Rails.logger.error "Failed to schedule expired drafts cleanup: #{e.message}"
end
sleep 10.minutes
end
end
end
end

View File

@@ -42,6 +42,7 @@ Rails.application.routes.draw do
get "events/:slug.:id/tickets/new", to: "tickets#new", as: "ticket_new" get "events/:slug.:id/tickets/new", to: "tickets#new", as: "ticket_new"
post "events/:slug.:id/tickets/create", to: "tickets#create", as: "ticket_create" post "events/:slug.:id/tickets/create", to: "tickets#create", as: "ticket_create"
get "events/:slug.:id/tickets/checkout", to: "tickets#checkout", as: "ticket_checkout" get "events/:slug.:id/tickets/checkout", to: "tickets#checkout", as: "ticket_checkout"
post "events/:slug.:id/tickets/retry", to: "tickets#retry_payment", as: "ticket_retry_payment"
# Payment routes # Payment routes
get "payments/success", to: "tickets#payment_success", as: "payment_success" get "payments/success", to: "tickets#payment_success", as: "payment_success"

View File

@@ -9,6 +9,14 @@ class CreateTickets < ActiveRecord::Migration[8.0]
t.string :first_name t.string :first_name
t.string :last_name t.string :last_name
# Implemented to "temporize" tickets
# If a ticket is not paid in time, it is removed from the database
#
t.string :stripe_session_id
t.timestamp :expires_at
t.integer :payment_attempts, default: 0
t.timestamp :last_payment_attempt_at
t.references :user, null: true, foreign_key: false t.references :user, null: true, foreign_key: false
t.references :ticket_type, null: false, foreign_key: false t.references :ticket_type, null: false, foreign_key: false
@@ -22,5 +30,9 @@ class CreateTickets < ActiveRecord::Migration[8.0]
# Add indexes for better performance # Add indexes for better performance
# add_index :tickets, :first_name unless index_exists?(:tickets, :first_name) # add_index :tickets, :first_name unless index_exists?(:tickets, :first_name)
# add_index :tickets, :last_name unless index_exists?(:tickets, :last_name) # add_index :tickets, :last_name unless index_exists?(:tickets, :last_name)
#
# add_index :tickets, :stripe_session_id, unique: true
# add_index :tickets, [ :status, :expires_at ]
# add_index :tickets, [ :user_id, :status ]
end end
end end

7
db/schema.rb generated Executable file → Normal file
View File

@@ -40,7 +40,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_08_23_171354) do
t.integer "quantity" t.integer "quantity"
t.datetime "sale_start_at" t.datetime "sale_start_at"
t.datetime "sale_end_at" t.datetime "sale_end_at"
t.boolean "requires_id"
t.integer "minimum_age" t.integer "minimum_age"
t.bigint "event_id", null: false t.bigint "event_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
@@ -53,9 +52,13 @@ ActiveRecord::Schema[8.0].define(version: 2025_08_23_171354) do
create_table "tickets", charset: "utf8mb4", collation: "utf8mb4_uca1400_ai_ci", force: :cascade do |t| create_table "tickets", charset: "utf8mb4", collation: "utf8mb4_uca1400_ai_ci", force: :cascade do |t|
t.string "qr_code" t.string "qr_code"
t.integer "price_cents" t.integer "price_cents"
t.string "status", default: "active" t.string "status", default: "draft"
t.string "first_name" t.string "first_name"
t.string "last_name" t.string "last_name"
t.string "stripe_session_id"
t.timestamp "expires_at"
t.integer "payment_attempts", default: 0
t.timestamp "last_payment_attempt_at"
t.bigint "user_id" t.bigint "user_id"
t.bigint "ticket_type_id", null: false t.bigint "ticket_type_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false

22
lib/tasks/tickets.rake Normal file
View File

@@ -0,0 +1,22 @@
namespace :tickets do
desc "Clean up expired draft tickets"
task cleanup_expired_drafts: :environment do
puts "Starting cleanup of expired draft tickets..."
CleanupExpiredDraftsJob.perform_now
puts "Cleanup completed."
end
desc "Show stats about draft tickets"
task stats: :environment do
total_drafts = Ticket.draft.count
expired_drafts = Ticket.expired_drafts.count
retryable_drafts = Ticket.can_retry_payment.count
puts "=== Draft Ticket Statistics ==="
puts "Total draft tickets: #{total_drafts}"
puts "Expired draft tickets: #{expired_drafts}"
puts "Retryable draft tickets: #{retryable_drafts}"
puts "Max payment attempts: #{Ticket::MAX_PAYMENT_ATTEMPTS}"
puts "Draft expiry time: #{Ticket::DRAFT_EXPIRY_TIME}"
end
end