feat: Add APIs for Campaigns (#2175)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user