2022-07-19 12:07:00 +00:00
|
|
|
class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
|
2022-07-26 07:11:22 +00:00
|
|
|
before_action :fetch_macro, only: [:show, :update, :destroy, :execute]
|
2022-11-08 01:46:00 +00:00
|
|
|
before_action :check_authorization, only: [:show, :update, :destroy, :execute]
|
2022-07-19 12:07:00 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
@macros = Macro.with_visibility(current_user, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@macro = Current.account.macros.new(macros_with_user.merge(created_by_id: current_user.id))
|
|
|
|
@macro.set_visibility(current_user, permitted_params)
|
|
|
|
@macro.actions = params[:actions]
|
|
|
|
|
|
|
|
render json: { error: @macro.errors.messages }, status: :unprocessable_entity and return unless @macro.valid?
|
|
|
|
|
|
|
|
@macro.save!
|
2022-10-21 02:41:48 +00:00
|
|
|
process_attachments
|
|
|
|
@macro
|
2022-07-19 12:07:00 +00:00
|
|
|
end
|
|
|
|
|
2022-09-21 08:40:35 +00:00
|
|
|
def show
|
|
|
|
head :not_found if @macro.nil?
|
|
|
|
end
|
2022-07-19 12:07:00 +00:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
@macro.destroy!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2022-10-21 02:41:48 +00:00
|
|
|
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
|
|
|
|
|
2022-07-19 12:07:00 +00:00
|
|
|
def update
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@macro.update!(macros_with_user)
|
|
|
|
@macro.set_visibility(current_user, permitted_params)
|
2022-10-21 02:41:48 +00:00
|
|
|
process_attachments
|
2022-07-19 12:07:00 +00:00
|
|
|
@macro.save!
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e
|
|
|
|
render json: { error: @macro.errors.messages }.to_json, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-26 07:11:22 +00:00
|
|
|
def execute
|
|
|
|
::MacrosExecutionJob.perform_later(@macro, conversation_ids: params[:conversation_ids], user: Current.user)
|
|
|
|
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2022-11-08 01:46:00 +00:00
|
|
|
private
|
|
|
|
|
2022-10-21 02:41:48 +00:00
|
|
|
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
|
|
|
|
|
2022-07-19 12:07:00 +00:00
|
|
|
def permitted_params
|
|
|
|
params.permit(
|
|
|
|
:name, :account_id, :visibility,
|
|
|
|
actions: [:action_name, { action_params: [] }]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def macros_with_user
|
|
|
|
permitted_params.merge(updated_by_id: current_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_macro
|
|
|
|
@macro = Current.account.macros.find_by(id: params[:id])
|
|
|
|
end
|
2022-11-08 01:46:00 +00:00
|
|
|
|
|
|
|
def check_authorization
|
|
|
|
authorize(@macro) if @macro.present?
|
|
|
|
end
|
2022-07-19 12:07:00 +00:00
|
|
|
end
|