From a7354e815e297298706ae96f282ae7a989dc438a Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 9 Jan 2025 06:45:24 +0530 Subject: [PATCH] Check competition dates for upcoming comps while banning (#10573) --- app/controllers/api/v0/user_roles_controller.rb | 7 +++++-- app/models/competition.rb | 1 + spec/controllers/api/v0/user_roles_controller_spec.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v0/user_roles_controller.rb b/app/controllers/api/v0/user_roles_controller.rb index c5a81dcd82..aa189a5120 100644 --- a/app/controllers/api/v0/user_roles_controller.rb +++ b/app/controllers/api/v0/user_roles_controller.rb @@ -91,10 +91,13 @@ def create user = User.find(user_id) ban_reason = params[:banReason] scope = params[:scope] - upcoming_comps_for_user = user.competitions_registered_for.not_over.merge(Registration.not_cancelled).pluck(:id) + upcoming_comps_for_user = user.competitions_registered_for.not_over.merge(Registration.not_cancelled) + if end_date.present? + upcoming_comps_for_user = upcoming_comps_for_user.between_dates(Date.today, end_date) + end unless upcoming_comps_for_user.empty? return render status: :unprocessable_entity, json: { - error: "The user has upcoming competitions: #{upcoming_comps_for_user.join(', ')}. Before banning the user, make sure their registrations are deleted.", + error: "The user has upcoming competitions: #{upcoming_comps_for_user.pluck(:id).join(', ')}. Before banning the user, make sure their registrations are deleted.", } end end diff --git a/app/models/competition.rb b/app/models/competition.rb index 358ad74b69..67a4989a04 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -54,6 +54,7 @@ class Competition < ApplicationRecord scope :not_visible, -> { where(showAtAll: false) } scope :over, -> { where("results_posted_at IS NOT NULL OR end_date < ?", Date.today) } scope :not_over, -> { where("results_posted_at IS NULL AND end_date >= ?", Date.today) } + scope :between_dates, ->(start_date, end_date) { where("start_date <= ? AND end_date >= ?", end_date, start_date) } scope :end_date_passed_since, lambda { |num_days| where(end_date: ...(num_days.days.ago)) } scope :belongs_to_region, lambda { |region_id| joins(:country).where( diff --git a/spec/controllers/api/v0/user_roles_controller_spec.rb b/spec/controllers/api/v0/user_roles_controller_spec.rb index caaf7ae97a..751377be2c 100644 --- a/spec/controllers/api/v0/user_roles_controller_spec.rb +++ b/spec/controllers/api/v0/user_roles_controller_spec.rb @@ -125,6 +125,15 @@ expect(response_json["error"]).to eq "The user has upcoming competitions: #{upcoming_comps_for_user.join(', ')}. Before banning the user, make sure their registrations are deleted." end + it "can ban a user if the user's upcoming competitions are after end date" do + post :create, params: { + userId: user_to_be_banned_with_future_comps.id, + groupType: UserGroup.group_types[:banned_competitors], + endDate: user_to_be_banned_with_future_comps.competitions_registered_for.not_over.merge(Registration.not_cancelled).first.end_date - 1, + } + expect(response).to be_successful + end + it 'can ban a user if the user have a deleted registration in an upcoming competitions' do post :create, params: { userId: user_to_be_banned_with_deleted_registration_in_future_comps.id,