Skip to content
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
14 changes: 12 additions & 2 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,26 @@ def find_duplicate_people(first_name, last_name, email)
normalized_last = NicknameMap.normalize(last_name)
normalized_first = NicknameMap.normalize(first_name)

# Match the typed first name (and its nickname variants) against either the
# stored first_name or legal_first_name, so a returning registrant whose legal
# name lives in legal_first_name (nickname in first_name) is still surfaced when
# they type their legal name. Mirrors the registration matcher in
# EventRegistrationServices::PublicRegistration#find_matching_person.
name_matches = Person.includes(:user)
.where("REPLACE(REPLACE(LOWER(last_name), '.', ''), ' ', '') = ?", normalized_last)
.where("REPLACE(REPLACE(LOWER(first_name), '.', ''), ' ', '') IN (?)", first_variants)
.where(
"REPLACE(REPLACE(LOWER(first_name), '.', ''), ' ', '') IN (:names) " \
"OR REPLACE(REPLACE(LOWER(COALESCE(legal_first_name, '')), '.', ''), ' ', '') IN (:names)",
names: first_variants
)
.limit(10)

name_matches.each do |person|
next if duplicate_ids.include?(person.id)

duplicate_ids.add(person.id)
exact_name = NicknameMap.normalize(person.first_name) == normalized_first
exact_name = NicknameMap.normalize(person.first_name) == normalized_first ||
NicknameMap.normalize(person.legal_first_name) == normalized_first

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

πŸ€– From Claude: A legal-name hit counts as an exact name match ("name match" badge + name-priority sort), matching how a first_name hit is treated. normalized_first is never blank here (guarded by if first_name.presence), so a nil/blank legal_first_name normalizes to "" and can't false-match.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

πŸ€– From Claude: A legal-name hit counts as an exact name match (name-match badge + name-priority sort), matching how a first_name hit is treated. normalized_first is never blank here (guarded by if first_name.presence), so a nil/blank legal_first_name normalizes to an empty string and can't false-match.

duplicates << format_duplicate(person, exact: exact_name, entered_email: email)
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/requests/people_check_duplicates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@
end
end

# --- Legal first name matching ---

context "when the entered first name matches a stored legal first name" do
before do
create(:person, first_name: "Sunny", legal_first_name: "Yuki", last_name: "Tanaka")
end

it "matches a returning registrant who types their legal name" do
get check_duplicates_people_path, params: { first_name: "Yuki", last_name: "Tanaka", email: "" }

expect(response).to have_http_status(:ok)
expect(response.body).to include("Sunny Tanaka")
expect(response.body).to include("name match")
end

it "matches the legal name through period normalization" do
create(:person, first_name: "Beanie", legal_first_name: "J.J.", last_name: "Park")

get check_duplicates_people_path, params: { first_name: "JJ", last_name: "Park", email: "" }

expect(response).to have_http_status(:ok)
expect(response.body).to include("Beanie Park")
expect(response.body).to include("name match")
end

it "does not match when neither first nor legal name matches" do
get check_duplicates_people_path, params: { first_name: "Kenji", last_name: "Tanaka", email: "" }

expect(response).to have_http_status(:ok)
expect(response.body).not_to include("Sunny Tanaka")
end
end

# --- Period and space normalization ---

context "when names contain periods or extra spaces" do
Expand Down