Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for newcomer month #10328

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class Competition < ApplicationRecord
event_change_deadline_date
competition_series_id
registration_version
newcomer_reserved_spots
).freeze
VALID_NAME_RE = /\A([-&.:' [:alnum:]]+) (\d{4})\z/
VALID_ID_RE = /\A[a-zA-Z0-9]+\Z/
Expand All @@ -188,6 +189,9 @@ class Competition < ApplicationRecord
MAX_MARKDOWN_LENGTH = 255
MAX_COMPETITOR_LIMIT = 5000
MAX_GUEST_LIMIT = 100
NEWCOMER_MONTH_ENABLED = true
MAX_NEWCOMER_SPOTS_RESERVED_FRACTION = 0.5

validates_inclusion_of :competitor_limit_enabled, in: [true, false], if: :competitor_limit_required?
validates_numericality_of :competitor_limit, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_COMPETITOR_LIMIT, if: :competitor_limit_enabled?
validates :competitor_limit_reason, presence: true, if: :competitor_limit_enabled?
Expand Down Expand Up @@ -242,6 +246,30 @@ class Competition < ApplicationRecord
validates :external_website, :external_registration_page, length: { maximum: MAX_URL_LENGTH }
validates :contact, length: { maximum: MAX_MARKDOWN_LENGTH }

validate :validate_newcomer_reserved_spots
private def validate_newcomer_reserved_spots
return unless competitor_limit.present?

if newcomer_reserved_spots > 0 && !NEWCOMER_MONTH_ENABLED
errors.add(:newcomer_reserved_spots, 'newcomer competitions are not allowed at present')
end

max_newcomer_spots = (competitor_limit * MAX_NEWCOMER_SPOTS_RESERVED_FRACTION).to_i
if newcomer_reserved_spots > max_newcomer_spots
errors.add(:newcomer_reserved_spots, 'cant reserve more than 50% of spots for newcomers')
end
end

# TODO: Make sure this query is optimized
def newcomers_competing
registrations.competing_status_accepted.includes(:user).select { |registration| registration.user.newcomer? }
end

# TODO: What if there are no newcomer reserved spots? Is this represented as nil or 0 in the db?
def newcomer_reserved_spots_remaining
newcomer_reserved_spots - newcomers_competing.count
end

# Dirty old trick to deal with competition id changes (see other methods using
# 'with_old_id' for more details).
def persisted_events_id
Expand Down Expand Up @@ -2351,6 +2379,7 @@ def to_form_data
"enabled" => competitor_limit_enabled,
"count" => competitor_limit,
"reason" => competitor_limit_reason,
"newcomerReservedSpots" => newcomer_reserved_spots,
},
"staff" => {
"staffDelegateIds" => staff_delegates.to_a.pluck(:id),
Expand Down Expand Up @@ -2452,6 +2481,7 @@ def form_errors
"enabled" => errors[:competitor_limit_enabled],
"count" => errors[:competitor_limit],
"reason" => errors[:competitor_limit_reason],
"newcomer_reserved_spots" => errors[:newcomer_reserved_spots],
},
"staff" => {
"staffDelegateIds" => errors[:staff_delegate_ids],
Expand Down Expand Up @@ -2589,6 +2619,7 @@ def self.form_data_to_attributes(form_data)
competitor_limit_enabled: form_data.dig('competitorLimit', 'enabled'),
competitor_limit: form_data.dig('competitorLimit', 'count'),
competitor_limit_reason: form_data.dig('competitorLimit', 'reason'),
newcomer_reserved_spots: form_data.dig('competitorLimit', 'newcomerReservedSpots'),
extra_registration_requirements: form_data.dig('registration', 'extraRequirements'),
on_the_spot_registration: form_data.dig('registration', 'allowOnTheSpot'),
on_the_spot_entry_fee_lowest_denomination: form_data.dig('entryFees', 'onTheSpotEntryFee'),
Expand Down Expand Up @@ -2732,6 +2763,7 @@ def self.form_data_json_schema
"enabled" => { "type" => ["boolean", "null"] },
"count" => { "type" => ["integer", "null"] },
"reason" => { "type" => ["string", "null"] },
"newcomerReservedSpots" => { "type" => ["integer", "null"] },
},
},
"staff" => {
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ def country
Country.find_by_iso2(country_iso2)
end

def newcomer?
person.nil? || wca_id.start_with?(Time.current.year.to_s)
end

def locale
preferred_locale || I18n.default_locale
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function CompetitorLimit() {
<ConditionalSection showIf={hasLimit}>
<InputNumber id="count" min={0} />
<InputTextArea id="reason" />
<InputNumber id="newcomerReservedSpots" min={0} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate and display the max number of newcomer reserved spots allowed per the 50% WCRP exception limit

</ConditionalSection>
</SubSection>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export const REGISTRATION_CLOSED = -4008;
export const ORGANIZER_MUST_CANCEL_REGISTRATION = -4009;
export const INVALID_WAITING_LIST_POSITION = -4010;
export const QUALIFICATION_NOT_MET = -4012;
export const NO_UNRESERVED_SPOTS_REMAINING = -4013;
export const PAYMENT_NOT_ENABLED = -6001;
export const PAYMENT_NOT_READY = -6002;
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ en:
enabled: "Competitor limit"
count: "Maximum number of competitors"
reason: "The reason for the competitor limit"
newcomer_reserved_spots: "Number of spots to reserve for newcomers"
staff:
staff_delegate_ids: "WCA Delegate(s)"
trainee_delegate_ids: "WCA Trainee Delegate(s)"
Expand Down Expand Up @@ -1746,6 +1747,7 @@ en:
enabled: "Although it is not required by the Regulations to provide a competitor limit, it's highly recommended to do so."
count: "The number of competitors allowed in this competition. For now this number is informational only, and does not yet prevent more people from registering. We are working on adding explicit support for this to the registration flow, but it requires some other work first."
reason: "What is the reason for the limit on competitors? Please fill this out in English!"
newcomer_reserved_spots: "Only allowed during Newcomer Month - reserved spots can't be more than half of the Competitor Limit."
staff:
staff_delegate_ids: "WCA Delegates for the competition."
trainee_delegate_ids: "WCA Trainee Delegates for the competition."
Expand Down Expand Up @@ -1947,6 +1949,7 @@ en:
-4010: "The Waiting List Position is not Valid"
-4011: "You can only accept the first person on the waiting list"
-4012: "You are not qualified to compete in the selected events"
-4013: "Cant accept non-newcomer when only reserved newcomer spots remain"
-6001: "WCA Payment is not enabled for this competition"
-6002: "You need to finish your registration before you can pay"
#context: and when an error occured
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddNewcomerReservedSpotsToCompetitions < ActiveRecord::Migration[7.2]
def change
add_column :Competitions, :newcomer_reserved_spots, :integer, default: 0, null: false
end
end
1 change: 1 addition & 0 deletions lib/database_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def self.actions_to_column_sanitizers(columns_by_action)
registration_version
forbid_newcomers
forbid_newcomers_reason
newcomer_reserved_spots
),
db_default: %w(
connected_stripe_account_id
Expand Down
1 change: 1 addition & 0 deletions lib/registrations/error_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module ErrorCodes
ORGANIZER_MUST_CANCEL_REGISTRATION = -4009
INVALID_WAITING_LIST_POSITION = -4010
QUALIFICATION_NOT_MET = -4012
NO_UNRESERVED_SPOTS_REMAINING = -4013

# Payment Errors
PAYMENT_NOT_ENABLED = -6001
Expand Down
16 changes: 13 additions & 3 deletions lib/registrations/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,22 @@ def contains_organizer_fields?(request, organizer_fields)
def validate_update_status!(new_status, competition, current_user, target_user, registration, events)
raise WcaExceptions::RegistrationError.new(:unprocessable_entity, Registrations::ErrorCodes::INVALID_REQUEST_DATA) unless
Registration.competing_statuses.include?(new_status)
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::COMPETITOR_LIMIT_REACHED) if
new_status == Registrations::Helper::STATUS_ACCEPTED && competition.competitor_limit_enabled? &&
competition.registrations.competing_status_accepted.count >= competition.competitor_limit
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if
new_status == Registrations::Helper::STATUS_ACCEPTED && existing_registration_in_series?(competition, target_user)

if new_status == Registrations::Helper::STATUS_ACCEPTED && competition.competitor_limit_enabled?
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::COMPETITOR_LIMIT_REACHED) if
competition.registrations.competing_status_accepted.count >= competition.competitor_limit

