feat: Team APIs (#1654)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2021-01-17 23:56:56 +05:30
committed by GitHub
parent dd90e24d02
commit a0c33254e7
29 changed files with 523 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
class Api::V1::Accounts::TeamMembersController < Api::V1::Accounts::BaseController
before_action :fetch_team
before_action :check_authorization
def index
@team_members = @team.team_members.map(&:user)
end
def create
record = @team.team_members.find_or_create_by(user_id: params[:user_id])
@team_member = record.user
end
def destroy
@team.team_members.find_by(user_id: params[:user_id])&.destroy
head :ok
end
private
def fetch_team
@team = Current.account.teams.find(params[:team_id])
end
end

View File

@@ -0,0 +1,34 @@
class Api::V1::Accounts::TeamsController < Api::V1::Accounts::BaseController
before_action :fetch_team, only: [:show, :update, :destroy]
before_action :check_authorization
def index
@teams = Current.account.teams
end
def show; end
def create
@team = Current.account.teams.new(team_params)
@team.save!
end
def update
@team.update!(team_params)
end
def destroy
@team.destroy
head :ok
end
private
def fetch_team
@team = Current.account.teams.find(params[:id])
end
def team_params
params.require(:team).permit(:name, :description, :allow_auto_assign)
end
end

View File

@@ -55,6 +55,7 @@ class Account < ApplicationRecord
has_many :kbase_portals, dependent: :destroy, class_name: '::Kbase::Portal'
has_many :kbase_categories, dependent: :destroy, class_name: '::Kbase::Category'
has_many :kbase_articles, dependent: :destroy, class_name: '::Kbase::Article'
has_many :teams, dependent: :destroy
has_flags ACCOUNT_SETTINGS_FLAGS.merge(column: 'settings_flags').merge(DEFAULT_QUERY_SETTING)
enum locale: LANGUAGES_CONFIG.map { |key, val| [val[:iso_639_1_code], key] }.to_h

View File

@@ -19,16 +19,19 @@
# contact_inbox_id :bigint
# display_id :integer not null
# inbox_id :integer not null
# team_id :bigint
#
# Indexes
#
# index_conversations_on_account_id (account_id)
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
# index_conversations_on_contact_inbox_id (contact_inbox_id)
# index_conversations_on_team_id (team_id)
#
# Foreign Keys
#
# fk_rails_... (contact_inbox_id => contact_inboxes.id)
# fk_rails_... (team_id => teams.id)
#
class Conversation < ApplicationRecord
@@ -49,6 +52,7 @@ class Conversation < ApplicationRecord
belongs_to :assignee, class_name: 'User', optional: true
belongs_to :contact
belongs_to :contact_inbox
belongs_to :team, optional: true
has_many :messages, dependent: :destroy, autosave: true

25
app/models/team.rb Normal file
View File

@@ -0,0 +1,25 @@
# == 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
#
# index_teams_on_account_id (account_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
#
class Team < ApplicationRecord
belongs_to :account
has_many :team_members, dependent: :destroy
has_many :conversations, dependent: :nullify
end

24
app/models/team_member.rb Normal file
View File

@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: team_members
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# team_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_team_members_on_team_id (team_id)
# index_team_members_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (team_id => teams.id)
# fk_rails_... (user_id => users.id)
#
class TeamMember < ApplicationRecord
belongs_to :user
belongs_to :team
end

View File

@@ -78,6 +78,8 @@ class User < ApplicationRecord
has_many :notifications, dependent: :destroy
has_many :notification_settings, dependent: :destroy
has_many :notification_subscriptions, dependent: :destroy
has_many :team_members, dependent: :destroy
has_many :teams, through: :team_members
before_validation :set_password_and_uid, on: :create

View File

@@ -0,0 +1,13 @@
class TeamMemberPolicy < ApplicationPolicy
def index?
true
end
def create?
@account_user.administrator?
end
def destroy?
@account_user.administrator?
end
end

View File

@@ -0,0 +1,21 @@
class TeamPolicy < ApplicationPolicy
def index?
true
end
def update?
@account_user.administrator?
end
def show?
true
end
def create?
@account_user.administrator?
end
def destroy?
@account_user.administrator?
end
end

View File

@@ -1,5 +1,5 @@
json.payload do
json.array! @agents do |agent|
json.partial! 'api/v1/models/user.json.jbuilder', resource: agent
json.partial! 'api/v1/models/agent.json.jbuilder', resource: agent
end
end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/agent.json.jbuilder', resource: @team_member

View File

@@ -0,0 +1,3 @@
json.array! @team_members do |team_member|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: team_member
end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/team.json.jbuilder', resource: @team

View File

@@ -0,0 +1,3 @@
json.array! @teams do |team|
json.partial! 'api/v1/models/team.json.jbuilder', resource: team
end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/team.json.jbuilder', resource: @team

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/team.json.jbuilder', resource: @team

View File

@@ -0,0 +1,5 @@
json.id resource.id
json.name resource.name
json.description resource.description
json.allow_auto_assign resource.allow_auto_assign
json.account_id resource.account.id