Compare commits

...
Author SHA1 Message Date
Shivam Mishra 42139adb7b refactor: find_or_build_for_multiple_conversations 2024-04-03 18:09:41 +05:30
Shivam Mishra 411c6458eb fix: resolved condition for message_builder 2024-04-03 18:07:40 +05:30
Shivam Mishra c0232ad7c8 feat: handle conversation query order 2024-04-03 18:02:46 +05:30
Shivam Mishra c711f78d24 Revert "refactor conversation handling in message builders"
This reverts commit ad524a5cab.
2024-04-03 18:01:50 +05:30
Jaideep Guntupalli ad524a5cab refactor conversation handling in message builders 2024-04-03 15:32:11 +05:30
Jaideep GuntupalliandGitHub 9faafdbd25 Merge branch 'develop' into extending-lock-to-single-conversation-to-meta-inbox 2024-04-02 17:59:47 +05:30
Jaideep Guntupalli e74b318772 refactor: abstracted common query 2024-03-31 10:13:20 +05:30
Jaideep Guntupalli 54d522aa50 updated rspec tests to use factories 2024-03-28 01:31:48 +05:30
Jaideep Guntupalli 3abd839c1b refactored to independent methods 2024-03-23 11:21:34 +05:30
Jaideep Guntupalli 939f43aeb4 update: added count check 2024-03-22 01:28:25 +05:30
Jaideep GuntupalliandGitHub f3c069b6d6 Merge branch 'develop' into extending-lock-to-single-conversation-to-meta-inbox 2024-03-22 01:24:20 +05:30
Jaideep GuntupalliandGitHub 51d3c10f57 Merge branch 'develop' into extending-lock-to-single-conversation-to-meta-inbox 2024-03-22 01:24:06 +05:30
Jaideep GuntupalliandGitHub 647b9fc029 Merge branch 'develop' into extending-lock-to-single-conversation-to-meta-inbox 2024-03-21 20:29:44 +05:30
Jaideep Guntupalli 55e9d912b2 refactored tests and the conversation function for instagram and facebook 2024-03-21 19:54:18 +05:30
Jaideep Guntupalli 2707c8b380 rspec tests for lock_to_single_conversation for meta inboxes 2024-03-21 18:33:54 +05:30
Jaideep GuntupalliandGitHub e3a03a9efe Merge branch 'develop' into extending-lock-to-single-conversation-to-meta-inbox 2024-03-13 12:51:35 +05:30
Jaideep Guntupalli 7c4275532d refactor: added comments for clarity 2024-03-13 10:39:20 +05:30
Jaideep Guntupalli 617a1a9fc9 fix: added additional attributes filter and refactored conversation logic in Instagram message builder 2024-03-13 10:36:07 +05:30
Jaideep Guntupalli f225a004e3 feat: backend Changes in Message Handling 2024-03-13 10:04:59 +05:30
Jaideep Guntupalli bcb5165de7 feat: enable LOCK_TO_SINGLE_CONVERSATION setting for meta inbox 2024-03-12 00:50:59 +05:30
6 changed files with 235 additions and 5 deletions
@@ -53,7 +53,23 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder
end
def conversation
@conversation ||= Conversation.find_by(conversation_params) || build_conversation
@conversation ||= set_conversation_based_on_inbox_config
end
def set_conversation_based_on_inbox_config
if @inbox.lock_to_single_conversation
Conversation.where(conversation_params).order(created_at: :desc).first || build_conversation
else
find_or_build_for_multiple_conversations
end
end
def find_or_build_for_multiple_conversations
# If lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
last_conversation = Conversation.where(conversation_params).where.not(status: :resolved).order(created_at: :desc).first
return build_conversation if last_conversation.nil?
last_conversation
end
def build_conversation
@@ -69,9 +69,28 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder
end
def conversation
@conversation ||= Conversation.where(conversation_params).find_by(
"additional_attributes ->> 'type' = 'instagram_direct_message'"
) || build_conversation
@conversation ||= set_conversation_based_on_inbox_config
end
def instagram_direct_message_conversation
Conversation.where(conversation_params)
.where("additional_attributes ->> 'type' = 'instagram_direct_message'")
end
def set_conversation_based_on_inbox_config
if @inbox.lock_to_single_conversation
instagram_direct_message_conversation.order(created_at: :desc).first || build_conversation
else
find_or_build_for_multiple_conversations
end
end
def find_or_build_for_multiple_conversations
last_conversation = instagram_direct_message_conversation..where.not(status: :resolved).order(created_at: :desc).first
return build_conversation if last_conversation.nil?
last_conversation
end
def message_content
@@ -589,7 +589,9 @@ export default {
return this.inbox.name;
},
canLocktoSingleConversation() {
return this.isASmsInbox || this.isAWhatsAppChannel;
return (
this.isASmsInbox || this.isAWhatsAppChannel || this.isAFacebookInbox
);
},
inboxNameLabel() {
if (this.isAWebWidgetInbox) {
@@ -58,5 +58,91 @@ describe Messages::Facebook::MessageBuilder do
expect(facebook_channel.inbox.reload.contacts.count).to eq(1)
expect(contact.name).to eq(default_name)
end
context 'when lock to single conversation' do
subject(:mocked_message_builder) do
described_class.new(mocked_incoming_fb_text_message, facebook_channel.inbox).perform
end
let!(:mocked_message_object) { build(:mocked_message_text, sender_id: contact_inbox.source_id).to_json }
let!(:mocked_incoming_fb_text_message) { Integrations::Facebook::MessageParser.new(mocked_message_object) }
let(:contact) { create(:contact, name: 'Jane Dae') }
let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: facebook_channel.inbox.id) }
context 'when lock to single conversation is disabled' do
before do
facebook_channel.inbox.update!(lock_to_single_conversation: false)
stub_request(:get, /graph.facebook.com/)
end
it 'creates a new conversation if existing conversation is not present' do
inital_count = Conversation.count
mocked_message_builder
facebook_channel.inbox.reload
expect(facebook_channel.inbox.conversations.count).to eq(1)
expect(Conversation.count).to eq(inital_count + 1)
end
it 'will not create a new conversation if last conversation is not resolved' do
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
contact_id: contact.id, contact_inbox_id: contact_inbox.id,
status: :open)
mocked_message_builder
facebook_channel.inbox.reload
expect(facebook_channel.inbox.conversations.last.id).to eq(existing_conversation.id)
end
it 'creates a new conversation if last conversation is resolved' do
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
contact_id: contact.id, contact_inbox_id: contact_inbox.id, status: :resolved)
inital_count = Conversation.count
mocked_message_builder
facebook_channel.inbox.reload
expect(facebook_channel.inbox.conversations.last.id).not_to eq(existing_conversation.id)
expect(Conversation.count).to eq(inital_count + 1)
end
end
context 'when lock to single conversation is enabled' do
before do
facebook_channel.inbox.update!(lock_to_single_conversation: true)
stub_request(:get, /graph.facebook.com/)
end
it 'creates a new conversation if existing conversation is not present' do
inital_count = Conversation.count
mocked_message_builder
facebook_channel.inbox.reload
expect(facebook_channel.inbox.conversations.count).to eq(1)
expect(Conversation.count).to eq(inital_count + 1)
end
it 'reopens last conversation if last conversation exists' do
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
contact_id: contact.id, contact_inbox_id: contact_inbox.id)
inital_count = Conversation.count
mocked_message_builder
facebook_channel.inbox.reload
expect(facebook_channel.inbox.conversations.last.id).to eq(existing_conversation.id)
expect(Conversation.count).to eq(inital_count)
end
end
end
end
end
@@ -183,4 +183,96 @@ describe Messages::Instagram::MessageBuilder do
expect(contact.name).to eq('Jane Dae')
end
end
context 'when lock to single conversation is disabled' do
before do
instagram_inbox.update!(lock_to_single_conversation: false)
stub_request(:get, /graph.facebook.com/)
end
it 'creates a new conversation if existing conversation is not present' do
inital_count = Conversation.count
message = dm_params[:entry][0]['messaging'][0]
contact_inbox
described_class.new(message, instagram_inbox).perform
instagram_inbox.reload
contact_inbox.reload
expect(instagram_inbox.conversations.count).to eq(1)
expect(Conversation.count).to eq(inital_count + 1)
end
it 'will not create a new conversation if last conversation is not resolved' do
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :open,
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
message = dm_params[:entry][0]['messaging'][0]
contact_inbox
described_class.new(message, instagram_inbox).perform
instagram_inbox.reload
contact_inbox.reload
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
end
it 'creates a new conversation if last conversation is resolved' do
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :resolved,
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
inital_count = Conversation.count
message = dm_params[:entry][0]['messaging'][0]
contact_inbox
described_class.new(message, instagram_inbox).perform
instagram_inbox.reload
contact_inbox.reload
expect(instagram_inbox.conversations.last.id).not_to eq(existing_conversation.id)
expect(Conversation.count).to eq(inital_count + 1)
end
end
context 'when lock to single conversation is enabled' do
before do
instagram_inbox.update!(lock_to_single_conversation: true)
stub_request(:get, /graph.facebook.com/)
end
it 'creates a new conversation if existing conversation is not present' do
inital_count = Conversation.count
message = dm_params[:entry][0]['messaging'][0]
contact_inbox
described_class.new(message, instagram_inbox).perform
instagram_inbox.reload
contact_inbox.reload
expect(instagram_inbox.conversations.count).to eq(1)
expect(Conversation.count).to eq(inital_count + 1)
end
it 'reopens last conversation if last conversation is resolved' do
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id, status: :resolved,
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
inital_count = Conversation.count
message = dm_params[:entry][0]['messaging'][0]
contact_inbox
described_class.new(message, instagram_inbox).perform
instagram_inbox.reload
contact_inbox.reload
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
expect(Conversation.count).to eq(inital_count)
end
end
end
@@ -11,6 +11,21 @@ FactoryBot.define do
initialize_with { attributes }
end
factory :mocked_message_text, class: Hash do
transient do
sender_id { '3383290475046708' }
end
initialize_with do
{ messaging: { sender: { id: sender_id },
recipient: { id: '117172741761305' },
message: { mid: 'm_KXGKDUpO6xbVdAmZFBVpzU1AhKVJdAIUnUH4cwkvb_K3iZsWhowDRyJ_DcowEpJjncaBwdCIoRrixvCbbO1PcA',
text: 'facebook message' } } }
end
# initialize_with { attributes }
end
factory :message_deliveries, class: Hash do
messaging do
{ sender: { id: '3383290475046708' },