Updated

app/use_cases/accounts/apps/chatwoots / sync_import_contacts.rb

B
74 lines of codes
6 methods
15.6 complexity/method
13 churn
93.81 complexity
0 duplications
class Accounts::Apps::Chatwoots::SyncImportContacts
  1. Accounts::Apps::Chatwoots::SyncImportContacts has no descriptive comment
def initialize(chatwoot) @chatwoot = chatwoot @account = @chatwoot.account end def call response = update_or_create_contact { ok: response } end def update_or_create_contact
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact has a flog score of 69
  2. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact has approx 22 statements
contacts_imported = 0 contacts_failed = 0 contacts_updated = 0 quantity_per_page = 1 page = 0 until quantity_per_page.zero? page += 1 request = Faraday.get( "#{@chatwoot.chatwoot_endpoint_url}/api/v1/accounts/#{@chatwoot.chatwoot_account_id}/contacts/", { page: page }, @chatwoot.request_headers ) if request.status == 200 chatwoot_contacts = JSON.parse(request.body)['payload']
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'request.body' 2 times Locations: 0 1
quantity_per_page = chatwoot_contacts.count chatwoot_contacts.each do |chatwoot_contact| contact = Accounts::Contacts::GetByParams.call(@account, chatwoot_contact.slice('email', 'phone', 'identifier').transform_values(&:to_s)) if contact[:ok]
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'contact[:ok]' 4 times Locations: 0 1 2 3
update_contact_chatwoot_id(contact[:ok], chatwoot_contact['id'])
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'contact[:ok]' 4 times Locations: 0 1 2 3
import_labels(contact[:ok])
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'contact[:ok]' 4 times Locations: 0 1 2 3
contact[:ok].save
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'contact[:ok]' 4 times Locations: 0 1 2 3
contacts_updated += 1 else contact = build_contact_att(chatwoot_contact) import_labels(contact) if contact.save contacts_imported += 1 else contacts_failed += 1 Rails.logger.error("Error import contact from chatwoot #{contact.errors.inspect}, chatwoot: #{@chatwoot.inspect}") end end end else return { error: request.body }
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_or_create_contact calls 'request.body' 2 times Locations: 0 1
end end "Contacts imported #{contacts_imported} / Contacts updated #{contacts_updated} / Contacts failed #{contacts_failed}" end def update_contact_chatwoot_id(contact, chatwoot_id)
  1. Accounts::Apps::Chatwoots::SyncImportContacts#update_contact_chatwoot_id doesn't depend on instance state (maybe move it to another class?)
contact.additional_attributes.merge!({ 'chatwoot_id' => chatwoot_id }) end def import_labels(contact) Accounts::Apps::Chatwoots::Webhooks::ImportContact.import_contact_tags(@chatwoot, contact) Accounts::Apps::Chatwoots::Webhooks::ImportContact.import_contact_converstions_tags(@chatwoot, contact) end def build_contact_att(body) contact = @account.contacts.new( full_name: body['name'],
  1. Accounts::Apps::Chatwoots::SyncImportContacts#build_contact_att refers to 'body' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
email: (body['email']).to_s,
  1. Accounts::Apps::Chatwoots::SyncImportContacts#build_contact_att refers to 'body' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
phone: (body['phone_number']).to_s
  1. Accounts::Apps::Chatwoots::SyncImportContacts#build_contact_att refers to 'body' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
) contact.additional_attributes.merge!({ 'chatwoot_id' => body['id'], 'chatwoot_identifier' => body['identifier'] })
  1. Accounts::Apps::Chatwoots::SyncImportContacts#build_contact_att refers to 'body' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
contact.custom_attributes.merge!(body['custom_attributes'])
  1. Accounts::Apps::Chatwoots::SyncImportContacts#build_contact_att refers to 'body' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
contact end end