Skip to content

Commit

Permalink
Fix pagination and group by filters (#1582)
Browse files Browse the repository at this point in the history
* fix pagination and group by filters

* rename spec

* fix failing spec

* address review comments

---------

Co-authored-by: “Apoorv <“[email protected]”>
  • Loading branch information
apoorv1316 and “Apoorv authored Nov 7, 2023
1 parent 631b37f commit 1a2d7de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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
Expand Down
58 changes: 24 additions & 34 deletions app/services/reports/time_entries/report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a2d7de

Please sign in to comment.