feat: Macros CRUD api (#5047)
This commit is contained in:
@@ -46,7 +46,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
|
||||
|
||||
def list_params
|
||||
params.require(:payload).permit(
|
||||
:category_slug, :locale, :query
|
||||
:category_slug, :locale, :query, :page
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
51
app/controllers/api/v1/accounts/macros_controller.rb
Normal file
51
app/controllers/api/v1/accounts/macros_controller.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
|
||||
before_action :check_authorization
|
||||
before_action :fetch_macro, only: [:show, :update, :destroy]
|
||||
|
||||
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!
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def destroy
|
||||
@macro.destroy!
|
||||
head :ok
|
||||
end
|
||||
|
||||
def update
|
||||
ActiveRecord::Base.transaction do
|
||||
@macro.update!(macros_with_user)
|
||||
@macro.set_visibility(current_user, permitted_params)
|
||||
@macro.save!
|
||||
rescue StandardError => e
|
||||
Rails.logger.error e
|
||||
render json: { error: @macro.errors.messages }.to_json, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
@@ -40,7 +40,8 @@ class Account < ApplicationRecord
|
||||
has_many :agent_bots, dependent: :destroy_async
|
||||
has_many :api_channels, dependent: :destroy_async, class_name: '::Channel::Api'
|
||||
has_many :articles, dependent: :destroy_async, class_name: '::Article'
|
||||
has_many :automation_rules, dependent: :destroy
|
||||
has_many :automation_rules, dependent: :destroy_async
|
||||
has_many :macros, dependent: :destroy_async
|
||||
has_many :campaigns, dependent: :destroy_async
|
||||
has_many :canned_responses, dependent: :destroy_async
|
||||
has_many :categories, dependent: :destroy_async, class_name: '::Category'
|
||||
|
||||
49
app/models/macro.rb
Normal file
49
app/models/macro.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: macros
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# actions :jsonb not null
|
||||
# name :string not null
|
||||
# visibility :integer default("user")
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# created_by_id :bigint not null
|
||||
# updated_by_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_macros_on_account_id (account_id)
|
||||
# index_macros_on_created_by_id (created_by_id)
|
||||
# index_macros_on_updated_by_id (updated_by_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (created_by_id => users.id)
|
||||
# fk_rails_... (updated_by_id => users.id)
|
||||
#
|
||||
class Macro < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :created_by,
|
||||
class_name: :User
|
||||
belongs_to :updated_by,
|
||||
class_name: :User
|
||||
enum visibility: { personal: 0, global: 1 }
|
||||
|
||||
def set_visibility(user, params)
|
||||
self.visibility = params[:visibility]
|
||||
self.visibility = :personal if user.agent?
|
||||
end
|
||||
|
||||
def self.with_visibility(user, params)
|
||||
records = user.administrator? ? Current.account.macros : Current.account.macros.global
|
||||
records = records.or(personal.where(created_by_id: user.id)) if user.agent?
|
||||
records.page(current_page(params))
|
||||
records
|
||||
end
|
||||
|
||||
def self.current_page(params)
|
||||
params[:page] || 1
|
||||
end
|
||||
end
|
||||
@@ -100,6 +100,7 @@ class User < ApplicationRecord
|
||||
class_name: :Portal,
|
||||
dependent: :nullify,
|
||||
source: :portal
|
||||
has_many :macros, foreign_key: 'created_by_id', dependent: :destroy_async
|
||||
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
|
||||
21
app/policies/macro_policy.rb
Normal file
21
app/policies/macro_policy.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class MacroPolicy < ApplicationPolicy
|
||||
def index?
|
||||
true
|
||||
end
|
||||
|
||||
def create?
|
||||
true
|
||||
end
|
||||
|
||||
def show?
|
||||
true
|
||||
end
|
||||
|
||||
def update?
|
||||
true
|
||||
end
|
||||
|
||||
def destroy?
|
||||
true
|
||||
end
|
||||
end
|
||||
3
app/views/api/v1/accounts/macros/create.json.jbuilder
Normal file
3
app/views/api/v1/accounts/macros/create.json.jbuilder
Normal file
@@ -0,0 +1,3 @@
|
||||
json.payload do
|
||||
json.partial! 'api/v1/models/macro.json.jbuilder', macro: @macro
|
||||
end
|
||||
5
app/views/api/v1/accounts/macros/index.json.jbuilder
Normal file
5
app/views/api/v1/accounts/macros/index.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.payload do
|
||||
json.array! @macros do |macro|
|
||||
json.partial! 'api/v1/models/macro.json.jbuilder', macro: macro
|
||||
end
|
||||
end
|
||||
3
app/views/api/v1/accounts/macros/show.json.jbuilder
Normal file
3
app/views/api/v1/accounts/macros/show.json.jbuilder
Normal file
@@ -0,0 +1,3 @@
|
||||
json.payload do
|
||||
json.partial! 'api/v1/models/macro.json.jbuilder', macro: @macro
|
||||
end
|
||||
3
app/views/api/v1/accounts/macros/update.json.jbuilder
Normal file
3
app/views/api/v1/accounts/macros/update.json.jbuilder
Normal file
@@ -0,0 +1,3 @@
|
||||
json.payload do
|
||||
json.partial! 'api/v1/models/macro.json.jbuilder', macro: @macro
|
||||
end
|
||||
18
app/views/api/v1/models/_macro.json.jbuilder
Normal file
18
app/views/api/v1/models/_macro.json.jbuilder
Normal file
@@ -0,0 +1,18 @@
|
||||
json.id macro.id
|
||||
json.name macro.name
|
||||
json.visibility macro.visibility
|
||||
|
||||
if macro.created_by.present?
|
||||
json.created_by do
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: macro.created_by
|
||||
end
|
||||
end
|
||||
|
||||
if macro.updated_by.present?
|
||||
json.updated_by do
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: macro.updated_by
|
||||
end
|
||||
end
|
||||
|
||||
json.account_id macro.account_id
|
||||
json.actions macro.actions
|
||||
Reference in New Issue
Block a user