Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/internal_api/v1/team_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# frozen_string_literal: true

class InternalApi::V1::TeamController < InternalApi::V1::ApplicationController
helper ApplicationHelper

def index
authorize :index, policy_class: TeamPolicy
query = current_company.users.includes([:avatar_attachment, :roles]).ransack(params[:q])
team = query.result(distinct: true)
render :index, locals: { team: }, status: :ok
end

def destroy
authorize :team
company_user.discard!
Expand Down
22 changes: 22 additions & 0 deletions app/views/internal_api/v1/team/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

json.key_format! camelize: :lower
json.deep_format_keys!

def status(member)
if current_user.has_role?(:owner, current_company) || current_user.has_role?(:admin, current_company)
if member.unconfirmed_email?
I18n.t("team.reconfirmation")
elsif member.created_by_invite? && !member.invitation_accepted? && !member.has_role?(:owner, current_company)
I18n.t("team.invitation")
end
end
end

json.team team do |member|
json.profile_picture user_avatar(member)
json.name member.full_name
json.email member.email
json.role member.primary_role
json.status status(member)
end
4 changes: 2 additions & 2 deletions config/routes/internal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
resources :providers, only: [:index, :update]
end

resources :team, only: [:index, :destroy]

resource :profile, only: [:update, :show], controller: "profile" do
delete "/remove_avatar", to: "profile#remove_avatar"
end

resources :team, only: :destroy
end
end
5 changes: 5 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
user.avatar.attach(io: File.open(file_path), filename: file_name, content_type: "image/png")
end
end

trait :with_pending_invitation do
invitation_token { Faker::String.random(length: 10) }
invitation_created_at { Time.current }
end
Comment on lines +20 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

end
end
87 changes: 87 additions & 0 deletions spec/requests/internal_api/v1/team/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "InternalApi::V1::Team#index", type: :request do
let(:company) { create(:company) }
let(:user) { create(:user, :with_avatar, current_workspace_id: company.id) }
let(:user2) { create(:user, :with_pending_invitation, current_workspace_id: company.id) }

before do
create(:company_user, company:, user:)
create(:company_user, company:, user: user2)
user.add_role :admin, company
user2.add_role :employee, company
end

context "when user is admin" do
before do
sign_in user
send_request :get, internal_api_v1_team_index_path
end

it "returns http success" do
expect(response).to have_http_status(:ok)
end

it "checks if profile picture is there with each team member" do
expect(json_response["team"].first["profilePicture"]).to eq(JSON.parse(user.avatar.to_json))
expect(json_response["team"].last["profilePicture"]).to include("/assets/avatar")
end

it "checks if correct team members data is returned" do
actual_members_data = json_response["team"].map do |member|
member.slice("name", "email", "role", "status")
end
expected_members_data =
[{
"name" => user.full_name, "email" => user.email, "role" => "admin", "status" => nil
},
{
"name" => user2.full_name, "email" => user2.email, "role" => "employee", "status" => I18n.t("team.invitation")
}]
expect(actual_members_data).to eq(expected_members_data)
end
end

context "when user is employee" do
let(:user3) { create(:user, current_workspace_id: company.id) }

before do
create(:company_user, company:, user: user3)
user3.add_role :employee, company
sign_in user3
send_request :get, internal_api_v1_team_index_path
end

it "is permitted to access Team#index page" do
expect(response).to have_http_status(:ok)
end

it "checks if correct team members data is returned" do
actual_members_data = json_response["team"].map do |member|
member.slice("name", "email", "role", "status")
end
expected_members_data =
[{
"name" => user.full_name, "email" => user.email, "role" => "admin", "status" => nil
},
{
"name" => user2.full_name, "email" => user2.email, "role" => "employee", "status" => nil
},
{
"name" => user3.full_name, "email" => user3.email, "role" => "employee", "status" => nil
}
]
expect(actual_members_data).to eq(expected_members_data)
end
end

context "when unauthenticated" do
it "is not permitted to view team members" do
send_request :get, internal_api_v1_team_index_path
expect(response).to have_http_status(:unauthorized)
expect(json_response["error"]).to eq("You need to sign in or sign up before continuing.")
end
end
end