Files

47 lines
1.2 KiB
Ruby

module Tournaments
class Destroy
class LiveBroadcastError < StandardError; end
def self.call(tournament:)
new(tournament).call
end
def initialize(tournament)
@tournament = tournament
end
def call
raise LiveBroadcastError, I18n.t("flash.tournaments.delete_blocked_live") if live_broadcast?
team = @tournament.broadcast_team
Tournament.transaction do
purge_recordings!
@tournament.update_column(:broadcast_team_id, nil) if team
@tournament.destroy!
destroy_orphan_broadcast_team!(team)
end
end
private
def live_broadcast?
StreamSession.where(
match_id: @tournament.matches.select(:id),
status: %w[connecting live reconnecting paused]
).exists?
end
def purge_recordings!
session_ids = StreamSession.where(match_id: @tournament.matches.select(:id)).select(:id)
Recording.where(stream_session_id: session_ids).find_each(&:destroy!)
end
def destroy_orphan_broadcast_team!(team)
return unless team&.tournament_broadcast?
return if Tournament.exists?(broadcast_team_id: team.id)
team.destroy!
end
end
end