diff --git a/app/services/reports/time_entries/page_service.rb b/app/services/reports/time_entries/filter_service.rb similarity index 80% rename from app/services/reports/time_entries/page_service.rb rename to app/services/reports/time_entries/filter_service.rb index b733e8b196..eeb706d740 100644 --- a/app/services/reports/time_entries/page_service.rb +++ b/app/services/reports/time_entries/filter_service.rb @@ -1,12 +1,10 @@ # frozen_string_literal: true -class Reports::TimeEntries::PageService < ApplicationService +class Reports::TimeEntries::FilterService < ApplicationService include Pagy::Backend attr_reader :params, :page, :group_by, :current_company, :pagy_data, :es_filter, :reports - DEFAULT_ITEMS_PER_PAGE = 50 - def initialize(params, current_company) @params = params @page = params["page"] @@ -21,18 +19,6 @@ def process es_filter_for_pagination end - def pagination_details - { - page: pagy_data.page, - pages: pagy_data.pages, - first: pagy_data.page == 1, - prev: pagy_data.prev.nil? ? 0 : pagy_data.prev, - next: pagy_data.next, - last: pagy_data.last, - item: pagy_data.items - } - end - private def es_filter_for_pagination diff --git a/app/services/reports/time_entries/report_service.rb b/app/services/reports/time_entries/report_service.rb index 5183d9e34d..9907c07c1a 100644 --- a/app/services/reports/time_entries/report_service.rb +++ b/app/services/reports/time_entries/report_service.rb @@ -39,38 +39,30 @@ def fetch_reports default_filter = current_company_filter.merge(this_month_filter) where_clause = default_filter.merge(TimeEntries::Filters.process(params)) pagy_reports(where_clause) - end - - def pagy_reports(where_clause) - page_service = Reports::TimeEntries::PageService.new(params, current_company) - page_service.process - - search_results = search_timesheet_entries(where_clause.merge(page_service.es_filter)) - - pagination_details_for_es_query(search_results) - end - - def change_pagination(page) - page > 0 ? [page - 1, 1].max : page end - def pagination_details_for_es_query(search_result) - @pagy_data, paginated_data = pagy_searchkick( - search_result, - items: Reports::TimeEntries::PageService::DEFAULT_ITEMS_PER_PAGE, - page: params[:page] - ) - @reports = paginated_data - pagination_details + def pagy_reports(where_clause) + filter_service = Reports::TimeEntries::FilterService.new(params, current_company) + filter_service.process + @reports = search_timesheet_entries(where_clause.merge(filter_service.es_filter)) end def search_timesheet_entries(where_clause, page = nil) - TimesheetEntry.pagy_search( + order_fields = { + "project" => :project_name, + "client" => :client_name, + "team_member" => :user_name, + "default" => :client_name + } + order_field = order_fields[params[:group_by]] || order_fields["default"] + + TimesheetEntry.search( where: where_clause, - order: { work_date: :desc }, + order: [order_field => :asc, work_date: :desc], + per_page: 50, page: params[:page], - load: false - ) + load: false, + ) end def client_logos @@ -83,11 +75,11 @@ def client_logos def current_company_filter { project_id: current_company.project_ids } - end + end def this_month_filter { work_date: DateTime.current.beginning_of_month..DateTime.current.end_of_month } - end + end def users_not_client_role users_with_client_role_ids = current_company.users.joins(:roles).where(roles: { name: "client" }).pluck(:id) @@ -96,13 +88,11 @@ def users_not_client_role def pagination_details { - pages: pagy_data.pages, - first: pagy_data.page == 1, - prev: pagy_data.prev.nil? ? 0 : pagy_data.prev, - next: pagy_data.next, - last: pagy_data.last, - page: pagy_data.page, - items: pagy_data.items + pages: @reports.total_pages, + first: @reports.first_page?, + prev: @reports.prev_page, + next: @reports.next_page, + last: @reports.last_page? } end end diff --git a/spec/services/reports/time_entries/page_service_spec.rb b/spec/services/reports/time_entries/filter_service_spec.rb similarity index 99% rename from spec/services/reports/time_entries/page_service_spec.rb rename to spec/services/reports/time_entries/filter_service_spec.rb index 813679141c..b0996bd03b 100644 --- a/spec/services/reports/time_entries/page_service_spec.rb +++ b/spec/services/reports/time_entries/filter_service_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe Reports::TimeEntries::PageService do +RSpec.describe Reports::TimeEntries::FilterService do let(:company) { create(:company) } describe "pagination for team members" do