Feat: attachments automation (#4266)
This commit is contained in:
@@ -7,14 +7,22 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseCont
|
||||
end
|
||||
|
||||
def create
|
||||
@automation_rule = Current.account.automation_rules.create(automation_rules_permit)
|
||||
@automation_rule.update(actions: params[:actions])
|
||||
@automation_rule = Current.account.automation_rules.new(automation_rules_permit)
|
||||
@automation_rule.actions = params[:actions]
|
||||
|
||||
render json: { error: @automation_rule.errors.messages }, status: :unprocessable_entity and return unless @automation_rule.valid?
|
||||
|
||||
@automation_rule.save!
|
||||
process_attachments
|
||||
@automation_rule
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def update
|
||||
@automation_rule.update(automation_rules_permit)
|
||||
process_attachments
|
||||
@automation_rule
|
||||
end
|
||||
|
||||
def destroy
|
||||
@@ -31,6 +39,14 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseCont
|
||||
|
||||
private
|
||||
|
||||
def process_attachments
|
||||
return if params[:attachments].blank?
|
||||
|
||||
params[:attachments].each do |uploaded_attachment|
|
||||
@automation_rule.files.attach(uploaded_attachment)
|
||||
end
|
||||
end
|
||||
|
||||
def automation_rules_permit
|
||||
params.permit(
|
||||
:name, :description, :event_name, :account_id, :active,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#
|
||||
class AutomationRule < ApplicationRecord
|
||||
belongs_to :account
|
||||
has_many_attached :files
|
||||
|
||||
validate :json_conditions_format
|
||||
validate :json_actions_format
|
||||
@@ -26,8 +27,8 @@ class AutomationRule < ApplicationRecord
|
||||
|
||||
scope :active, -> { where(active: true) }
|
||||
|
||||
CONDITIONS_ATTRS = %w[country_code status browser_language assignee_id team_id referer].freeze
|
||||
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agents].freeze
|
||||
CONDITIONS_ATTRS = %w[email country_code status message_type browser_language assignee_id team_id referer city company].freeze
|
||||
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agents send_attachments].freeze
|
||||
|
||||
private
|
||||
|
||||
@@ -35,13 +36,17 @@ class AutomationRule < ApplicationRecord
|
||||
return if conditions.nil?
|
||||
|
||||
attributes = conditions.map { |obj, _| obj['attribute_key'] }
|
||||
(attributes - CONDITIONS_ATTRS).blank?
|
||||
conditions = attributes - CONDITIONS_ATTRS
|
||||
conditions -= account.custom_attribute_definitions.pluck(:attribute_key)
|
||||
errors.add(:conditions, "Automation conditions #{conditions.join(',')} not supported.") if conditions.any?
|
||||
end
|
||||
|
||||
def json_actions_format
|
||||
return if actions.nil?
|
||||
|
||||
attributes = actions.map { |obj, _| obj['attribute_key'] }
|
||||
(attributes - ACTIONS_ATTRS).blank?
|
||||
actions = attributes - ACTIONS_ATTRS
|
||||
|
||||
errors.add(:actions, "Automation actions #{actions.join(',')} not supported.") if actions.any?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,6 +21,15 @@ class AutomationRules::ActionService
|
||||
|
||||
private
|
||||
|
||||
def send_attachments(_file_params)
|
||||
return if @rule.event_name == 'message_created'
|
||||
|
||||
blobs = @rule.files.map { |file, _| file.blob }
|
||||
params = { content: nil, private: false, attachments: blobs }
|
||||
mb = Messages::MessageBuilder.new(nil, @conversation, params)
|
||||
mb.perform
|
||||
end
|
||||
|
||||
def send_email_transcript(emails)
|
||||
emails.each do |email|
|
||||
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, email)&.deliver_later
|
||||
@@ -48,7 +57,7 @@ class AutomationRules::ActionService
|
||||
return if @rule.event_name == 'message_created'
|
||||
|
||||
params = { content: message[0], private: false }
|
||||
mb = Messages::MessageBuilder.new(@administrator, @conversation, params)
|
||||
mb = Messages::MessageBuilder.new(nil, @conversation, params)
|
||||
mb.perform
|
||||
end
|
||||
|
||||
@@ -85,10 +94,6 @@ class AutomationRules::ActionService
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user