Skip to content

Commit

Permalink
chore: Campaign ID migration for existing accounts (chatwoot#2189)
Browse files Browse the repository at this point in the history
* chore: Campaign ID migration for existing accounts

* chore: update factory

* chore: minor fixes

* chore: fixes
  • Loading branch information
sojan-official authored Apr 30, 2021
1 parent bd7e9d2 commit a07200b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def campaign
end

def campaign_params
params.require(:campaign).permit(:title, :description, :content, :enabled, :inbox_id, :sender_id, trigger_rules: {})
params.require(:campaign).permit(:title, :description, :message, :enabled, :inbox_id, :sender_id, trigger_rules: {})
end
end
4 changes: 3 additions & 1 deletion app/models/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Table name: campaigns
#
# id :bigint not null, primary key
# content :text not null
# description :text
# enabled :boolean default(TRUE)
# message :text not null
# title :string not null
# trigger_rules :jsonb
# created_at :datetime not null
Expand All @@ -28,6 +28,8 @@
class Campaign < ApplicationRecord
validates :account_id, presence: true
validates :inbox_id, presence: true
validates :title, presence: true
validates :message, presence: true
belongs_to :account
belongs_to :inbox
belongs_to :sender, class_name: 'User', optional: true
Expand Down
8 changes: 4 additions & 4 deletions app/views/api/v1/models/_campaign.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,15 +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.description resource.description
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.message resource.message
json.enabled resource.enabled
json.trigger_rules resource.trigger_rules
json.created_at resource.created_at
json.updated_at resource.updated_at
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AddCampDpIdSeqForExistingAccounts < ActiveRecord::Migration[6.0]
def up
::Account.find_in_batches do |accounts_batch|
Rails.logger.info "migrated till #{accounts_batch.first.id}\n"
accounts_batch.each do |account|
display_id = account.campaigns.count
ActiveRecord::Base.connection.exec_query("create sequence IF NOT EXISTS camp_dpid_seq_#{account.id} START #{display_id + 1}")
end
end
end

def down
::Account.find_in_batches do |accounts_batch|
Rails.logger.info "migrated till #{accounts_batch.first.id}\n"
accounts_batch.each do |account|
ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS camp_dpid_seq_#{account.id}")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameCampaignContentToMessage < ActiveRecord::Migration[6.0]
def change
rename_column :campaigns, :content, :message
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_04_28_151147) do
ActiveRecord::Schema.define(version: 2021_04_30_100138) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -117,7 +117,7 @@
t.integer "display_id", null: false
t.string "title", null: false
t.text "description"
t.text "content", null: false
t.text "message", null: false
t.integer "sender_id"
t.boolean "enabled", default: true
t.bigint "account_id", null: false
Expand Down
12 changes: 6 additions & 6 deletions spec/controllers/api/v1/accounts/campaigns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/campaigns",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
as: :json

expect(response).to have_http_status(:unauthorized)
Expand All @@ -88,7 +88,7 @@

it 'returns unauthorized for agents' do
post "/api/v1/accounts/#{account.id}/campaigns",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
headers: agent.create_new_auth_token,
as: :json

Expand All @@ -97,7 +97,7 @@

it 'creates a new campaign' do
post "/api/v1/accounts/#{account.id}/campaigns",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
headers: administrator.create_new_auth_token,
as: :json

Expand All @@ -114,7 +114,7 @@
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
as: :json

expect(response).to have_http_status(:unauthorized)
Expand All @@ -127,7 +127,7 @@

it 'returns unauthorized for agents' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
headers: agent.create_new_auth_token,
as: :json

Expand All @@ -136,7 +136,7 @@

it 'updates the campaign' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
params: { inbox_id: inbox.id, title: 'test', content: 'test message' },
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
headers: administrator.create_new_auth_token,
as: :json

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/campaigns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FactoryBot.define do
factory :campaign do
sequence(:title) { |n| "Campaign #{n}" }
sequence(:content) { |n| "Campaign content #{n}" }
sequence(:message) { |n| "Campaign message #{n}" }
after(:build) do |campaign|
campaign.account ||= create(:account)
campaign.inbox ||= create(
Expand Down

0 comments on commit a07200b

Please sign in to comment.