Updated

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

C
85 lines of codes
7 methods
15.4 complexity/method
17 churn
107.8 complexity
60 duplications
class Accounts::Apps::Chatwoots::ExportContact
  1. Accounts::Apps::Chatwoots::ExportContact has no descriptive comment
def self.call(chatwoot, contact) create_or_update_contact(chatwoot, contact) end def self.create_or_update_contact(chatwoot, contact) contact_chatwoot_id = contact['additional_attributes']['chatwoot_id'] if contact_chatwoot_id.present? update_contact(chatwoot, contact) else create_contact(chatwoot, contact) end end def self.update_contact(chatwoot, contact) request = Faraday.put( "#{chatwoot.chatwoot_endpoint_url}/api/v1/accounts/#{chatwoot.chatwoot_account_id}/contacts/#{contact.additional_attributes['chatwoot_id']}", build_body(contact), chatwoot.request_headers ) if request.status == 200 export_contact_tags(chatwoot, contact) { ok: contact } else { error: request.body } end end def self.export_contact_tags(chatwoot, contact) request = Faraday.post( "#{chatwoot.chatwoot_endpoint_url}/api/v1/accounts/#{chatwoot.chatwoot_account_id}/contacts/#{contact.additional_attributes['chatwoot_id']}/labels", { labels: contact.label_list }.to_json, chatwoot.request_headers ) JSON.parse(request.body)['payload'] end def self.create_contact(chatwoot, contact)
  1. Accounts::Apps::Chatwoots::ExportContact::create_contact has a flog score of 65
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact has approx 13 statements
request = Faraday.post(
  1. Similar code found in 2 nodes Locations: 0 1
"#{chatwoot.chatwoot_endpoint_url}/api/v1/accounts/#{chatwoot.chatwoot_account_id}/contacts", build_body(contact), chatwoot.request_headers ) response_body = JSON.parse(request.body)
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.body' 3 times Locations: 0 1 2
if response_body['message'] == 'Email has already been taken' && request.status == 422
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.status == 422' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.status' 3 times Locations: 0 1 2
  3. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['message']' 2 times Locations: 0 1
search_chatwoot_contact = Accounts::Apps::Chatwoots::SearchContact.call(chatwoot, contact['email'])
  1. Similar code found in 2 nodes Locations: 0 1
update_contact_chatwoot_id_and_identifier(contact, search_chatwoot_contact['id'],
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'search_chatwoot_contact['id']' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'update_contact_chatwoot_id_and_identifier(contact, search_chatwoot_contact['id'],; search_chatwoot_contact['identifier'])' 2 times Locations: 0 1
search_chatwoot_contact['identifier'])
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'search_chatwoot_contact['identifier']' 2 times Locations: 0 1
{ ok: contact } elsif response_body['message'] == 'Phone number has already been taken' && request.status == 422
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.status == 422' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.status' 3 times Locations: 0 1 2
  3. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['message']' 2 times Locations: 0 1
search_chatwoot_contact = Accounts::Apps::Chatwoots::SearchContact.call(chatwoot, contact['phone'])
  1. Similar code found in 2 nodes Locations: 0 1
update_contact_chatwoot_id_and_identifier(contact, search_chatwoot_contact['id'],
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'search_chatwoot_contact['id']' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'update_contact_chatwoot_id_and_identifier(contact, search_chatwoot_contact['id'],; search_chatwoot_contact['identifier'])' 2 times Locations: 0 1
search_chatwoot_contact['identifier'])
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'search_chatwoot_contact['identifier']' 2 times Locations: 0 1
{ ok: contact } elsif request.status == 200
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.status' 3 times Locations: 0 1 2
update_contact_chatwoot_id_and_identifier(contact, response_body['payload']['contact']['id'],
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['payload']' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['payload']['contact']' 2 times Locations: 0 1
response_body['payload']['contact']['identifier'])
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['payload']' 2 times Locations: 0 1
  2. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'response_body['payload']['contact']' 2 times Locations: 0 1
export_contact_tags(chatwoot, contact) { ok: contact } else Rails.logger.error( 'Error when export contact to chatwoot,' + "Chatwoot Apps: #{chatwoot.inspect}," + "Chatwoot request: #{request.inspect}," + "Chatwoot response: #{request.body}"
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.body' 3 times Locations: 0 1 2
) { error: request.body }
  1. Accounts::Apps::Chatwoots::ExportContact#self.create_contact calls 'request.body' 3 times Locations: 0 1 2
end end def self.update_contact_chatwoot_id_and_identifier(contact, chatwoot_id, chatwoot_identifier) contact.update(additional_attributes: contact['additional_attributes'].merge({ chatwoot_id: chatwoot_id, chatwoot_identifier: chatwoot_identifier })) end def self.build_body(contact) { "name": contact['full_name'], "email": contact['email'], "phone_number": contact['phone'], "custom_attributes": contact['custom_attributes'] }.to_json end end