Skip to content

Commit

Permalink
Fix query to fetch user_ids_with_only_client_role (#1837)
Browse files Browse the repository at this point in the history
* Fix query to fetch `user_ids_with_only_client_role`

* Code refactoring and reuse the common method

* Remove old/unused code

---------

Co-authored-by: Nishant Samel <nishant@saeloun.com>
  • Loading branch information
nisusam and Nishant Samel committed May 21, 2024
1 parent a518d19 commit d092d8a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 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
6 changes: 1 addition & 5 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ def recipient_email_not_changed
end
end

def users_not_with_client_role
company.employments.kept.joins(user: :roles).where.not(roles: { name: "client" })
end

def send_invitation_mail
user_already_exists = User.exists?(email: recipient_email)

company_details = {
name: company.name,
logo: company.company_logo,
employee_count: users_not_with_client_role.count
employee_count: company.employees_without_client_role.count
}

sender_details = {
Expand Down
8 changes: 1 addition & 7 deletions app/services/clients/index_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def process
{
client_details:,
total_minutes:,
overdue_outstanding_amount:,
users_not_in_client_members:
overdue_outstanding_amount:
}
end

Expand Down Expand Up @@ -59,10 +58,5 @@ def total_minutes
def overdue_outstanding_amount
current_company.overdue_and_outstanding_and_draft_amount
end

def users_not_in_client_members
users_with_client_role = current_company.users.includes(:roles).where(roles: { name: "client" })
users_with_client_role.where.not(id: current_company.client_members.pluck(:user_id))
end
end
end
7 changes: 1 addition & 6 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,11 +120,6 @@ def active_time_entries
{ discarded_at: nil }
end

def users_not_client_role
users_with_client_role_ids = current_company.users.joins(:roles).where(roles: { name: "client" }).pluck(:id)
current_company.users.where.not(id: users_with_client_role_ids)
end

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

0 comments on commit d092d8a

Please sign in to comment.