Updated

app/controllers/accounts/contacts / chatwoot_embed_controller.rb

D
67 lines of codes
8 methods
11.3 complexity/method
14 churn
90.57 complexity
78 duplications
class Accounts::Contacts::ChatwootEmbedController < InternalController
  1. Accounts::Contacts::ChatwootEmbedController assumes too much for instance variable '@contact'
  2. Accounts::Contacts::ChatwootEmbedController has no descriptive comment
layout 'embed' before_action :set_contact, only: %i[show] def search
  1. Accounts::Contacts::ChatwootEmbedController#search has a flog score of 25
contact = contact_search if contact.present? redirect_to account_chatwoot_embed_path(current_user.account, contact)
  1. Accounts::Contacts::ChatwootEmbedController#search calls 'current_user.account' 2 times Locations: 0 1
else chatwoot_contact = JSON.parse(params['chatwoot_contact']) @contact = current_user.account.contacts.new({
  1. Identical code found in 2 nodes Locations: 0 1
  2. Accounts::Contacts::ChatwootEmbedController#search calls 'current_user.account' 2 times Locations: 0 1
full_name: chatwoot_contact['name'], email: chatwoot_contact['email'], phone: chatwoot_contact['phone_number'], additional_attributes: { 'chatwoot_id': chatwoot_contact['id'] } }) render :new end end def show; end def new chatwoot_contact = JSON.parse(params['chatwoot_contact']) @contact = current_user.account.contacts.new({
  1. Identical code found in 2 nodes Locations: 0 1
full_name: chatwoot_contact['name'],
  1. Accounts::Contacts::ChatwootEmbedController#new refers to 'chatwoot_contact' more than self (maybe move it to another class?) Locations: 0 1 2 3
email: chatwoot_contact['email'],
  1. Accounts::Contacts::ChatwootEmbedController#new refers to 'chatwoot_contact' more than self (maybe move it to another class?) Locations: 0 1 2 3
phone: chatwoot_contact['phone_number'],
  1. Accounts::Contacts::ChatwootEmbedController#new refers to 'chatwoot_contact' more than self (maybe move it to another class?) Locations: 0 1 2 3
additional_attributes: { 'chatwoot_id': chatwoot_contact['id'] }
  1. Accounts::Contacts::ChatwootEmbedController#new refers to 'chatwoot_contact' more than self (maybe move it to another class?) Locations: 0 1 2 3
}) end def create
  1. Similar code found in 2 nodes Locations: 0 1
@contact = current_user.account.contacts.new(contact_params)
  1. Accounts::Contacts::ChatwootEmbedController#create calls 'current_user.account' 2 times Locations: 0 1
if @contact.save redirect_to account_chatwoot_embed_path(current_user.account, @contact),
  1. Accounts::Contacts::ChatwootEmbedController#create calls 'current_user.account' 2 times Locations: 0 1
notice: t('flash_messages.created', model: Contact.model_name.human) else render :new, status: :unprocessable_entity end end private def set_contact @contact = Contact.find(params[:id]) end def contact_params params.require(:contact).permit(:full_name, :phone, :email, additional_attributes: {}) end def chatwoot_contact @chatwoot_contact ||= JSON.parse(params['chatwoot_contact']) end def contact_search result = current_user.account.contacts.by_chatwoot_id(chatwoot_contact['id']).first
  1. Accounts::Contacts::ChatwootEmbedController#contact_search calls 'current_user.account' 2 times Locations: 0 1
return result if result.present? Accounts::Contacts::GetByParams.call(current_user.account,
  1. Accounts::Contacts::ChatwootEmbedController#contact_search calls 'current_user.account' 2 times Locations: 0 1
{ email: chatwoot_contact['email'], phone: chatwoot_contact['phone_number'] })[:ok] end end