diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index b6ba7846b..cdf16dc9f 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -41,9 +41,13 @@ Assumptions end + def contact + @contact ||= @inbox.contacts.find_by(source_id: @sender_id) + end + def build_contact - if !@inbox.contacts.exists?(source_id: @sender_id) - contact = @inbox.contacts.create!(contact_params) + if contact.nil? + @contact = @inbox.contacts.create!(contact_params) end end @@ -99,7 +103,7 @@ Assumptions { account_id: @inbox.account_id, inbox_id: @inbox.id, - sender_id: @sender_id + sender_id: contact.id } end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 47b00cc2f..6a8038e01 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -13,7 +13,7 @@ class Conversation < ApplicationRecord belongs_to :account belongs_to :inbox belongs_to :assignee, class_name: 'User', optional: true - belongs_to :sender, class_name: 'Contact', primary_key: :source_id + belongs_to :sender, class_name: 'Contact' has_many :messages, dependent: :destroy, autosave: true diff --git a/lib/integrations/facebook/delivery_status.rb b/lib/integrations/facebook/delivery_status.rb index f1f767586..d236c4e8e 100644 --- a/lib/integrations/facebook/delivery_status.rb +++ b/lib/integrations/facebook/delivery_status.rb @@ -14,8 +14,12 @@ class Integrations::Facebook::DeliveryStatus @params.sender['id'] end + def contact_id + @contact ||= ::Contact.find_by(source_id: sender_id) + end + def conversation - @conversation ||= Conversation.find_by(sender_id: sender_id) + @conversation ||= ::Conversation.find_by(sender_id: contact_id) end def update_message_status