feat: Added new actions for macros

This commit is contained in:
tejaswini chile 2022-10-11 22:31:55 +05:30
parent 1bdd59f025
commit 05b31f90cb
9 changed files with 76 additions and 22 deletions

View file

@ -14,6 +14,8 @@ class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
render json: { error: @macro.errors.messages }, status: :unprocessable_entity and return unless @macro.valid?
@macro.save!
process_attachments
@macro
end
def show
@ -25,10 +27,21 @@ class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
head :ok
end
def attach_file
file_blob = ActiveStorage::Blob.create_and_upload!(
key: nil,
io: params[:attachment].tempfile,
filename: params[:attachment].original_filename,
content_type: params[:attachment].content_type
)
render json: { blob_key: file_blob.key, blob_id: file_blob.id }
end
def update
ActiveRecord::Base.transaction do
@macro.update!(macros_with_user)
@macro.set_visibility(current_user, permitted_params)
process_attachments
@macro.save!
rescue StandardError => e
Rails.logger.error e
@ -42,6 +55,17 @@ class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
head :ok
end
def process_attachments
actions = @macro.actions.filter_map { |k, _v| k if k['action_name'] == 'send_attachment' }
return if actions.blank?
actions.each do |action|
blob_id = action['action_params']
blob = ActiveStorage::Blob.find_by(id: blob_id)
@macro.files.attach(blob)
end
end
def permitted_params
params.permit(
:name, :account_id, :visibility,

View file

@ -195,6 +195,7 @@ class Conversation < ApplicationRecord
private
def execute_after_update_commit_callbacks
binding.pry
notify_status_change
create_activity
notify_conversation_updation
@ -259,7 +260,7 @@ class Conversation < ApplicationRecord
def create_label_change(user_name)
return unless user_name
previous_labels, current_labels = previous_changes[:label_list]
previous_labels, current_labels = reload.previous_changes[:label_list]
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)
create_label_added(user_name, current_labels - previous_labels)

View file

@ -19,17 +19,21 @@
# index_macros_on_updated_by_id (updated_by_id)
#
class Macro < ApplicationRecord
include Rails.application.routes.url_helpers
belongs_to :account
belongs_to :created_by,
class_name: :User
belongs_to :updated_by,
class_name: :User
has_many_attached :files
enum visibility: { personal: 0, global: 1 }
validate :json_actions_format
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agent send_webhook_event mute_conversation change_status
resolve_conversation snooze_conversation].freeze
ACTIONS_ATTRS = %w[send_message add_label assign_team assign_best_agent mute_conversation change_status
resolve_conversation snooze_conversation send_email_transcript send_attachment].freeze
def set_visibility(user, params)
self.visibility = params[:visibility]
@ -47,6 +51,20 @@ class Macro < ApplicationRecord
params[:page] || 1
end
def file_base_data
files.map do |file|
{
id: file.id,
automation_rule_id: id,
file_type: file.content_type,
account_id: account_id,
file_url: url_for(file),
blob_id: file.blob_id,
filename: file.filename.to_s
}
end
end
private
def json_actions_format

View file

@ -22,4 +22,8 @@ class MacroPolicy < ApplicationPolicy
def execute?
true
end
def attach_file?
true
end
end

View file

@ -39,6 +39,12 @@ class ActionService
@conversation.update!(team_id: team_ids[0])
end
def send_email_transcript(emails)
emails.each do |email|
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, email)&.deliver_later
end
end
private
def agent_belongs_to_account?(agent_ids)

View file

@ -26,21 +26,15 @@ class AutomationRules::ActionService < ActionService
return unless @rule.files.attached?
blob = ActiveStorage::Blob.find(blob_ids)
blobs = ActiveStorage::Blob.where(id: blob_ids)
return if blob.blank?
return if blobs.blank?
params = { content: nil, private: false, attachments: 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
end
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)

View file

@ -21,18 +21,25 @@ class Macros::ExecutionService < ActionService
private
def send_webhook_event(webhook_url)
payload = @conversation.webhook_data.merge(event: "macro_event.#{@macro.name}")
WebhookJob.perform_later(webhook_url[0], payload)
end
def send_message(message)
return if conversation_a_tweet?
params = { content: message[0], private: false, content_attributes: { macro_id: @macro.id } }
params = { content: message[0], private: false }
mb = Messages::MessageBuilder.new(nil, @conversation, params)
mb.perform
end
def send_email_to_team(_params); end
def send_attachment(blob_ids)
return if conversation_a_tweet?
return unless @macro.files.attached?
blobs = ActiveStorage::Blob.where(id: blob_ids)
return if blobs.blank?
params = { content: nil, private: false, attachments: blobs }
mb = Messages::MessageBuilder.new(nil, @conversation, params)
mb.perform
end
end

View file

@ -16,3 +16,4 @@ end
json.account_id macro.account_id
json.actions macro.actions
json.files macro.file_base_data if macro.files.any?

View file

@ -58,9 +58,8 @@ Rails.application.routes.draw do
post :attach_file, on: :collection
end
resources :macros, only: [:index, :create, :show, :update, :destroy] do
member do
post :execute
end
post :execute, on: :member
post :attach_file, on: :collection
end
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
resources :dashboard_apps, only: [:index, :show, :create, :update, :destroy]