Updated

app/models/contact/migrations / merge_duplicate_contacts_job.rb

B
51 lines of codes
5 methods
9.9 complexity/method
5 churn
49.39 complexity
54 duplications
class Contact::Migrations::MergeDuplicateContactsJob < ApplicationJob
  1. Contact::Migrations::MergeDuplicateContactsJob has no descriptive comment
self.queue_adapter = :good_job def perform phone_groups = group_duplicate_contacts_by_phone merge_process(phone_groups) if phone_groups.present? email_groups = group_duplicate_contacts_by_email merge_process(email_groups) if email_groups.present? end private def group_duplicate_contacts_by_email
  1. Similar code found in 2 nodes Locations: 0 1
  2. Contact::Migrations::MergeDuplicateContactsJob#group_duplicate_contacts_by_email doesn't depend on instance state (maybe move it to another class?)
Contact.where.not(email: [nil, '']) .group(:email) .having('COUNT(*) > 1') .pluck(:email) .map { |email| Contact.where(email:).order(:id).pluck(:id) } end def group_duplicate_contacts_by_phone
  1. Similar code found in 2 nodes Locations: 0 1
  2. Contact::Migrations::MergeDuplicateContactsJob#group_duplicate_contacts_by_phone doesn't depend on instance state (maybe move it to another class?)
Contact.where.not(phone: [nil, '']) .group(:phone) .having('COUNT(*) > 1') .pluck(:phone) .map { |phone| Contact.where(phone:).order(:id).pluck(:id) } end def merge_process(contact_groups) contact_groups.each do |contact_ids| next if contact_ids.size < 2 merge_group(contact_ids) end end def merge_group(contact_ids)
  1. Contact::Migrations::MergeDuplicateContactsJob#merge_group has approx 6 statements
  2. Contact::Migrations::MergeDuplicateContactsJob#merge_group doesn't depend on instance state (maybe move it to another class?)
contacts = Contact.where(id: contact_ids).order(:id).to_a return if contacts.size < 2 base_contact = contacts.shift base_contact.skip_validation = true contacts.each do |mergee_contact| Contact::Merge.new(base_contact:, mergee_contact:).perform end end end