refactored to independent methods

This commit is contained in:
Jaideep Guntupalli
2024-03-23 11:21:34 +05:30
parent 939f43aeb4
commit 3abd839c1b
2 changed files with 33 additions and 29 deletions
@@ -53,24 +53,25 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder
end
def conversation
@conversation ||= begin
lock_to_single_conversation = @inbox.lock_to_single_conversation
@conversation ||= set_conversation_based_on_inbox_config
end
if lock_to_single_conversation
Conversation.find_by(conversation_params) || build_conversation
else
# If lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
last_conversation = Conversation.where(conversation_params).order(created_at: :desc).first
if last_conversation.nil? || last_conversation.resolved?
build_conversation
else
last_conversation
end
end
def set_conversation_based_on_inbox_config
if @inbox.lock_to_single_conversation
Conversation.find_by(conversation_params) || 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
Conversation.create!(conversation_params.merge(
contact_inbox_id: @contact_inbox.id
@@ -69,23 +69,26 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder
end
def conversation
@conversation ||= begin
lock_to_single_conversation = @inbox.lock_to_single_conversation
@conversation ||= set_conversation_based_on_inbox_config
end
if lock_to_single_conversation
Conversation.where(conversation_params)
.find_by("additional_attributes ->> 'type' = 'instagram_direct_message'") || build_conversation
else
# 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("additional_attributes ->> 'type' = 'instagram_direct_message'").order(created_at: :desc).first
def set_conversation_based_on_inbox_config
if @inbox.lock_to_single_conversation
Conversation.where(conversation_params)
.find_by("additional_attributes ->> 'type' = 'instagram_direct_message'") || build_conversation
else
find_or_build_for_multiple_conversations
end
end
if last_conversation.nil? || last_conversation.resolved?
build_conversation
else
last_conversation
end
end
def find_or_build_for_multiple_conversations
last_conversation = Conversation.where(conversation_params)
.where("additional_attributes ->> 'type' = 'instagram_direct_message'").order(created_at: :desc).first
if last_conversation.nil? || last_conversation.resolved?
build_conversation
else
last_conversation
end
end