-
Notifications
You must be signed in to change notification settings - Fork 27
Add a registration deduper + fix polymorphic reassignment in the deduper engine #2467
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86fb9d3
Add a registration deduper on the shared Dedupable pattern
maebeale e996fea
Type-scope polymorphic reassignment in ModelDeduper
maebeale 39d54d3
Surface the registration deduper from an event's bulk actions
maebeale 938a93a
Re-credit a moved scholarship to the kept registrant on merge
maebeale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
app/services/event_registration_services/duplicate_finder.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| require "set" | ||
|
|
||
| module EventRegistrationServices | ||
| # Surfaces likely-duplicate registrations for the deduper's candidate list. The | ||
| # (registrant_id, event_id) unique index already forbids one person registering | ||
| # for the same event twice, so a duplicate is one real person registered for the | ||
| # same event under two different Person records. Clusters registrations for the | ||
| # same event whose registrants share a signal β nickname/legal-name-aware name, | ||
| # email (or email_2), or FileMaker code β then annotates each cluster with why it | ||
| # was flagged. Registrants grouped by name but carrying different FileMaker codes | ||
| # are surfaced as a caution to verify, never silently treated as a clean merge. | ||
| class DuplicateFinder | ||
| Group = Struct.new(:key, :label, :records, :reasons, keyword_init: true) | ||
|
|
||
| def initialize(scope = EventRegistration.all) | ||
| @registrations = scope.includes(:registrant, :event).to_a | ||
| end | ||
|
|
||
| def groups | ||
| union = UnionFind.new(@registrations.map(&:id)) | ||
| cluster(union) { |registration| name_keys(registration) } | ||
| cluster(union) { |registration| email_keys(registration) } | ||
| cluster(union) { |registration| Array(filemaker_key(registration)) } | ||
|
|
||
| by_id = @registrations.index_by(&:id) | ||
| union.components.filter_map do |ids| | ||
| next if ids.size < 2 | ||
| records = ids.map { |id| by_id[id] }.sort_by(&:id) | ||
| Group.new( | ||
| key: records.map(&:id).join("-"), | ||
| label: label_for(records), | ||
| records: records, | ||
| reasons: reasons_for(records) | ||
| ) | ||
| end.sort_by { |group| group.label.to_s.downcase } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def cluster(union) | ||
| buckets = Hash.new { |hash, key| hash[key] = [] } | ||
| @registrations.each do |registration| | ||
| yield(registration).each { |key| buckets[key] << registration.id } | ||
| end | ||
| buckets.each_value { |ids| union.union_all(ids) if ids.size > 1 } | ||
| end | ||
|
|
||
| def label_for(records) | ||
| registrant = records.first.registrant | ||
| "#{registrant&.full_name} β #{records.first.event&.title}" | ||
| end | ||
|
|
||
| def reasons_for(records) | ||
| reasons = [] | ||
| reasons << "Same registrant name" if shared?(records) { |registration| name_keys(registration) } | ||
| reasons << "Shared registrant email" if shared?(records) { |registration| email_keys(registration) } | ||
| reasons.concat(filemaker_reasons(records)) | ||
| reasons | ||
| end | ||
|
|
||
| def filemaker_reasons(records) | ||
| codes = records.filter_map { |registration| registration.registrant&.filemaker_code.presence }.uniq.sort | ||
| return [] if codes.empty? | ||
| return [ "β Different FileMaker codes (#{codes.join(", ")}) β verify these are the same person" ] if codes.size > 1 | ||
|
|
||
| [ "Same registrant FileMaker code (#{codes.first})" ] | ||
| end | ||
|
|
||
| def shared?(records) | ||
| keys = records.flat_map { |registration| Array(yield(registration)) } | ||
| keys.tally.any? { |_key, count| count > 1 } | ||
| end | ||
|
|
||
| # Only registrations for the same event can be duplicates, so every signal is | ||
| # scoped by event_id β the same person on two different events is legitimate. | ||
| # First-name variant (nickname or legal first name) paired with the last name, | ||
| # so "Bob Smith" and "Robert Smith" on one event cluster together. | ||
| def name_keys(registration) | ||
| registrant = registration.registrant | ||
| last = normalized(registrant&.last_name) | ||
| return [] if last.blank? | ||
|
|
||
| first_forms = [ registrant&.first_name, registrant&.legal_first_name ].filter_map { |name| name.presence } | ||
| first_forms | ||
| .flat_map { |name| NicknameMap.variants_for(name) } | ||
| .uniq | ||
| .map { |variant| "#{registration.event_id}|#{variant}|#{last}" } | ||
| end | ||
|
|
||
| def email_keys(registration) | ||
| registrant = registration.registrant | ||
| [ registrant&.email, registrant&.email_2 ] | ||
| .filter_map { |email| email.to_s.strip.downcase.presence } | ||
| .map { |email| "#{registration.event_id}|#{email}" } | ||
| end | ||
|
|
||
| def filemaker_key(registration) | ||
| code = registration.registrant&.filemaker_code.to_s.strip.downcase | ||
| "#{registration.event_id}|fm|#{code}" if code.present? | ||
| end | ||
|
|
||
| def normalized(value) | ||
| NicknameMap.normalize(value) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π€ From Claude: Load-bearing behavior change for all five deduper models, not just registrations: polymorphic
has_many β¦ as:children (allocations, comments, bookmarks) were previously reassigned by FK id alone. This adds the*_typescope so a merge can only touch this model's own rows β worth a close look since it changes existing people/org/workshop merges too.