Fix: Campaign triggers first_response reporting event (#4607)

* Fix: Campaign triggers first_response reporting event

* fix spec failure
This commit is contained in:
Aswin Dev P.S 2022-05-09 14:04:52 +05:30 committed by GitHub
parent 5b5a6d89c0
commit 360b438a55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 25 deletions

View file

@ -21,7 +21,8 @@ class Campaigns::CampaignConversationBuilder
def message_params
ActionController::Parameters.new({
content: @campaign.message
content: @campaign.message,
campaign_id: @campaign.id
})
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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