2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: accounts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2019-12-03 04:39:45 +00:00
|
|
|
# name :string not null
|
2019-11-30 13:39:55 +00:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Account < ApplicationRecord
|
|
|
|
include Events::Types
|
2020-03-18 11:23:35 +00:00
|
|
|
include Reportable
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-12-03 04:39:45 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
|
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
|
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
|
|
|
|
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-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'
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :telegram_bots, dependent: :destroy
|
|
|
|
has_many :canned_responses, dependent: :destroy
|
2020-02-14 17:49:17 +00:00
|
|
|
has_many :webhooks, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
has_one :subscription, dependent: :destroy
|
2020-02-29 15:11:09 +00:00
|
|
|
has_many :notification_settings, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_create :create_subscription
|
|
|
|
after_create :notify_creation
|
|
|
|
after_destroy :notify_deletion
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def channel
|
|
|
|
# This should be unique for account
|
|
|
|
'test_channel'
|
|
|
|
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
|
|
|
|
|
|
|
|
def subscription_data
|
|
|
|
agents_count = users.count
|
|
|
|
per_agent_price = Plan.paid_plan.price
|
|
|
|
{
|
|
|
|
state: subscription.state,
|
|
|
|
expiry: subscription.expiry.to_i,
|
|
|
|
agents_count: agents_count,
|
|
|
|
per_agent_cost: per_agent_price,
|
|
|
|
total_cost: (per_agent_price * agents_count),
|
|
|
|
iframe_url: Subscription::ChargebeeService.hosted_page_url(self),
|
|
|
|
trial_expired: subscription.trial_expired?,
|
|
|
|
account_suspended: subscription.suspended?,
|
|
|
|
payment_source_added: subscription.payment_source_added
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_subscription
|
2019-10-20 08:47:26 +00:00
|
|
|
subscription = build_subscription
|
2019-08-14 09:48:44 +00:00
|
|
|
subscription.save
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def notify_deletion
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(ACCOUNT_DESTROYED, Time.zone.now, account: self)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|