feat: Save automation rules (#3359)
This commit is contained in:
63
app/services/automation_rules/action_service.rb
Normal file
63
app/services/automation_rules/action_service.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
class AutomationRules::ActionService
|
||||
def initialize(rule, conversation)
|
||||
@rule = rule
|
||||
@conversation = conversation
|
||||
@account = @conversation.account
|
||||
end
|
||||
|
||||
def perform
|
||||
@rule.actions.each do |action, _current_index|
|
||||
action = action.with_indifferent_access
|
||||
send(action[:action_name], action[:action_params])
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_message(message)
|
||||
# params = { content: message, private: false }
|
||||
# mb = Messages::MessageBuilder.new(@administrator, @conversation, params)
|
||||
# mb.perform
|
||||
end
|
||||
|
||||
def assign_team(team_ids = [])
|
||||
return unless team_belongs_to_account?(team_ids)
|
||||
|
||||
@account.teams.find_by(id: team_ids)
|
||||
@conversation.update!(team_id: team_ids[0])
|
||||
end
|
||||
|
||||
def assign_best_agents(agent_ids = [])
|
||||
return unless agent_belongs_to_account?(agent_ids)
|
||||
|
||||
@agent = @account.users.find_by(id: agent_ids)
|
||||
@conversation.update_assignee(@agent)
|
||||
end
|
||||
|
||||
def add_label(labels = [])
|
||||
@conversation.add_labels(labels)
|
||||
end
|
||||
|
||||
def send_email_to_team(params)
|
||||
team = Team.find(params[:team_ids][0])
|
||||
|
||||
case @rule.event_name
|
||||
when 'conversation_created', 'conversation_status_changed'
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[:message])
|
||||
when 'conversation_updated'
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_updated(@conversation, team, params[:message])
|
||||
end
|
||||
end
|
||||
|
||||
def administrator
|
||||
@administrator ||= @account.administrators.first
|
||||
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
|
||||
end
|
||||
45
app/services/automation_rules/conditions_filter_service.rb
Normal file
45
app/services/automation_rules/conditions_filter_service.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require 'json'
|
||||
|
||||
class AutomationRules::ConditionsFilterService < FilterService
|
||||
def initialize(rule, conversation)
|
||||
super([], nil)
|
||||
@rule = rule
|
||||
@conversation = conversation
|
||||
file = File.read('./lib/filters/filter_keys.json')
|
||||
@filters = JSON.parse(file)
|
||||
end
|
||||
|
||||
def perform
|
||||
conversation_filters = @filters['conversations']
|
||||
|
||||
@rule.conditions.each_with_index do |query_hash, current_index|
|
||||
current_filter = conversation_filters[query_hash['attribute_key']]
|
||||
@query_string += conversation_query_string(current_filter, query_hash.with_indifferent_access, current_index)
|
||||
end
|
||||
|
||||
records = base_relation.where(@query_string, @filter_values.with_indifferent_access)
|
||||
records.any?
|
||||
end
|
||||
|
||||
def conversation_query_string(current_filter, query_hash, current_index)
|
||||
attribute_key = query_hash['attribute_key']
|
||||
query_operator = query_hash['query_operator']
|
||||
|
||||
filter_operator_value = filter_operation(query_hash, current_index)
|
||||
|
||||
case current_filter['attribute_type']
|
||||
when 'additional_attributes'
|
||||
" conversations.additional_attributes ->> '#{attribute_key}' #{filter_operator_value} #{query_operator} "
|
||||
when 'standard'
|
||||
if attribute_key == 'labels'
|
||||
" tags.id #{filter_operator_value} #{query_operator} "
|
||||
else
|
||||
" conversations.#{attribute_key} #{filter_operator_value} #{query_operator} "
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def base_relation
|
||||
Conversation.where(id: @conversation)
|
||||
end
|
||||
end
|
||||
@@ -51,7 +51,6 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
||||
def send_to_facebook_page(message_content)
|
||||
access_token = channel.page_access_token
|
||||
app_secret_proof = calculate_app_secret_proof(GlobalConfigService.load('FB_APP_SECRET', ''), access_token)
|
||||
|
||||
query = { access_token: access_token }
|
||||
query[:appsecret_proof] = app_secret_proof if app_secret_proof
|
||||
|
||||
|
||||
Reference in New Issue
Block a user