diff --git a/.gitignore b/.gitignore index b646284b..ea505e36 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ Gemfile.lock # reports spec/coverage +coverage spec/reports brakeman-output.tabs rubocop-results.xml diff --git a/app/domain/insieme/import/person_duplicate_finder.rb b/app/domain/insieme/import/person_duplicate_finder.rb index e4d0bb41..d009ed3f 100644 --- a/app/domain/insieme/import/person_duplicate_finder.rb +++ b/app/domain/insieme/import/person_duplicate_finder.rb @@ -10,23 +10,25 @@ module Import module PersonDuplicateFinder private - def duplicates(attrs) + def duplicate_ids_with_first_person(attrs) if attrs[:number].present? - ::Person.where(number: attrs[:number]).to_a.presence || - check_duplicate_with_different_number(attrs, super) + people_ids = ::Person.where(number: attrs[:number]).pluck(:id).presence + return {people_ids:, first_person: find_first_person(people_ids)} if people_ids + check_duplicate_with_different_number(attrs, super) else super end end - def check_duplicate_with_different_number(attrs, duplicates) - if duplicates.present? - person = duplicates.first - add_duplicate_with_different_number_error(person) if person.number? + def check_duplicate_with_different_number(attrs, duplicate_ids_with_first_person) + duplicate_ids = duplicate_ids_with_first_person[:people_ids] + first_person = duplicate_ids_with_first_person[:first_person] + if duplicate_ids.present? + add_duplicate_with_different_number_error(first_person) if first_person.number? else attrs[:manual_number] = true end - duplicates + duplicate_ids_with_first_person end def add_duplicate_with_different_number_error(person)