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
|
2022-07-18 19:03:06 +00:00
|
|
|
# custom_attributes :jsonb
|
2020-11-01 07:23:25 +00:00
|
|
|
# domain :string(100)
|
|
|
|
# feature_flags :integer default(0), not null
|
2021-12-09 06:37:48 +00:00
|
|
|
# limits :jsonb
|
2020-11-01 07:23:25 +00:00
|
|
|
# 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 = {
|
2021-07-28 14:06:51 +00:00
|
|
|
flag_query_mode: :bit_operator,
|
|
|
|
check_for_column: false
|
2020-04-30 14:50:26 +00:00
|
|
|
}.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
|
2022-01-19 07:46:21 +00:00
|
|
|
validates :auto_resolve_duration, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 999, allow_nil: true }
|
2022-02-02 22:21:17 +00:00
|
|
|
validates :name, length: { maximum: 255 }
|
2019-12-03 04:39:45 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :account_users, dependent: :destroy_async
|
|
|
|
has_many :agent_bot_inboxes, dependent: :destroy_async
|
|
|
|
has_many :agent_bots, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :api_channels, dependent: :destroy_async, class_name: '::Channel::Api'
|
2022-06-01 05:43:10 +00:00
|
|
|
has_many :articles, dependent: :destroy_async, class_name: '::Article'
|
|
|
|
has_many :automation_rules, dependent: :destroy
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :campaigns, dependent: :destroy_async
|
|
|
|
has_many :canned_responses, dependent: :destroy_async
|
2022-06-01 05:43:10 +00:00
|
|
|
has_many :categories, dependent: :destroy_async, class_name: '::Category'
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :contacts, dependent: :destroy_async
|
|
|
|
has_many :conversations, dependent: :destroy_async
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :csat_survey_responses, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :custom_attribute_definitions, dependent: :destroy_async
|
|
|
|
has_many :custom_filters, dependent: :destroy_async
|
2022-06-01 05:43:10 +00:00
|
|
|
has_many :dashboard_apps, dependent: :destroy
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :data_imports, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :email_channels, dependent: :destroy_async, class_name: '::Channel::Email'
|
|
|
|
has_many :facebook_pages, dependent: :destroy_async, class_name: '::Channel::FacebookPage'
|
|
|
|
has_many :hooks, dependent: :destroy_async, class_name: 'Integrations::Hook'
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :inboxes, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :labels, dependent: :destroy_async
|
|
|
|
has_many :line_channels, dependent: :destroy_async, class_name: '::Channel::Line'
|
|
|
|
has_many :mentions, dependent: :destroy_async
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :messages, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :notes, dependent: :destroy_async
|
|
|
|
has_many :notification_settings, dependent: :destroy_async
|
2022-06-01 05:43:10 +00:00
|
|
|
has_many :notifications, dependent: :destroy
|
|
|
|
has_many :portals, dependent: :destroy_async, class_name: '::Portal'
|
|
|
|
has_many :sms_channels, dependent: :destroy_async, class_name: '::Channel::Sms'
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :teams, dependent: :destroy_async
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :telegram_bots, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :telegram_channels, dependent: :destroy_async, class_name: '::Channel::Telegram'
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :twilio_sms, dependent: :destroy_async, class_name: '::Channel::TwilioSms'
|
|
|
|
has_many :twitter_profiles, dependent: :destroy_async, class_name: '::Channel::TwitterProfile'
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :users, through: :account_users
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :web_widgets, dependent: :destroy_async, class_name: '::Channel::WebWidget'
|
|
|
|
has_many :webhooks, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :whatsapp_channels, dependent: :destroy_async, class_name: '::Channel::Whatsapp'
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :working_hours, dependent: :destroy_async
|
2021-07-23 11:34:33 +00:00
|
|
|
|
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
|
|
|
|
2022-02-14 10:25:08 +00:00
|
|
|
before_validation :validate_limit_keys
|
2020-08-25 17:34:02 +00:00
|
|
|
after_create_commit :notify_creation
|
2022-03-24 08:03:15 +00:00
|
|
|
after_destroy :remove_account_sequences
|
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
|
2022-07-15 02:51:59 +00:00
|
|
|
super || ENV.fetch('MAILER_SENDER_EMAIL') { GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] }
|
2021-03-04 08:29:59 +00:00
|
|
|
end
|
|
|
|
|
2021-12-09 06:37:48 +00:00
|
|
|
def usage_limits
|
|
|
|
{
|
2022-02-14 10:25:08 +00:00
|
|
|
agents: ChatwootApp.max_limit.to_i,
|
|
|
|
inboxes: ChatwootApp.max_limit.to_i
|
2021-12-09 06:37:48 +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
|
2022-02-14 10:25:08 +00:00
|
|
|
|
|
|
|
def validate_limit_keys
|
|
|
|
# method overridden in enterprise module
|
|
|
|
end
|
2022-03-24 08:03:15 +00:00
|
|
|
|
|
|
|
def remove_account_sequences
|
|
|
|
ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS camp_dpid_seq_#{id}")
|
|
|
|
ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS conv_dpid_seq_#{id}")
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2022-06-13 14:48:38 +00:00
|
|
|
|
|
|
|
Account.prepend_mod_with('Account')
|