feat: Add APIs for Campaigns (#2175)
This commit is contained in:
28
app/controllers/api/v1/accounts/campaigns_controller.rb
Normal file
28
app/controllers/api/v1/accounts/campaigns_controller.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController
|
||||
before_action :campaign, except: [:index, :create]
|
||||
before_action :check_authorization
|
||||
|
||||
def index
|
||||
@campaigns = Current.account.campaigns
|
||||
end
|
||||
|
||||
def create
|
||||
@campaign = Current.account.campaigns.create!(campaign_params)
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def update
|
||||
@campaign.update(campaign_params)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def campaign
|
||||
@campaign ||= Current.account.campaigns.find_by(display_id: params[:id])
|
||||
end
|
||||
|
||||
def campaign_params
|
||||
params.require(:campaign).permit(:title, :description, :content, :enabled, :inbox_id, :sender_id, trigger_rules: {})
|
||||
end
|
||||
end
|
||||
@@ -11,6 +11,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||
@assignable_agents = (Current.account.users.where(id: @inbox.members.select(:user_id)) + Current.account.administrators).uniq
|
||||
end
|
||||
|
||||
def campaigns
|
||||
@campaigns = @inbox.campaigns
|
||||
end
|
||||
|
||||
def create
|
||||
ActiveRecord::Base.transaction do
|
||||
channel = create_channel
|
||||
|
||||
@@ -36,6 +36,7 @@ class Account < ApplicationRecord
|
||||
has_many :data_imports, dependent: :destroy
|
||||
has_many :users, through: :account_users
|
||||
has_many :inboxes, dependent: :destroy
|
||||
has_many :campaigns, dependent: :destroy
|
||||
has_many :conversations, dependent: :destroy
|
||||
has_many :messages, dependent: :destroy
|
||||
has_many :contacts, dependent: :destroy
|
||||
@@ -104,4 +105,8 @@ class Account < ApplicationRecord
|
||||
trigger.after(:insert).for_each(:row) do
|
||||
"execute format('create sequence IF NOT EXISTS conv_dpid_seq_%s', NEW.id);"
|
||||
end
|
||||
|
||||
trigger.name('camp_dpid_before_insert').after(:insert).for_each(:row) do
|
||||
"execute format('create sequence IF NOT EXISTS camp_dpid_seq_%s', NEW.id);"
|
||||
end
|
||||
end
|
||||
|
||||
49
app/models/campaign.rb
Normal file
49
app/models/campaign.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: campaigns
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# content :text not null
|
||||
# description :text
|
||||
# enabled :boolean default(TRUE)
|
||||
# title :string not null
|
||||
# trigger_rules :jsonb
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# display_id :integer not null
|
||||
# inbox_id :bigint not null
|
||||
# sender_id :integer
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_campaigns_on_account_id (account_id)
|
||||
# index_campaigns_on_inbox_id (inbox_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (account_id => accounts.id)
|
||||
# fk_rails_... (inbox_id => inboxes.id)
|
||||
#
|
||||
class Campaign < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
belongs_to :account
|
||||
belongs_to :inbox
|
||||
belongs_to :sender, class_name: 'User', optional: true
|
||||
|
||||
has_many :conversations, dependent: :nullify, autosave: true
|
||||
|
||||
after_commit :set_display_id, unless: :display_id?
|
||||
|
||||
private
|
||||
|
||||
def set_display_id
|
||||
reload
|
||||
end
|
||||
|
||||
# creating db triggers
|
||||
trigger.before(:insert).for_each(:row) do
|
||||
"NEW.display_id := nextval('camp_dpid_seq_' || NEW.account_id);"
|
||||
end
|
||||
end
|
||||
@@ -14,6 +14,7 @@
|
||||
# updated_at :datetime not null
|
||||
# account_id :integer not null
|
||||
# assignee_id :integer
|
||||
# campaign_id :bigint
|
||||
# contact_id :bigint
|
||||
# contact_inbox_id :bigint
|
||||
# display_id :integer not null
|
||||
@@ -24,11 +25,13 @@
|
||||
#
|
||||
# index_conversations_on_account_id (account_id)
|
||||
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
|
||||
# index_conversations_on_campaign_id (campaign_id)
|
||||
# index_conversations_on_contact_inbox_id (contact_inbox_id)
|
||||
# index_conversations_on_team_id (team_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (campaign_id => campaigns.id)
|
||||
# fk_rails_... (contact_inbox_id => contact_inboxes.id)
|
||||
# fk_rails_... (team_id => teams.id)
|
||||
#
|
||||
@@ -54,6 +57,7 @@ class Conversation < ApplicationRecord
|
||||
belongs_to :contact
|
||||
belongs_to :contact_inbox
|
||||
belongs_to :team, optional: true
|
||||
belongs_to :campaign, optional: true
|
||||
|
||||
has_many :messages, dependent: :destroy, autosave: true
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ class Inbox < ApplicationRecord
|
||||
|
||||
belongs_to :channel, polymorphic: true, dependent: :destroy
|
||||
|
||||
has_many :campaigns, dependent: :destroy
|
||||
has_many :contact_inboxes, dependent: :destroy
|
||||
has_many :contacts, through: :contact_inboxes
|
||||
|
||||
|
||||
21
app/policies/campaign_policy.rb
Normal file
21
app/policies/campaign_policy.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class CampaignPolicy < ApplicationPolicy
|
||||
def index?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def update?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def show?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def create?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@account_user.administrator?
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,10 @@ class InboxPolicy < ApplicationPolicy
|
||||
true
|
||||
end
|
||||
|
||||
def campaigns?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def create?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
1
app/views/api/v1/accounts/campaigns/create.json.jbuilder
Normal file
1
app/views/api/v1/accounts/campaigns/create.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/campaign.json.jbuilder', resource: @campaign
|
||||
3
app/views/api/v1/accounts/campaigns/index.json.jbuilder
Normal file
3
app/views/api/v1/accounts/campaigns/index.json.jbuilder
Normal file
@@ -0,0 +1,3 @@
|
||||
json.array! @campaigns do |campaign|
|
||||
json.partial! 'api/v1/models/campaign.json.jbuilder', resource: campaign
|
||||
end
|
||||
1
app/views/api/v1/accounts/campaigns/show.json.jbuilder
Normal file
1
app/views/api/v1/accounts/campaigns/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/campaign.json.jbuilder', resource: @campaign
|
||||
1
app/views/api/v1/accounts/campaigns/update.json.jbuilder
Normal file
1
app/views/api/v1/accounts/campaigns/update.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/campaign.json.jbuilder', resource: @campaign
|
||||
@@ -0,0 +1,3 @@
|
||||
json.array! @campaigns do |campaign|
|
||||
json.partial! 'api/v1/models/campaign.json.jbuilder', resource: campaign
|
||||
end
|
||||
15
app/views/api/v1/models/_campaign.json.jbuilder
Normal file
15
app/views/api/v1/models/_campaign.json.jbuilder
Normal file
@@ -0,0 +1,15 @@
|
||||
json.id resource.display_id
|
||||
json.content resource.content
|
||||
json.description resource.description
|
||||
json.enabled resource.enabled
|
||||
json.title resource.title
|
||||
json.trigger_rules resource.trigger_rules
|
||||
json.account_id resource.account_id
|
||||
json.inbox do
|
||||
json.partial! 'api/v1/models/inbox.json.jbuilder', resource: resource.inbox
|
||||
end
|
||||
json.sender do
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: resource.sender if resource.sender.present?
|
||||
end
|
||||
json.created_at resource.created_at
|
||||
json.updated_at resource.updated_at
|
||||
Reference in New Issue
Block a user