diff --git a/app/builders/campaigns/campaign_conversation_builder.rb b/app/builders/campaigns/campaign_conversation_builder.rb index 9eb640576..b04c6d077 100644 --- a/app/builders/campaigns/campaign_conversation_builder.rb +++ b/app/builders/campaigns/campaign_conversation_builder.rb @@ -21,7 +21,8 @@ class Campaigns::CampaignConversationBuilder def message_params ActionController::Parameters.new({ - content: @campaign.message + content: @campaign.message, + campaign_id: @campaign.id }) end diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index d2936e9f3..5c8cadbcd 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -69,6 +69,10 @@ class Messages::MessageBuilder @automation_rule.present? ? { content_attributes: { automation_rule_id: @automation_rule } } : {} end + def campaign_id + @params[:campaign_id].present? ? { additional_attributes: { campaign_id: @params[:campaign_id] } } : {} + end + def message_sender return if @params[:sender_type] != 'AgentBot' @@ -87,6 +91,6 @@ class Messages::MessageBuilder items: @items, in_reply_to: @in_reply_to, echo_id: @params[:echo_id] - }.merge(external_created_at).merge(automation_rule_id) + }.merge(external_created_at).merge(automation_rule_id).merge(campaign_id) end end diff --git a/app/models/message.rb b/app/models/message.rb index 13d58db6b..48f58768c 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -2,30 +2,32 @@ # # Table name: messages # -# id :integer not null, primary key -# content :text -# content_attributes :json -# content_type :integer default("text"), not null -# external_source_ids :jsonb -# message_type :integer not null -# private :boolean default(FALSE) -# sender_type :string -# status :integer default("sent") -# created_at :datetime not null -# updated_at :datetime not null -# account_id :integer not null -# conversation_id :integer not null -# inbox_id :integer not null -# sender_id :bigint -# source_id :string +# id :integer not null, primary key +# additional_attributes :jsonb +# content :text +# content_attributes :json +# content_type :integer default("text"), not null +# external_source_ids :jsonb +# message_type :integer not null +# private :boolean default(FALSE) +# sender_type :string +# status :integer default("sent") +# created_at :datetime not null +# updated_at :datetime not null +# account_id :integer not null +# conversation_id :integer not null +# inbox_id :integer not null +# sender_id :bigint +# source_id :string # # Indexes # -# index_messages_on_account_id (account_id) -# index_messages_on_conversation_id (conversation_id) -# index_messages_on_inbox_id (inbox_id) -# index_messages_on_sender_type_and_sender_id (sender_type,sender_id) -# index_messages_on_source_id (source_id) +# index_messages_on_account_id (account_id) +# index_messages_on_additional_attributes_campaign_id (((additional_attributes -> 'campaign_id'::text))) USING gin +# index_messages_on_conversation_id (conversation_id) +# index_messages_on_inbox_id (inbox_id) +# index_messages_on_sender_type_and_sender_id (sender_type,sender_id) +# index_messages_on_source_id (source_id) # class Message < ApplicationRecord @@ -168,7 +170,7 @@ class Message < ApplicationRecord def dispatch_create_events Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by) - if outgoing? && conversation.messages.outgoing.count == 1 + if outgoing? && conversation.messages.outgoing.where("(additional_attributes->'campaign_id') is null").count == 1 Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by) end end diff --git a/db/migrate/20220506163839_add_additional_attributes_to_message.rb b/db/migrate/20220506163839_add_additional_attributes_to_message.rb new file mode 100644 index 000000000..713584c41 --- /dev/null +++ b/db/migrate/20220506163839_add_additional_attributes_to_message.rb @@ -0,0 +1,14 @@ +class AddAdditionalAttributesToMessage < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def up + add_column :messages, :additional_attributes, :jsonb, default: {} + add_index :messages, "((additional_attributes->'campaign_id'))", name: 'index_messages_on_additional_attributes_campaign_id', using: 'gin', + algorithm: :concurrently + end + + def down + remove_index :messages, name: 'index_messages_on_additional_attributes_campaign_id' + remove_column :messages, :additional_attributes + end +end diff --git a/db/schema.rb b/db/schema.rb index c998b839b..157e2ac2f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_28_101325) do +ActiveRecord::Schema.define(version: 2022_05_06_163839) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -577,6 +577,8 @@ ActiveRecord::Schema.define(version: 2022_04_28_101325) do t.string "sender_type" t.bigint "sender_id" t.jsonb "external_source_ids", default: {} + t.jsonb "additional_attributes", default: {} + t.index "((additional_attributes -> 'campaign_id'::text))", name: "index_messages_on_additional_attributes_campaign_id", using: :gin t.index ["account_id"], name: "index_messages_on_account_id" t.index ["conversation_id"], name: "index_messages_on_conversation_id" t.index ["inbox_id"], name: "index_messages_on_inbox_id" diff --git a/spec/builders/campaigns/campaign_conversation_builder_spec.rb b/spec/builders/campaigns/campaign_conversation_builder_spec.rb index 3735c0e66..c461337cd 100644 --- a/spec/builders/campaigns/campaign_conversation_builder_spec.rb +++ b/spec/builders/campaigns/campaign_conversation_builder_spec.rb @@ -16,6 +16,7 @@ describe ::Campaigns::CampaignConversationBuilder do expect(campaign_conversation.campaign_id).to eq(campaign.id) expect(campaign_conversation.messages.first.content).to eq(campaign.message) + expect(campaign_conversation.messages.first.additional_attributes['campaign_id']).to eq(campaign.id) end it 'will not create a conversation with campaign id if another conversation exists' do