Chatwoot/app/models/account.rb
Emil Shakirov 4768aca484 Refactor Conversation model (#134)
* Add Conversation factory with dependent factories

* Include FactoryBot methods in rspec config

* Add unit tests for public methods of Conversation model

* Move Current model into a separate file in lib folder

* Disable Metrics/BlockLength rule for db/migrate and spec folders

* Get rid of global $dispatcher variable

* Create Message#unread_since scope

* Refactor callback methods in Conversation model

* Create Conversations::EventDataPresenter

* Add translation keys for activity messages

* Add pry-rails gem

* Refactor Conversation#notify_status_change

* Add mock_redis for test env
2019-10-12 23:38:41 +05:30

62 lines
1.8 KiB
Ruby

class Account < ApplicationRecord
include Events::Types
has_many :users, dependent: :destroy
has_many :inboxes, dependent: :destroy
has_many :conversations, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :facebook_pages, dependent: :destroy
has_many :telegram_bots, dependent: :destroy
has_many :canned_responses, dependent: :destroy
has_one :subscription, dependent: :destroy
after_create :create_subscription
after_create :notify_creation
after_destroy :notify_deletion
def channel
# This should be unique for account
'test_channel'
end
def all_conversation_tags
#returns array of tags
conversation_ids = conversations.pluck(:id)
ActsAsTaggableOn::Tagging.includes(:tag)
.where(context: 'labels',
taggable_type: "Conversation",
taggable_id: conversation_ids )
.map {|_| _.tag.name}
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
private
def create_subscription
subscription = self.build_subscription
subscription.save
end
def notify_creation
Rails.configuration.dispatcher.dispatch(ACCOUNT_CREATED, Time.zone.now, account: self)
end
def notify_deletion
Rails.configuration.dispatcher.dispatch(ACCOUNT_DESTROYED, Time.zone.now, account: self)
end
end