Chatwoot/app/services/automation_rules/action_service.rb

60 lines
1.6 KiB
Ruby
Raw Normal View History

class AutomationRules::ActionService < ActionService
2022-03-21 07:42:27 +00:00
def initialize(rule, account, conversation)
super(conversation)
2022-01-10 07:11:59 +00:00
@rule = rule
2022-03-21 07:42:27 +00:00
@account = account
Current.executed_by = rule
2022-01-10 07:11:59 +00:00
end
def perform
2022-03-21 07:42:27 +00:00
@rule.actions.each do |action|
feat: Custom attributes in automations and refactor (#4548) * Custom attributes * Custom Attrs Manifest * Fix dropdown values for custom attributes * Handle edit mode for custom attributes * Ported duplicate logic to a mixin * fix Code climate issue * Fix Codeclimate complexity warning * Bug fix - Custom attributes getting duplicated * Bug fixes and Code Climate issue fix * Code Climate Issues Breakdown * Fix test spec * Add labels for Custom attributes in dropdown * Refactor * Refactor Automion mixin * Refactor Mixin * Refactor getOperator * Fix getOperatorType * File name method refactor * Refactor appendNewCondition * spec update * Refactor methods * Mixin Spec update * Automation Mixins Test Specs * Mixin Spec Rerun * Automation validations mixin spec * Automation helper test spec * Send custom_attr key * Fix spec fixtures * fix: Changes for custom attribute type and lower case search * fix: Specs * fix: Specs * fix: Ruby version change * fix: Ruby version change * Removes Lowercased values and fix label value in api payload * Fix specs * Fixed Query Spec * Removed disabled labels if no attributes are present * Code Climate Fixes * fix: custom attribute with indifferent access * fix: custom attribute with indifferent access * Fix specs * Minor label fix * REtrigger circle ci build * Update app/javascript/shared/mixins/specs/automationMixin.spec.js * Update app/javascript/shared/mixins/specs/automationMixin.spec.js * fix: Custom attribute case insensitivity search * Add missing reset action method to input * Set team_input to single select instead of multiple * fix: remove value case check for date,boolean and number data type * fix: cognitive complexity * fix: cognitive complexity * fix: Fixed activity message for automation system * fix: Fixed activity message for automation system * fix: Fixed activity message for automation system * fix: codeclimate * fix: codeclimate * fix: action cable events for label update * fix: codeclimate, conversation modela number of methods * fix: codeclimate, conversation modela number of methods * fix: codeclimate, conversation modela number of methods * fix: codeclimate, conversation modela number of methods * Fix margin bottom for attachment button * Remove margin bottom to avoid conflict from macros * Fix automation action query generator using the right key * fix: not running message created event for activity message * fix: not running message created event for activity message * codeclimate fix * codeclimate fix * codeclimate fix * Update app/javascript/dashboard/mixins/automations/methodsMixin.js Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> * Update app/javascript/shared/mixins/specs/automationHelper.spec.js Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> * Update app/javascript/dashboard/helper/automationHelper.js Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> * Update app/javascript/dashboard/mixins/automations/methodsMixin.js Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Pranav Raj S <pranav@chatwoot.com> Co-authored-by: Tejaswini <tejaswini@chatwoot.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2022-11-10 05:23:29 +00:00
@conversation.reload
2022-01-10 07:11:59 +00:00
action = action.with_indifferent_access
2022-03-21 07:42:27 +00:00
begin
send(action[:action_name], action[:action_params])
rescue StandardError => e
ChatwootExceptionTracker.new(e, account: @account).capture_exception
2022-03-21 07:42:27 +00:00
end
2022-01-10 07:11:59 +00:00
end
2022-03-21 07:42:27 +00:00
ensure
Current.reset
2022-01-10 07:11:59 +00:00
end
private
def send_attachment(blob_ids)
return if conversation_a_tweet?
return unless @rule.files.attached?
blobs = ActiveStorage::Blob.where(id: blob_ids)
return if blobs.blank?
params = { content: nil, private: false, attachments: blobs }
2022-03-30 02:38:58 +00:00
mb = Messages::MessageBuilder.new(nil, @conversation, params)
mb.perform
end
2022-03-29 07:57:16 +00:00
def send_webhook_event(webhook_url)
payload = @conversation.webhook_data.merge(event: "automation_event.#{@rule.event_name}")
2022-03-29 07:57:16 +00:00
WebhookJob.perform_later(webhook_url[0], payload)
2022-03-21 07:42:27 +00:00
end
2022-01-10 07:11:59 +00:00
def send_message(message)
return if conversation_a_tweet?
params = { content: message[0], private: false, content_attributes: { automation_rule_id: @rule.id } }
2022-03-30 02:38:58 +00:00
mb = Messages::MessageBuilder.new(nil, @conversation, params)
2022-03-21 07:42:27 +00:00
mb.perform
2022-01-10 07:11:59 +00:00
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
2022-01-10 07:11:59 +00:00
end
end
end