diff --git a/app/services/concerns/employee_fetching_concern.rb b/app/services/concerns/employee_fetching_concern.rb new file mode 100644 index 0000000000..3eca38dca9 --- /dev/null +++ b/app/services/concerns/employee_fetching_concern.rb @@ -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 diff --git a/app/services/time_tracking_index_service.rb b/app/services/time_tracking_index_service.rb index d0dd72dd28..e8ceeca577 100644 --- a/app/services/time_tracking_index_service.rb +++ b/app/services/time_tracking_index_service.rb @@ -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 @@ -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 @@ -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 diff --git a/app/services/timeoff_entries/index_service.rb b/app/services/timeoff_entries/index_service.rb index 0d1d9a0936..3a1ffad752 100644 --- a/app/services/timeoff_entries/index_service.rb +++ b/app/services/timeoff_entries/index_service.rb @@ -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 @@ -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:, @@ -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 @@ -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 diff --git a/config/application.rb b/config/application.rb index 45d52a6abb..c092714957 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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) # Configuration for the application, engines, and railties goes here. #