Skip to content

Commit

Permalink
fix: Refactored execution service
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaswinichile committed Jul 22, 2022
1 parent 2f1cc5d commit 1fceae6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 117 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/macros_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def update
end

def execute
::MacrosExecutionJob.perform_later(@macro, conversation_ids: params[:conversation_ids], user: current_user)
::MacrosExecutionJob.perform_later(@macro, conversation_ids: params[:conversation_ids], user: Current.user)

head :ok
end
Expand Down
57 changes: 57 additions & 0 deletions app/services/action_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class ActionService
def initialize(conversation)
@conversation = conversation
end

def mute_conversation(_params)
@conversation.mute!
end

def snooze_conversation(_params)
@conversation.snoozed!
end

def resolve_conversation(_params)
@conversation.resolved!
end

def change_status(status)
@conversation.update!(status: status[0])
end

def add_label(labels)
return if labels.empty?

@conversation.add_labels(labels)
end

def assign_best_agent(agent_ids = [])
return unless agent_belongs_to_account?(agent_ids)

@agent = @account.users.find_by(id: agent_ids)

@conversation.update!(assignee_id: @agent.id) if @agent.present?
end

def assign_team(team_ids = [])
return unless team_belongs_to_account?(team_ids)

@conversation.update!(team_id: team_ids[0])
end

private

def agent_belongs_to_account?(agent_ids)
@account.agents.pluck(:id).include?(agent_ids[0])
end

def team_belongs_to_account?(team_ids)
@account.team_ids.include?(team_ids[0])
end

def conversation_a_tweet?
return false if @conversation.additional_attributes.blank?

@conversation.additional_attributes['type'] == 'tweet'
end
end
54 changes: 2 additions & 52 deletions app/services/automation_rules/action_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class AutomationRules::ActionService
class AutomationRules::ActionService < ActionService
def initialize(rule, account, conversation)
super(conversation)
@rule = rule
@account = account
@conversation = conversation
Current.executed_by = rule
end

Expand Down Expand Up @@ -41,22 +41,6 @@ def send_email_transcript(emails)
end
end

def mute_conversation(_params)
@conversation.mute!
end

def snooze_conversation(_params)
@conversation.snoozed!
end

def resolve_conversation(_params)
@conversation.resolved!
end

def change_status(status)
@conversation.update!(status: status[0])
end

def send_webhook_event(webhook_url)
payload = @conversation.webhook_data.merge(event: "automation_event.#{@rule.event_name}")
WebhookJob.perform_later(webhook_url[0], payload)
Expand All @@ -70,45 +54,11 @@ def send_message(message)
mb.perform
end

def assign_team(team_ids = [])
return unless team_belongs_to_account?(team_ids)

@conversation.update!(team_id: team_ids[0])
end

def assign_best_agent(agent_ids = [])
return unless agent_belongs_to_account?(agent_ids)

@agent = @account.users.find_by(id: agent_ids)

@conversation.update!(assignee_id: @agent.id) if @agent.present?
end

def add_label(labels)
return if labels.empty?

@conversation.add_labels(labels)
end

def send_email_to_team(params)
teams = Team.where(id: params[0][:team_ids])

teams.each do |team|
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[0][:message])&.deliver_now
end
end

def agent_belongs_to_account?(agent_ids)
@account.agents.pluck(:id).include?(agent_ids[0])
end

def team_belongs_to_account?(team_ids)
@account.team_ids.include?(team_ids[0])
end

def conversation_a_tweet?
return false if @conversation.additional_attributes.blank?

@conversation.additional_attributes['type'] == 'tweet'
end
end
56 changes: 3 additions & 53 deletions app/services/macros/execution_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Macros::ExecutionService
class Macros::ExecutionService < ActionService
def initialize(macro, conversation, user)
super(conversation)
@macro = macro
@account = macro.account
@conversation = conversation
Current.user = user
end

Expand All @@ -21,22 +21,6 @@ def perform

private

