mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-05-13 17:18:58 +00:00
43 lines
1.5 KiB
Ruby
43 lines
1.5 KiB
Ruby
class TournamentCleanupJob < ApplicationJob
|
|
queue_as :default
|
|
|
|
def perform
|
|
# Task 1: Remove tournaments that are 1 week old and have 0 matches finished
|
|
remove_empty_old_tournaments
|
|
|
|
# Task 2: Cleanup tournaments that are a week old with at least 1 finished match
|
|
cleanup_active_old_tournaments
|
|
end
|
|
|
|
private
|
|
|
|
def remove_empty_old_tournaments
|
|
# Find tournaments older than 1 week with no finished matches
|
|
Tournament.where('date < ?', 1.week.ago.to_date)
|
|
.joins('LEFT JOIN matches ON matches.tournament_id = tournaments.id AND matches.finished = 1')
|
|
.group('tournaments.id')
|
|
.having('COUNT(matches.id) = 0')
|
|
.destroy_all
|
|
end
|
|
|
|
def cleanup_active_old_tournaments
|
|
# Find tournaments older than 1 week with at least 1 finished match
|
|
old_active_tournaments = Tournament.where('date < ?', 1.week.ago.to_date)
|
|
.joins('INNER JOIN matches ON matches.tournament_id = tournaments.id AND matches.finished = 1')
|
|
.group('tournaments.id')
|
|
.having('COUNT(matches.id) > 0')
|
|
|
|
old_active_tournaments.each do |tournament|
|
|
# 1. Remove all school delegates
|
|
tournament.schools.each do |school|
|
|
school.delegates.destroy_all
|
|
end
|
|
|
|
# 2. Remove all tournament delegates
|
|
tournament.delegates.destroy_all
|
|
|
|
# 3. Set user_id to null
|
|
tournament.update(user_id: nil)
|
|
end
|
|
end
|
|
end |