Skip to content

Commit

Permalink
Check competition dates for upcoming comps while banning (#10573)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljames-dj authored Jan 9, 2025
1 parent 9dc018e commit a7354e8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/controllers/api/v0/user_roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/api/v0/user_roles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a7354e8

Please sign in to comment.