def mute_conversation(_params)
@conversation.mute!
end

def snooze_conversation(_params)
@conversation.snoozed!
end

def resolve_conversation(_params)
@conversation.resolved!
end

def change_status(status)
@conversation.update!(status: status[0])
end

def send_webhook_event(webhook_url)
payload = @conversation.webhook_data.merge(event: "macro_event.#{@macro.name}")
WebhookJob.perform_later(webhook_url[0], payload)
Expand All @@ -45,44 +29,10 @@ def send_webhook_event(webhook_url)
def send_message(message)
return if conversation_a_tweet?

params = { content: message[0], private: false, content_attributes: { automation_rule_id: @macro.id } }
params = { content: message[0], private: false, content_attributes: { macro_id: @macro.id } }
mb = Messages::MessageBuilder.new(nil, @conversation, params)
mb.perform
end

def assign_team(team_ids = [])
return unless team_belongs_to_account?(team_ids)

@conversation.update!(team_id: team_ids[0])
end

def assign_best_agent(agent_ids = [])
return unless agent_belongs_to_account?(agent_ids)

@agent = @account.users.find_by(id: agent_ids)

@conversation.update!(assignee_id: @agent.id) if @agent.present?
end

def add_label(labels)
return if labels.empty?

@conversation.add_labels(labels)
end

def send_email_to_team(_params); end

def agent_belongs_to_account?(agent_ids)
@account.agents.pluck(:id).include?(agent_ids[0])
end

def team_belongs_to_account?(team_ids)
@account.team_ids.include?(team_ids[0])
end

def conversation_a_tweet?
return false if @conversation.additional_attributes.blank?

@conversation.additional_attributes['type'] == 'tweet'
end
end
18 changes: 7 additions & 11 deletions spec/controllers/api/v1/accounts/macros_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
let!(:macro) { create(:macro, account: account, created_by: administrator, updated_by: administrator) }
let(:inbox) { create(:inbox, account: account) }
let(:contact) { create(:contact, account: account, identifier: '123') }
let(:conversation) { create(:conversation, inbox: inbox, account: account) }
let(:conversation) { create(:conversation, inbox: inbox, account: account, status: :open) }
let(:team) { create(:team, account: account) }
let(:user_1) { create(:user, role: 0) }

Expand All @@ -197,9 +197,8 @@
},
{ 'action_name' => 'assign_team', 'action_params' => [team.id] },
{ 'action_name' => 'add_label', 'action_params' => %w[support priority_customer] },
{ 'action_name' => 'snooze_conversation' },
{ 'action_name' => 'assign_best_agent', 'action_params' => [user_1.id] },
{ 'action_name' => 'mute_conversation', 'action_params' => nil },
{ 'action_name' => 'change_status', 'action_params' => ['snoozed'] },
{ 'action_name' => 'send_message', 'action_params' => ['Send this message.'] }
])
end
Expand All @@ -214,23 +213,20 @@

context 'when it is an authenticated user' do
it 'execute the macro' do
expect(conversation.status).to eql('open')
expect(conversation.messages).to be_empty
expect(conversation.assignee).to be_nil
expect(conversation.labels).to be_empty

perform_enqueued_jobs do
post "/api/v1/accounts/#{account.id}/macros/#{macro.id}/execute",
params: { conversation_ids: [conversation.display_id] },
headers: administrator.create_new_auth_token

expect(response).to have_http_status(:success)
end

conversation.reload

expect(conversation.status).to eql('snoozed')
expect(conversation.messages.last.content).to eq('Send this message.')
expect(conversation.assignee).to eq(user_1)
expect(conversation.reload.status).to eql('snoozed')
expect(conversation.messages.chat.last.content).to eq('Send this message.')
expect(conversation.label_list).to match_array(%w[support priority_customer])
expect(conversation.messages.activity.last.content).to eq("Assigned to #{user_1.name} by #{administrator.name}")
end
end
end
Expand Down

0 comments on commit 1fceae6

Please sign in to comment.