2021-04-15 09:43:01 +00:00
|
|
|
class Contacts::ContactableInboxesService
|
|
|
|
pattr_initialize [:contact!]
|
|
|
|
|
|
|
|
def get
|
|
|
|
account = contact.account
|
2021-08-03 14:41:52 +00:00
|
|
|
account.inboxes.filter_map { |inbox| get_contactable_inbox(inbox) }
|
2021-04-15 09:43:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_contactable_inbox(inbox)
|
2021-10-05 18:05:06 +00:00
|
|
|
case inbox.channel_type
|
|
|
|
when 'Channel::TwilioSms'
|
|
|
|
twilio_contactable_inbox(inbox)
|
|
|
|
when 'Channel::Whatsapp'
|
|
|
|
whatsapp_contactable_inbox(inbox)
|
2022-02-03 23:22:13 +00:00
|
|
|
when 'Channel::Sms'
|
|
|
|
sms_contactable_inbox(inbox)
|
2021-10-05 18:05:06 +00:00
|
|
|
when 'Channel::Email'
|
|
|
|
email_contactable_inbox(inbox)
|
|
|
|
when 'Channel::Api'
|
|
|
|
api_contactable_inbox(inbox)
|
|
|
|
when 'Channel::WebWidget'
|
|
|
|
website_contactable_inbox(inbox)
|
|
|
|
end
|
2021-04-15 09:43:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def website_contactable_inbox(inbox)
|
|
|
|
latest_contact_inbox = inbox.contact_inboxes.where(contact: @contact).last
|
|
|
|
return unless latest_contact_inbox
|
|
|
|
# FIXME : change this when multiple conversations comes in
|
|
|
|
return if latest_contact_inbox.conversations.present?
|
|
|
|
|
|
|
|
{ source_id: latest_contact_inbox.source_id, inbox: inbox }
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_contactable_inbox(inbox)
|
|
|
|
latest_contact_inbox = inbox.contact_inboxes.where(contact: @contact).last
|
|
|
|
source_id = latest_contact_inbox&.source_id || SecureRandom.uuid
|
|
|
|
|
|
|
|
{ source_id: source_id, inbox: inbox }
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_contactable_inbox(inbox)
|
|
|
|
return unless @contact.email
|
|
|
|
|
|
|
|
{ source_id: @contact.email, inbox: inbox }
|
|
|
|
end
|
|
|
|
|
2021-10-05 18:05:06 +00:00
|
|
|
def whatsapp_contactable_inbox(inbox)
|
|
|
|
return unless @contact.phone_number
|
|
|
|
|
|
|
|
# Remove the plus since thats the format 360 dialog uses
|
|
|
|
{ source_id: @contact.phone_number.delete('+'), inbox: inbox }
|
|
|
|
end
|
|
|
|
|
2022-02-03 23:22:13 +00:00
|
|
|
def sms_contactable_inbox(inbox)
|
|
|
|
return unless @contact.phone_number
|
|
|
|
|
|
|
|
{ source_id: @contact.phone_number, inbox: inbox }
|
|
|
|
end
|
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
def twilio_contactable_inbox(inbox)
|
2021-04-16 15:01:07 +00:00
|
|
|
return if @contact.phone_number.blank?
|
2021-04-15 09:43:01 +00:00
|
|
|
|
|
|
|
case inbox.channel.medium
|
|
|
|
when 'sms'
|
|
|
|
{ source_id: @contact.phone_number, inbox: inbox }
|
|
|
|
when 'whatsapp'
|
|
|
|
{ source_id: "whatsapp:#{@contact.phone_number}", inbox: inbox }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|