Skip to content

Commit

Permalink
Give more clear error messages when DOBs cannot be parsed (#9679)
Browse files Browse the repository at this point in the history
* Add error message about the parsing format of DOB strings in registration bulk upload

* Add test case for unparseable DOBs
  • Loading branch information
gregorbg authored Jul 19, 2024
1 parent 165df17 commit 94f7762
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def do_import
if wca_id_duplicates.any?
raise I18n.t("registrations.import.errors.wca_id_duplicates", wca_ids: wca_id_duplicates.join(", "))
end
raw_dobs = registration_rows.map { |registration_row| registration_row[:birth_date] }
wrong_format_dobs = raw_dobs.select { |raw_dob| Date.safe_parse(raw_dob)&.to_fs != raw_dob }
if wrong_format_dobs.any?
raise I18n.t("registrations.import.errors.wrong_dob_format", raw_dobs: wrong_format_dobs.join(", "))
end
new_locked_users = []
ActiveRecord::Base.transaction do
competition.registrations.accepted.each do |registration|
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ en:
email_user_with_different_unconfirmed_wca_id: "There is already a user with email %{email}, but it has unconfirmed WCA ID of %{unconfirmed_wca_id} instead of %{registration_wca_id}."
email_duplicates: "Email must be unique, found the following duplicates: %{emails}."
wca_id_duplicates: "WCA ID must be unique, found the following duplicates: %{wca_ids}."
wrong_dob_format: "Birthdate must follow the YYYY-mm-dd format (year-month-day, for example 1944-07-13), found the following dates which cannot be parsed: %{raw_dobs}."
add:
title: "Add registration for %{comp}"
info: "This will create an approved registration. Use this form to add walk-ins during the competition."
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/registrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
expect(response.body).to include "WCA ID must be unique, found the following duplicates: 2019HOLM01."
end

it "renders an error when there are invalid DOBs" do
file = csv_file [
["Status", "Name", "Country", "WCA ID", "Birth date", "Gender", "Email", "333", "444"],
["a", "Sherlock Holmes", "United Kingdom", "2019HOLM01", "01.01.2000", "m", "[email protected]", "1", "0"],
["a", "John Watson", "United Kingdom", "2019WATS01", "2000-01-01", "m", "[email protected]", "1", "1"],
["a", "James Moriarty", "United Kingdom", "2019MORI01", "Jan 01 2000", "m", "[email protected]", "0", "1"],
]
expect {
post competition_registrations_do_import_path(competition), params: { registrations_import: { registrations_file: file } }
}.to_not change { competition.registrations.count }
follow_redirect!
expect(response.body).to include "Birthdate must follow the YYYY-mm-dd format (year-month-day, for example 1944-07-13), found the following dates which cannot be parsed: 01.01.2000, Jan 01 2000."
end

describe "registrations import" do
context "registrant has WCA ID" do
it "renders an error if the WCA ID doesn't exist" do
Expand Down

0 comments on commit 94f7762

Please sign in to comment.