if !target_user.newcomer? && competition.newcomer_reserved_spots > 0
available_spots = competition.competitor_limit - competition.registrations.competing_status_accepted.count
newcomer_reserved_spots_remaining = competition.newcomer_reserved_spots - competition.newcomers_competing.count

raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::NO_UNRESERVED_SPOTS_REMAINING) if
(available_spots <= newcomer_reserved_spots_remaining) && competition.registration_currently_open?
end
end

# Otherwise, organizers can make any status change they want to
return if current_user.can_manage_competition?(competition)

Expand Down
9 changes: 9 additions & 0 deletions spec/factories/competitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,17 @@
refund_policy_percent { 0 }
guests_entry_fee_lowest_denomination { 0 }

# TODO: This should change to :v3
registration_version { :v1 }

trait :newcomer_month do
registration_open
with_organizer
with_competitor_limit
competitor_limit { 4 }
newcomer_reserved_spots { 2 }
end

trait :enforces_qualifications do
with_organizer
qualification_results { true }
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/persons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

FactoryBot.define do
factory :person do
transient do
wca_id_year { "2016" }
end

wca_id do
mid = ('A'..'Z').to_a.sample(4).join
id = "2016#{mid}01"
id = "#{wca_id_year}#{mid}01"
id = id.next while Person.exists?(wca_id: id)
id
end
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/registrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@
competing_status { Registrations::Helper::STATUS_WAITING_LIST }
end

trait :newcomer do
trait :first_timer do
association :user, factory: [:user]
end

trait :newcomer do
association :user, factory: [:user, :current_year_wca_id]
end

trait :paid do
after(:create) do |registration|
FactoryBot.create :registration_payment, registration: registration, user: registration.user,
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@
end
end

trait :current_year_wca_id do
transient do
person {
FactoryBot.create(
:person, name: name, countryId: Country.find_by_iso2(country_iso2).id, gender: gender, dob: dob.strftime("%F"), wca_id_year: Time.current.year.to_s
)
}
end
end

trait :with_2fa do
otp_required_for_login { true }
otp_secret { User.generate_otp_secret }
Expand Down
Loading