2019-09-02 10:52:54 +00:00
|
|
|
# frozen_string_literal: true
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: inboxes
|
|
|
|
#
|
2022-01-11 08:32:03 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# allow_messages_after_resolved :boolean default(TRUE)
|
2022-06-13 14:48:38 +00:00
|
|
|
# auto_assignment_config :jsonb
|
2022-01-11 08:32:03 +00:00
|
|
|
# channel_type :string
|
|
|
|
# csat_survey_enabled :boolean default(FALSE)
|
|
|
|
# email_address :string
|
|
|
|
# enable_auto_assignment :boolean default(TRUE)
|
|
|
|
# enable_email_collect :boolean default(TRUE)
|
|
|
|
# greeting_enabled :boolean default(FALSE)
|
|
|
|
# greeting_message :string
|
2022-11-25 10:01:04 +00:00
|
|
|
# lock_to_single_conversation :boolean default(FALSE), not null
|
2022-01-11 08:32:03 +00:00
|
|
|
# name :string not null
|
|
|
|
# out_of_office_message :string
|
|
|
|
# timezone :string default("UTC")
|
|
|
|
# working_hours_enabled :boolean default(FALSE)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# channel_id :integer not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_inboxes_on_account_id (account_id)
|
|
|
|
#
|
|
|
|
|
2019-09-02 10:52:54 +00:00
|
|
|
class Inbox < ApplicationRecord
|
2020-03-18 11:23:35 +00:00
|
|
|
include Reportable
|
2020-04-07 04:49:19 +00:00
|
|
|
include Avatarable
|
2020-10-31 18:44:33 +00:00
|
|
|
include OutOfOffisable
|
2020-03-18 11:23:35 +00:00
|
|
|
|
2022-06-30 05:29:37 +00:00
|
|
|
# Not allowing characters:
|
2022-06-23 15:06:46 +00:00
|
|
|
validates :name, presence: true
|
2022-06-30 05:29:37 +00:00
|
|
|
validates :name, if: :check_channel_type?, format: { with: %r{^^\b[^/\\<>@]*\b$}, multiline: true,
|
|
|
|
message: I18n.t('errors.inboxes.validations.name') }
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
2021-02-23 06:41:15 +00:00
|
|
|
validates :timezone, inclusion: { in: TZInfo::Timezone.all_identifiers }
|
2022-06-13 14:48:38 +00:00
|
|
|
validate :ensure_valid_max_assignment_limit
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
2019-10-30 05:19:23 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
belongs_to :channel, polymorphic: true, dependent: :destroy
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :campaigns, dependent: :destroy_async
|
|
|
|
has_many :contact_inboxes, dependent: :destroy_async
|
2019-10-27 07:44:36 +00:00
|
|
|
has_many :contacts, through: :contact_inboxes
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :inbox_members, dependent: :destroy_async
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :members, through: :inbox_members, source: :user
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :conversations, dependent: :destroy_async
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :messages, through: :conversations
|
2020-03-05 20:13:12 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_one :agent_bot_inbox, dependent: :destroy_async
|
2020-04-07 05:11:18 +00:00
|
|
|
has_one :agent_bot, through: :agent_bot_inbox
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :webhooks, dependent: :destroy_async
|
|
|
|
has_many :hooks, dependent: :destroy_async, class_name: 'Integrations::Hook'
|
2020-03-05 20:13:12 +00:00
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_destroy :delete_round_robin_agents
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-08-22 16:50:22 +00:00
|
|
|
scope :order_by_name, -> { order('lower(name) ASC') }
|
2020-08-05 12:16:17 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def add_member(user_id)
|
|
|
|
member = inbox_members.new(user_id: user_id)
|
|
|
|
member.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member(user_id)
|
2021-09-14 06:25:02 +00:00
|
|
|
member = inbox_members.find_by!(user_id: user_id)
|
2019-08-14 09:48:44 +00:00
|
|
|
member.try(:destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def facebook?
|
2021-06-23 13:59:27 +00:00
|
|
|
channel_type == 'Channel::FacebookPage'
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2022-12-08 12:55:24 +00:00
|
|
|
def instagram?
|
|
|
|
facebook? && channel.instagram_id.present?
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def web_widget?
|
2021-06-23 13:59:27 +00:00
|
|
|
channel_type == 'Channel::WebWidget'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
|
2021-08-27 14:26:45 +00:00
|
|
|
def api?
|
|
|
|
channel_type == 'Channel::Api'
|
|
|
|
end
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
def email?
|
|
|
|
channel_type == 'Channel::Email'
|
|
|
|
end
|
|
|
|
|
2021-09-16 11:21:06 +00:00
|
|
|
def twilio?
|
|
|
|
channel_type == 'Channel::TwilioSms'
|
|
|
|
end
|
|
|
|
|
2021-12-15 18:54:50 +00:00
|
|
|
def twitter?
|
|
|
|
channel_type == 'Channel::TwitterProfile'
|
|
|
|
end
|
|
|
|
|
2022-06-07 12:03:33 +00:00
|
|
|
def whatsapp?
|
|
|
|
channel_type == 'Channel::Whatsapp'
|
|
|
|
end
|
|
|
|
|
2022-11-02 20:54:56 +00:00
|
|
|
def active_bot?
|
|
|
|
agent_bot_inbox&.active? || hooks.pluck(:app_id).include?('dialogflow')
|
|
|
|
end
|
|
|
|
|
2020-09-04 13:43:47 +00:00
|
|
|
def inbox_type
|
|
|
|
channel.name
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-14 17:14:53 +00:00
|
|
|
def callback_webhook_url
|
2021-09-10 20:01:17 +00:00
|
|
|
case channel_type
|
2021-09-14 17:14:53 +00:00
|
|
|
when 'Channel::TwilioSms'
|
2022-07-15 02:51:59 +00:00
|
|
|
"#{ENV.fetch('FRONTEND_URL', nil)}/twilio/callback"
|
2022-02-03 23:22:13 +00:00
|
|
|
when 'Channel::Sms'
|
2022-07-15 02:51:59 +00:00
|
|
|
"#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/sms/#{channel.phone_number.delete_prefix('+')}"
|
2021-09-10 20:01:17 +00:00
|
|
|
when 'Channel::Line'
|
2022-07-15 02:51:59 +00:00
|
|
|
"#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/line/#{channel.line_channel_id}"
|
2022-07-06 19:45:03 +00:00
|
|
|
when 'Channel::Whatsapp'
|
2022-07-15 02:51:59 +00:00
|
|
|
"#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/whatsapp/#{channel.phone_number}"
|
2021-09-10 20:01:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-13 14:48:38 +00:00
|
|
|
def member_ids_with_assignment_capacity
|
|
|
|
members.ids
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2022-06-13 14:48:38 +00:00
|
|
|
def ensure_valid_max_assignment_limit
|
|
|
|
# overridden in enterprise/app/models/enterprise/inbox.rb
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def delete_round_robin_agents
|
2022-08-16 11:28:23 +00:00
|
|
|
::AutoAssignment::InboxRoundRobinService.new(inbox: self).clear_queue
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2022-06-30 05:29:37 +00:00
|
|
|
|
|
|
|
def check_channel_type?
|
|
|
|
['Channel::Email', 'Channel::Api', 'Channel::WebWidget'].include?(channel_type)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2022-06-13 14:48:38 +00:00
|
|
|
|
|
|
|
Inbox.prepend_mod_with('Inbox')
|