chore: refactor user
This commit is contained in:
parent
69a3416d46
commit
90cefe9fbb
3 changed files with 54 additions and 51 deletions
|
@ -12,7 +12,6 @@ Metrics/ClassLength:
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'app/models/conversation.rb'
|
- 'app/models/conversation.rb'
|
||||||
- 'app/models/contact.rb'
|
- 'app/models/contact.rb'
|
||||||
- 'app/models/user.rb'
|
|
||||||
- 'app/mailers/conversation_reply_mailer.rb'
|
- 'app/mailers/conversation_reply_mailer.rb'
|
||||||
- 'app/models/message.rb'
|
- 'app/models/message.rb'
|
||||||
- 'app/builders/messages/facebook/message_builder.rb'
|
- 'app/builders/messages/facebook/message_builder.rb'
|
||||||
|
|
53
app/models/concerns/user_attribute_helpers.rb
Normal file
53
app/models/concerns/user_attribute_helpers.rb
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
module UserAttributeHelpers
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
def available_name
|
||||||
|
self[:display_name].presence || name
|
||||||
|
end
|
||||||
|
|
||||||
|
def availability_status
|
||||||
|
current_account_user&.availability_status
|
||||||
|
end
|
||||||
|
|
||||||
|
def auto_offline
|
||||||
|
current_account_user&.auto_offline
|
||||||
|
end
|
||||||
|
|
||||||
|
def inviter
|
||||||
|
current_account_user&.inviter
|
||||||
|
end
|
||||||
|
|
||||||
|
def active_account_user
|
||||||
|
account_users.order(active_at: :desc)&.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_account_user
|
||||||
|
# We want to avoid subsequent queries in case where the association is preloaded.
|
||||||
|
# using where here will trigger n+1 queries.
|
||||||
|
account_users.find { |ac_usr| ac_usr.account_id == Current.account.id } if Current.account
|
||||||
|
end
|
||||||
|
|
||||||
|
def account
|
||||||
|
current_account_user&.account
|
||||||
|
end
|
||||||
|
|
||||||
|
def administrator?
|
||||||
|
current_account_user&.administrator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def agent?
|
||||||
|
current_account_user&.agent?
|
||||||
|
end
|
||||||
|
|
||||||
|
def role
|
||||||
|
current_account_user&.role
|
||||||
|
end
|
||||||
|
|
||||||
|
# Used internally for Chatwoot in Chatwoot
|
||||||
|
def hmac_identifier
|
||||||
|
hmac_key = GlobalConfig.get('CHATWOOT_INBOX_HMAC_KEY')['CHATWOOT_INBOX_HMAC_KEY']
|
||||||
|
return OpenSSL::HMAC.hexdigest('sha256', hmac_key, email) if hmac_key.present?
|
||||||
|
|
||||||
|
''
|
||||||
|
end
|
||||||
|
end
|
|
@ -48,6 +48,7 @@ class User < ApplicationRecord
|
||||||
include Rails.application.routes.url_helpers
|
include Rails.application.routes.url_helpers
|
||||||
include Reportable
|
include Reportable
|
||||||
include SsoAuthenticatable
|
include SsoAuthenticatable
|
||||||
|
include UserAttributeHelpers
|
||||||
|
|
||||||
devise :database_authenticatable,
|
devise :database_authenticatable,
|
||||||
:registerable,
|
:registerable,
|
||||||
|
@ -112,60 +113,10 @@ class User < ApplicationRecord
|
||||||
self.uid = email
|
self.uid = email
|
||||||
end
|
end
|
||||||
|
|
||||||
def active_account_user
|
|
||||||
account_users.order(active_at: :desc)&.first
|
|
||||||
end
|
|
||||||
|
|
||||||
def current_account_user
|
|
||||||
# We want to avoid subsequent queries in case where the association is preloaded.
|
|
||||||
# using where here will trigger n+1 queries.
|
|
||||||
account_users.find { |ac_usr| ac_usr.account_id == Current.account.id } if Current.account
|
|
||||||
end
|
|
||||||
|
|
||||||
def available_name
|
|
||||||
self[:display_name].presence || name
|
|
||||||
end
|
|
||||||
|
|
||||||
# Used internally for Chatwoot in Chatwoot
|
|
||||||
def hmac_identifier
|
|
||||||
hmac_key = GlobalConfig.get('CHATWOOT_INBOX_HMAC_KEY')['CHATWOOT_INBOX_HMAC_KEY']
|
|
||||||
return OpenSSL::HMAC.hexdigest('sha256', hmac_key, email) if hmac_key.present?
|
|
||||||
|
|
||||||
''
|
|
||||||
end
|
|
||||||
|
|
||||||
def account
|
|
||||||
current_account_user&.account
|
|
||||||
end
|
|
||||||
|
|
||||||
def assigned_inboxes
|
def assigned_inboxes
|
||||||
administrator? ? Current.account.inboxes : inboxes.where(account_id: Current.account.id)
|
administrator? ? Current.account.inboxes : inboxes.where(account_id: Current.account.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def administrator?
|
|
||||||
current_account_user&.administrator?
|
|
||||||
end
|
|
||||||
|
|
||||||
def agent?
|
|
||||||
current_account_user&.agent?
|
|
||||||
end
|
|
||||||
|
|
||||||
def role
|
|
||||||
current_account_user&.role
|
|
||||||
end
|
|
||||||
|
|
||||||
def availability_status
|
|
||||||
current_account_user&.availability_status
|
|
||||||
end
|
|
||||||
|
|
||||||
def auto_offline
|
|
||||||
current_account_user&.auto_offline
|
|
||||||
end
|
|
||||||
|
|
||||||
def inviter
|
|
||||||
current_account_user&.inviter
|
|
||||||
end
|
|
||||||
|
|
||||||
def serializable_hash(options = nil)
|
def serializable_hash(options = nil)
|
||||||
super(options).merge(confirmed: confirmed?)
|
super(options).merge(confirmed: confirmed?)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue