Chatwoot/app/services/contacts/contactable_inboxes_service.rb
Sojan Jose 45e43b0b89
feat: Contactable Inboxes API (#2101)
- Add endpoint which lists inboxes through which a contact can be contacted
- Conversation creation API auto-creates contact_inbox for specific channels [ Twilio, email, api]
- Ability to send the initial message payload along with the conversation creation
- Fixes #1678 ( issue saving additional attributes for conversation )
2021-04-15 15:13:01 +05:30

52 lines
1.6 KiB
Ruby

class Contacts::ContactableInboxesService
pattr_initialize [:contact!]
def get
account = contact.account
account.inboxes.map { |inbox| get_contactable_inbox(inbox) }.compact
end
private
def get_contactable_inbox(inbox)
return twilio_contactable_inbox(inbox) if inbox.channel_type == 'Channel::TwilioSms'
return email_contactable_inbox(inbox) if inbox.channel_type == 'Channel::Email'
return api_contactable_inbox(inbox) if inbox.channel_type == 'Channel::Api'
return website_contactable_inbox(inbox) if inbox.channel_type == 'Channel::WebWidget'
nil
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
def twilio_contactable_inbox(inbox)
return unless @contact.phone_number
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