2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: accounts
|
|
|
|
#
|
2020-11-01 07:23:25 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# auto_resolve_duration :integer
|
|
|
|
# domain :string(100)
|
|
|
|
# feature_flags :integer default(0), not null
|
|
|
|
# locale :integer default("en")
|
|
|
|
# name :string not null
|
|
|
|
# settings_flags :integer default(0), not null
|
|
|
|
# support_email :string(100)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Account < ApplicationRecord
|
2020-04-30 14:50:26 +00:00
|
|
|
# used for single column multi flags
|
|
|
|
include FlagShihTzu
|
2020-03-18 11:23:35 +00:00
|
|
|
include Reportable
|
2020-05-10 17:10:36 +00:00
|
|
|
include Featurable
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
DEFAULT_QUERY_SETTING = {
|
|
|
|
flag_query_mode: :bit_operator
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
ACCOUNT_SETTINGS_FLAGS = {
|
2020-07-19 17:38:07 +00:00
|
|
|
1 => :custom_email_domain_enabled
|
2020-04-30 14:50:26 +00:00
|
|
|
}.freeze
|
|
|
|
|
2019-12-03 04:39:45 +00:00
|
|
|
validates :name, presence: true
|
2020-11-01 07:23:25 +00:00
|
|
|
validates :auto_resolve_duration, numericality: { greater_than_or_equal_to: 1, allow_nil: true }
|
2019-12-03 04:39:45 +00:00
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
has_many :account_users, dependent: :destroy
|
2020-03-10 18:32:15 +00:00
|
|
|
has_many :agent_bot_inboxes, dependent: :destroy
|
2021-06-01 17:04:25 +00:00
|
|
|
has_many :agent_bots, dependent: :destroy
|
2021-02-03 13:54:51 +00:00
|
|
|
has_many :data_imports, dependent: :destroy
|
2020-03-07 06:48:16 +00:00
|
|
|
has_many :users, through: :account_users
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :inboxes, dependent: :destroy
|
2021-04-29 16:53:32 +00:00
|
|
|
has_many :campaigns, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :conversations, dependent: :destroy
|
2020-03-18 11:23:35 +00:00
|
|
|
has_many :messages, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :contacts, dependent: :destroy
|
2019-10-20 19:10:18 +00:00
|
|
|
has_many :facebook_pages, dependent: :destroy, class_name: '::Channel::FacebookPage'
|
2020-04-05 16:41:27 +00:00
|
|
|
has_many :telegram_bots, dependent: :destroy
|
|
|
|
has_many :twilio_sms, dependent: :destroy, class_name: '::Channel::TwilioSms'
|
2020-02-11 08:57:38 +00:00
|
|
|
has_many :twitter_profiles, dependent: :destroy, class_name: '::Channel::TwitterProfile'
|
2019-10-29 07:20:54 +00:00
|
|
|
has_many :web_widgets, dependent: :destroy, class_name: '::Channel::WebWidget'
|
2020-07-21 06:45:24 +00:00
|
|
|
has_many :email_channels, dependent: :destroy, class_name: '::Channel::Email'
|
|
|
|
has_many :api_channels, dependent: :destroy, class_name: '::Channel::Api'
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :canned_responses, dependent: :destroy
|
2020-02-14 17:49:17 +00:00
|
|
|
has_many :webhooks, dependent: :destroy
|
2020-06-07 05:47:13 +00:00
|
|
|
has_many :labels, dependent: :destroy
|
2020-02-29 15:11:09 +00:00
|
|
|
has_many :notification_settings, dependent: :destroy
|
2020-06-12 17:42:47 +00:00
|
|
|
has_many :hooks, dependent: :destroy, class_name: 'Integrations::Hook'
|
2020-10-31 18:44:33 +00:00
|
|
|
has_many :working_hours, dependent: :destroy
|
2020-09-25 21:02:34 +00:00
|
|
|
has_many :kbase_portals, dependent: :destroy, class_name: '::Kbase::Portal'
|
|
|
|
has_many :kbase_categories, dependent: :destroy, class_name: '::Kbase::Category'
|
|
|
|
has_many :kbase_articles, dependent: :destroy, class_name: '::Kbase::Article'
|
2021-01-17 18:26:56 +00:00
|
|
|
has_many :teams, dependent: :destroy
|
2020-04-30 14:50:26 +00:00
|
|
|
has_flags ACCOUNT_SETTINGS_FLAGS.merge(column: 'settings_flags').merge(DEFAULT_QUERY_SETTING)
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-06 16:47:07 +00:00
|
|
|
enum locale: LANGUAGES_CONFIG.map { |key, val| [val[:iso_639_1_code], key] }.to_h
|
2020-03-29 06:46:31 +00:00
|
|
|
|
2020-08-25 17:34:02 +00:00
|
|
|
after_create_commit :notify_creation
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-18 08:17:51 +00:00
|
|
|
def agents
|
|
|
|
users.where(account_users: { role: :agent })
|
|
|
|
end
|
|
|
|
|
|
|
|
def administrators
|
|
|
|
users.where(account_users: { role: :administrator })
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_conversation_tags
|
2019-10-20 08:47:26 +00:00
|
|
|
# returns array of tags
|
2019-08-14 09:48:44 +00:00
|
|
|
conversation_ids = conversations.pluck(:id)
|
|
|
|
ActsAsTaggableOn::Tagging.includes(:tag)
|
2019-10-20 08:47:26 +00:00
|
|
|
.where(context: 'labels',
|
|
|
|
taggable_type: 'Conversation',
|
|
|
|
taggable_id: conversation_ids)
|
|
|
|
.map { |_| _.tag.name }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-03-04 08:29:59 +00:00
|
|
|
def inbound_email_domain
|
|
|
|
domain || GlobalConfig.get('MAILER_INBOUND_EMAIL_DOMAIN')['MAILER_INBOUND_EMAIL_DOMAIN'] || ENV.fetch('MAILER_INBOUND_EMAIL_DOMAIN', false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def support_email
|
2021-03-12 10:07:06 +00:00
|
|
|
super || GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot <accounts@chatwoot.com>')
|
2021-03-04 08:29:59 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def notify_creation
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(ACCOUNT_CREATED, Time.zone.now, account: self)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2021-01-05 14:37:04 +00:00
|
|
|
|
|
|
|
trigger.after(:insert).for_each(:row) do
|
|
|
|
"execute format('create sequence IF NOT EXISTS conv_dpid_seq_%s', NEW.id);"
|
|
|
|
end
|
2021-04-29 16:53:32 +00:00
|
|
|
|
|
|
|
trigger.name('camp_dpid_before_insert').after(:insert).for_each(:row) do
|
|
|
|
"execute format('create sequence IF NOT EXISTS camp_dpid_seq_%s', NEW.id);"
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|