Skip to content

Commit

Permalink
Code refactoring and reuse the common method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishant Samel committed May 20, 2024
1 parent dce2aa2 commit 6944d12
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
7 changes: 1 addition & 6 deletions app/controllers/internal_api/v1/employments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class InternalApi::V1::EmploymentsController < InternalApi::V1::ApplicationContr

def index
authorize Employment
render :index, locals: { users: users_with_not_client_role }, status: :ok
render :index, locals: { users: current_company.employees_without_client_role }, status: :ok
end

def show
Expand All @@ -28,9 +28,4 @@ def set_employment
def employment_params
params.require(:employment).permit(:designation, :employment_type, :joined_at, :resigned_at, :employee_id)
end

def users_with_not_client_role
users_with_client_role_ids = current_company.users.joins(:roles).where(roles: { name: "client" }).pluck(:id)
current_company.users.kept.where.not(id: users_with_client_role_ids)
end
end
10 changes: 9 additions & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def billable_clients
end

def employees_without_client_role
users.with_kept_employments.joins(:roles).where.not(roles: { name: "client" }).distinct
user_ids_with_only_client_role = users.with_kept_employments
.joins(:roles)
.group("users.id, roles.resource_id, roles.resource_type")
.having("COUNT(roles.id) = 1 AND MAX(roles.name) = 'client' \
AND roles.resource_id = #{id} \
AND roles.resource_type = 'Company'")
.pluck("users.id")

users.with_kept_employments.where.not(id: user_ids_with_only_client_role).distinct
end
end
13 changes: 1 addition & 12 deletions app/services/reports/time_entries/report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def filter_options
if get_filters
@_filter_options ||= {
clients: current_company.clients.includes([:logo_attachment]).order(:name),
team_members: users_not_client_role.order(:first_name),
team_members: current_company.employees_without_client_role.order(:first_name),
projects: current_company.projects.as_json(only: [:id, :name])
}
end
Expand Down Expand Up @@ -120,17 +120,6 @@ def active_time_entries
{ discarded_at: nil }
end

def users_not_client_role
user_ids_with_only_client_role = current_company.users
.joins(:roles)
.group("users.id, roles.resource_id, roles.resource_type")
.having("COUNT(roles.id) = 1 AND MAX(roles.name) = 'client' \
AND roles.resource_id = #{current_company.id} \
AND roles.resource_type = 'Company'")
.pluck("users.id")
current_company.users.where.not(id: user_ids_with_only_client_role).distinct
end

def pagination_details
{
pages: @reports.total_pages,
Expand Down

0 comments on commit 6944d12

Please sign in to comment.