Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort users by name on time tracking page dropdown #1885

Merged
merged 7 commits into from
Sep 3, 2024
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
29 changes: 29 additions & 0 deletions app/services/concerns/employee_fetching_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module EmployeeFetchingConcern
extend ActiveSupport::Concern

included do
def set_employees
@employees = admin? ? current_company_users : [current_user_data]
end

private

def current_company_users
current_company.employees_without_client_role.select(:id, :first_name, :last_name).order(
:first_name,
:last_name)
end

def current_user_data
OpenStruct.new(current_user.slice(:id, :first_name, :last_name))
end

def admin?
@admin ||= current_user.has_any_role?(
{ name: :owner, resource: current_company },
{ name: :admin, resource: current_company })
end
end
end
17 changes: 3 additions & 14 deletions app/services/time_tracking_index_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

class TimeTrackingIndexService
include EmployeeFetchingConcern

attr_reader :current_user, :user, :current_company, :entries, :from, :to, :year
attr_accessor :clients, :projects, :is_admin, :employees
attr_accessor :clients, :projects, :employees

def initialize(current_user:, user:, company:, from:, to:, year:)
@current_user = current_user
Expand All @@ -29,20 +31,11 @@ def process
private

def setup_data
set_is_admin
set_clients
set_projects
set_employees
end

def set_employees
@employees = is_admin ? current_company_users : [current_user]
end

def current_company_users
current_company.employees_without_client_role.select(:id, :first_name, :last_name)
end

def format_entries
formatted_timesheet_entries
group_timeoff_entries_by_leave_date
Expand All @@ -60,10 +53,6 @@ def formatted_timesheet_entries
@entries = TimesheetEntriesPresenter.new(timesheet_entries).group_snippets_by_work_date
end

def set_is_admin
@is_admin = current_user.has_role?(:owner, current_company) || current_user.has_role?(:admin, current_company)
end

def set_clients
@clients = ClientPolicy::Scope.new(current_user, current_company).resolve.includes(:projects, :addresses)
end
Expand Down
18 changes: 4 additions & 14 deletions app/services/timeoff_entries/index_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module TimeoffEntries
class IndexService < ApplicationService
include EmployeeFetchingConcern

attr_reader :current_company, :current_user, :user_id, :year, :previous_year
attr_accessor :leave_balance, :optional_timeoff_entries, :national_timeoff_entries

Expand All @@ -17,7 +19,7 @@ def initialize(current_user, current_company, user_id, year)
def process
{
timeoff_entries:,
employees:,
employees: set_employees,
leave_balance: process_leave_balance,
total_timeoff_entries_duration: timeoff_entries.sum(&:duration),
optional_timeoff_entries:,
Expand All @@ -37,18 +39,6 @@ def timeoff_entries
.distinct
end

def employees
@_employees ||= is_admin? ? current_company_users : [current_user]
end

def current_company_users
current_company.employees_without_client_role.select(:id, :first_name, :last_name)
end

def is_admin?
current_user.has_role?(:owner, current_company) || current_user.has_role?(:admin, current_company)
end

def process_leave_balance
calculate_leave_balance
calculate_holiday_balance
Expand Down Expand Up @@ -174,7 +164,7 @@ def calculate_total_duration(leave_type, passed_year)
end

def user_joined_date
employee_id = is_admin? ? user_id : current_user.id
employee_id = admin? ? user_id : current_user.id
user = User.find(employee_id)
user.joined_date_for_company(current_company)
end
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Application < Rails::Application
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w(assets tasks))
config.autoload_paths += %W(#{config.root}/app/services/concerns)
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved

# Configuration for the application, engines, and railties goes here.
#
Expand Down
Loading