2021-01-17 18:26:56 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: teams
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# allow_auto_assign :boolean default(TRUE)
|
|
|
|
# description :text
|
|
|
|
# name :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :bigint not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-01-29 07:04:52 +00:00
|
|
|
# index_teams_on_account_id (account_id)
|
|
|
|
# index_teams_on_name_and_account_id (name,account_id) UNIQUE
|
2021-01-17 18:26:56 +00:00
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
2021-12-19 04:17:07 +00:00
|
|
|
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
|
2021-01-17 18:26:56 +00:00
|
|
|
#
|
|
|
|
class Team < ApplicationRecord
|
|
|
|
belongs_to :account
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :team_members, dependent: :destroy_async
|
2021-02-09 13:51:31 +00:00
|
|
|
has_many :members, through: :team_members, source: :user
|
2021-01-17 18:26:56 +00:00
|
|
|
has_many :conversations, dependent: :nullify
|
2021-01-29 07:04:52 +00:00
|
|
|
|
|
|
|
validates :name,
|
|
|
|
presence: { message: 'must not be blank' },
|
|
|
|
uniqueness: { scope: :account_id }
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.name = name.downcase if attribute_present?('name')
|
|
|
|
end
|
2021-02-09 13:51:31 +00:00
|
|
|
|
|
|
|
def add_member(user_id)
|
|
|
|
team_members.find_or_create_by(user_id: user_id)&.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member(user_id)
|
2022-03-24 07:58:25 +00:00
|
|
|
team_members.find_by(user_id: user_id)&.destroy!
|
2021-02-09 13:51:31 +00:00
|
|
|
end
|
2021-09-27 15:42:08 +00:00
|
|
|
|
|
|
|
def messages
|
|
|
|
account.messages.where(conversation_id: conversations.pluck(:id))
|
|
|
|
end
|
|
|
|
|
2022-02-28 12:46:12 +00:00
|
|
|
def reporting_events
|
|
|
|
account.reporting_events.where(conversation_id: conversations.pluck(:id))
|
2021-09-27 15:42:08 +00:00
|
|
|
end
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|