fix: Do not allow sending messages if merged contact has a duplicate session (#11152)

In this PR https://github.com/chatwoot/chatwoot/pull/11139, if there is
an attempt to create a duplication session for the contact in the same
inbox, we will anonymize the old session.

This PR would prevent sending messages to the older sessions. The
support agents will have to create a new conversation to continue
messages with customer.
This commit is contained in:
Pranav
2025-03-21 18:04:46 -07:00
committed by GitHub
parent 950d9f50a5
commit d355801555
12 changed files with 260 additions and 72 deletions

View File

@@ -12,8 +12,10 @@ class Base::SendOnChannelService
def perform
validate_target_channel
return unless outgoing_message?
return if invalid_message?
return if invalid_source_id?
perform_reply
end
@@ -49,6 +51,29 @@ class Base::SendOnChannelService
message.private? || outgoing_message_originated_from_channel?
end
def invalid_source_id?
return false unless channels_to_validate?
return false if contact_inbox.source_id == expected_source_id
message.update!(status: :failed, external_error: I18n.t('errors.channel_service.invalid_source_id'))
true
end
def expected_source_id
ContactInbox::SourceIdService.new(
contact: contact,
channel_type: inbox.channel_type,
medium: inbox.channel.try(:medium)
).generate
rescue ArgumentError
nil
end
def channels_to_validate?
inbox.sms? || inbox.whatsapp? || inbox.email? || inbox.twilio?
end
def validate_target_channel
raise 'Invalid channel service was called' if inbox.channel.class != channel_class
end