From 59ae33d5c76beeaf035c9f83fa940158a9269b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Benavente?= Date: Mon, 22 Jan 2024 02:16:49 -0300 Subject: [PATCH] Fix user stats subtraction upon deleting seasons and sessions --- app/controllers/rankings_controller.rb | 4 ---- app/controllers/seasons_controller.rb | 27 +++++++++++++++++++++----- app/controllers/sessions_controller.rb | 16 +++++++-------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/controllers/rankings_controller.rb b/app/controllers/rankings_controller.rb index fb31ed10..37e02250 100644 --- a/app/controllers/rankings_controller.rb +++ b/app/controllers/rankings_controller.rb @@ -67,12 +67,8 @@ def update # DELETE /rankings/1 or /rankings/1.json def destroy - sessions = @ranking.sessions - respond_to do |format| if @ranking.destroy! - sessions.each(&destroy) - format.html { redirect_to rankings_url, :notice => 'Ranking was successfully deleted.' } format.json { head :no_content } else diff --git a/app/controllers/seasons_controller.rb b/app/controllers/seasons_controller.rb index 6d1f490b..ecbb8a7d 100644 --- a/app/controllers/seasons_controller.rb +++ b/app/controllers/seasons_controller.rb @@ -71,17 +71,34 @@ def update # DELETE /seasons/1 or /seasons/1.json def destroy + require 'rva_calculate_results_service' + require 'stats_service' + require 'team_points_service' + @season.rankings.each do |r| - r.sessions.each(&:destroy) + r.sessions.each do |s| + + rva_results = RvaCalculateResultsService.new(s).call + + if s.teams? + TeamPointsService.new(s, rva_results).remove_team_points + else + StatsService.new(s, rva_results).remove_stats + end + + Rails.cache.delete("Session:#{s.id}") + + s.destroy! + end + r.destroy! end + @season.tracks.each(&:destroy) + @season.cars.each(&:destroy) + respond_to do |format| if @season.destroy! - rankings.each(&destroy) - tracks.each(&destroy) - cars.each(&destroy) - format.html { redirect_to seasons_url, :notice => 'Season was successfully deleted.' } format.json { head :no_content } else diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 72826a5e..abd1a190 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -71,18 +71,16 @@ def destroy require 'stats_service' require 'team_points_service' - session = Session.new(@session.attributes) # copy + rva_results = RvaCalculateResultsService.new(@session).call + + if @session.teams? + TeamPointsService.new(@session, rva_results).remove_team_points + else + StatsService.new(@session, rva_results).remove_stats + end respond_to do |format| if @session.destroy! - rva_results = RvaCalculateResultsService.new(session).call - - if session.teams? - TeamPointsService.new(session, rva_results).remove_team_points - else - StatsService.new(session, rva_results).remove_stats - end - format.html { redirect_to sessions_url, :notice => 'Session was successfully deleted.' } format.json { head :no_content }