iachat/app/services/contact_inbox/source_id_service.rb
Pranav d355801555
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.
2025-03-21 18:04:46 -07:00

56 lines
1.4 KiB
Ruby

class ContactInbox::SourceIdService
pattr_initialize [:contact!, :channel_type!, { medium: nil }]
def generate
case channel_type
when 'Channel::TwilioSms'
twilio_source_id
when 'Channel::Whatsapp'
wa_source_id
when 'Channel::Email'
email_source_id
when 'Channel::Sms'
phone_source_id
when 'Channel::Api', 'Channel::WebWidget'
SecureRandom.uuid
else
raise ArgumentError, "Unsupported operation for this channel: #{channel_type}"
end
end
private
def email_source_id
raise ArgumentError, 'contact email required' unless contact.email
contact.email
end
def phone_source_id
raise ArgumentError, 'contact phone number required' unless contact.phone_number
contact.phone_number
end
def wa_source_id
raise ArgumentError, 'contact phone number required' unless contact.phone_number
# whatsapp doesn't want the + in e164 format
contact.phone_number.delete('+').to_s
end
def twilio_source_id
raise ArgumentError, 'contact phone number required' unless contact.phone_number
raise ArgumentError, 'medium required for Twilio channel' if medium.blank?
case medium
when 'sms'
contact.phone_number
when 'whatsapp'
"whatsapp:#{contact.phone_number}"
else
raise ArgumentError, "Unsupported Twilio medium: #{medium}"
end
end
end