Fix: Campaign triggers first_response reporting event (#4607)
* Fix: Campaign triggers first_response reporting event * fix spec failure
This commit is contained in:
parent
5b5a6d89c0
commit
360b438a55
@ -21,7 +21,8 @@ class Campaigns::CampaignConversationBuilder
|
|||||||
|
|
||||||
def message_params
|
def message_params
|
||||||
ActionController::Parameters.new({
|
ActionController::Parameters.new({
|
||||||
content: @campaign.message
|
content: @campaign.message,
|
||||||
|
campaign_id: @campaign.id
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -69,6 +69,10 @@ class Messages::MessageBuilder
|
|||||||
@automation_rule.present? ? { content_attributes: { automation_rule_id: @automation_rule } } : {}
|
@automation_rule.present? ? { content_attributes: { automation_rule_id: @automation_rule } } : {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def campaign_id
|
||||||
|
@params[:campaign_id].present? ? { additional_attributes: { campaign_id: @params[:campaign_id] } } : {}
|
||||||
|
end
|
||||||
|
|
||||||
def message_sender
|
def message_sender
|
||||||
return if @params[:sender_type] != 'AgentBot'
|
return if @params[:sender_type] != 'AgentBot'
|
||||||
|
|
||||||
@ -87,6 +91,6 @@ class Messages::MessageBuilder
|
|||||||
items: @items,
|
items: @items,
|
||||||
in_reply_to: @in_reply_to,
|
in_reply_to: @in_reply_to,
|
||||||
echo_id: @params[:echo_id]
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,30 +2,32 @@
|
|||||||
#
|
#
|
||||||
# Table name: messages
|
# Table name: messages
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# content :text
|
# additional_attributes :jsonb
|
||||||
# content_attributes :json
|
# content :text
|
||||||
# content_type :integer default("text"), not null
|
# content_attributes :json
|
||||||
# external_source_ids :jsonb
|
# content_type :integer default("text"), not null
|
||||||
# message_type :integer not null
|
# external_source_ids :jsonb
|
||||||
# private :boolean default(FALSE)
|
# message_type :integer not null
|
||||||
# sender_type :string
|
# private :boolean default(FALSE)
|
||||||
# status :integer default("sent")
|
# sender_type :string
|
||||||
# created_at :datetime not null
|
# status :integer default("sent")
|
||||||
# updated_at :datetime not null
|
# created_at :datetime not null
|
||||||
# account_id :integer not null
|
# updated_at :datetime not null
|
||||||
# conversation_id :integer not null
|
# account_id :integer not null
|
||||||
# inbox_id :integer not null
|
# conversation_id :integer not null
|
||||||
# sender_id :bigint
|
# inbox_id :integer not null
|
||||||
# source_id :string
|
# sender_id :bigint
|
||||||
|
# source_id :string
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_messages_on_account_id (account_id)
|
# index_messages_on_account_id (account_id)
|
||||||
# index_messages_on_conversation_id (conversation_id)
|
# index_messages_on_additional_attributes_campaign_id (((additional_attributes -> 'campaign_id'::text))) USING gin
|
||||||
# index_messages_on_inbox_id (inbox_id)
|
# index_messages_on_conversation_id (conversation_id)
|
||||||
# index_messages_on_sender_type_and_sender_id (sender_type,sender_id)
|
# index_messages_on_inbox_id (inbox_id)
|
||||||
# index_messages_on_source_id (source_id)
|
# index_messages_on_sender_type_and_sender_id (sender_type,sender_id)
|
||||||
|
# index_messages_on_source_id (source_id)
|
||||||
#
|
#
|
||||||
|
|
||||||
class Message < ApplicationRecord
|
class Message < ApplicationRecord
|
||||||
@ -168,7 +170,7 @@ class Message < ApplicationRecord
|
|||||||
def dispatch_create_events
|
def dispatch_create_events
|
||||||
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
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)
|
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -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
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_stat_statements"
|
enable_extension "pg_stat_statements"
|
||||||
@ -577,6 +577,8 @@ ActiveRecord::Schema.define(version: 2022_04_28_101325) do
|
|||||||
t.string "sender_type"
|
t.string "sender_type"
|
||||||
t.bigint "sender_id"
|
t.bigint "sender_id"
|
||||||
t.jsonb "external_source_ids", default: {}
|
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 ["account_id"], name: "index_messages_on_account_id"
|
||||||
t.index ["conversation_id"], name: "index_messages_on_conversation_id"
|
t.index ["conversation_id"], name: "index_messages_on_conversation_id"
|
||||||
t.index ["inbox_id"], name: "index_messages_on_inbox_id"
|
t.index ["inbox_id"], name: "index_messages_on_inbox_id"
|
||||||
|
|||||||
@ -16,6 +16,7 @@ describe ::Campaigns::CampaignConversationBuilder do
|
|||||||
|
|
||||||
expect(campaign_conversation.campaign_id).to eq(campaign.id)
|
expect(campaign_conversation.campaign_id).to eq(campaign.id)
|
||||||
expect(campaign_conversation.messages.first.content).to eq(campaign.message)
|
expect(campaign_conversation.messages.first.content).to eq(campaign.message)
|
||||||
|
expect(campaign_conversation.messages.first.additional_attributes['campaign_id']).to eq(campaign.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not create a conversation with campaign id if another conversation exists' do
|
it 'will not create a conversation with campaign id if another conversation exists' do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user