From 6afb3480fc25ad32ced39eb4c4a40ebe82bdc0f7 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Mon, 2 Sep 2024 11:50:25 +0100 Subject: [PATCH 1/2] extract claim filters to object --- app/controllers/admin/claims_controller.rb | 58 +++----------------- app/forms/admin/claims_filter_form.rb | 64 ++++++++++++++++++++++ app/models/claim.rb | 6 +- 3 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 app/forms/admin/claims_filter_form.rb diff --git a/app/controllers/admin/claims_controller.rb b/app/controllers/admin/claims_controller.rb index 32f1897f1e..6f26e3cb2a 100644 --- a/app/controllers/admin/claims_controller.rb +++ b/app/controllers/admin/claims_controller.rb @@ -2,21 +2,16 @@ class Admin::ClaimsController < Admin::BaseAdminController include Pagy::Backend before_action :ensure_service_operator - before_action :filter_claims_by_status, only: :index def index - @claims ||= Claim.includes(:decisions).not_held.awaiting_decision + @filter_form = Admin::ClaimsFilterForm.new( + team_member: params[:team_member], + policy: params[:policy], + status: params[:status] + ) - @claims = @claims.by_policy(filtered_policy) if filtered_policy - @claims = @claims.by_claims_team_member(filtered_team_member, params[:status]) if filtered_team_member - @claims = @claims.unassigned if filtered_unassigned - - @claims = @claims.includes(:tasks, eligibility: [:claim_school, :current_school]) - @claims = @claims.order(:submitted_at) - - all_claims = @claims - @total_claim_count = all_claims.count - @pagy, @claims = pagy(@claims) + @total_claim_count = @filter_form.count + @pagy, @claims = pagy(@filter_form.claims) respond_to do |format| format.html { @@ -79,19 +74,6 @@ def unhold private - def filtered_policy - Policies[params[:policy]] - end - - def filtered_team_member - return if params[:team_member].blank? || filtered_unassigned - DfeSignIn::User.not_deleted.find(params[:team_member]).id - end - - def filtered_unassigned - params[:team_member] == "unassigned" - end - # Stores where View Claim originated from, e.g. claims index or search results def claims_backlink_path!(source_path) session[:claims_backlink_path] = source_path @@ -100,30 +82,4 @@ def claims_backlink_path!(source_path) def hold_params params.require(:hold).permit(:body).merge(claim: @claim) end - - def filter_claims_by_status - @claims = - case params[:status] - when "approved" - Claim.current_academic_year.approved - when "approved_awaiting_qa" - Claim.approved.awaiting_qa - when "approved_awaiting_payroll" - approved_awaiting_payroll - when "automatically_approved_awaiting_payroll" - Claim.current_academic_year.payrollable.auto_approved - when "rejected" - Claim.current_academic_year.rejected - when "held" - Claim.includes(:decisions).held.awaiting_decision - when "failed_bank_validation" - Claim.includes(:decisions).failed_bank_validation.awaiting_decision - end - end - - def approved_awaiting_payroll - claim_ids_with_payrollable_topups = Topup.payrollable.pluck(:claim_id) - - Claim.current_academic_year.payrollable.or(Claim.current_academic_year.where(id: claim_ids_with_payrollable_topups)) - end end diff --git a/app/forms/admin/claims_filter_form.rb b/app/forms/admin/claims_filter_form.rb new file mode 100644 index 0000000000..338c572396 --- /dev/null +++ b/app/forms/admin/claims_filter_form.rb @@ -0,0 +1,64 @@ +class Admin::ClaimsFilterForm + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :team_member, :string + attribute :policy, :string + attribute :status, :string + + def claims + @claims = + case status + when "approved" + Claim.current_academic_year.approved + when "approved_awaiting_qa" + Claim.approved.awaiting_qa + when "approved_awaiting_payroll" + approved_awaiting_payroll + when "automatically_approved_awaiting_payroll" + Claim.current_academic_year.payrollable.auto_approved + when "rejected" + Claim.current_academic_year.rejected + when "held" + Claim.includes(:decisions).held.awaiting_decision + when "failed_bank_validation" + Claim.includes(:decisions).failed_bank_validation.awaiting_decision + end + + @claims ||= Claim.includes(:decisions).not_held.awaiting_decision + + @claims = @claims.by_policy(filtered_policy) if filtered_policy + @claims = @claims.by_claims_team_member(filtered_team_member, status) if filtered_team_member + @claims = @claims.unassigned if filtered_unassigned + + @claims = @claims.includes(:tasks, eligibility: [:claim_school, :current_school]) + @claims = @claims.order(:submitted_at) + + @claims + end + + def count + claims.count + end + + private + + def approved_awaiting_payroll + claim_ids_with_payrollable_topups = Topup.payrollable.pluck(:claim_id) + + Claim.current_academic_year.payrollable.or(Claim.current_academic_year.where(id: claim_ids_with_payrollable_topups)) + end + + def filtered_policy + Policies[policy] + end + + def filtered_team_member + return if team_member.blank? || filtered_unassigned + DfeSignIn::User.not_deleted.find(team_member).id + end + + def filtered_unassigned + team_member == "unassigned" + end +end diff --git a/app/models/claim.rb b/app/models/claim.rb index c09e860c50..8464457f07 100644 --- a/app/models/claim.rb +++ b/app/models/claim.rb @@ -166,7 +166,11 @@ class Claim < ApplicationRecord scope :submitted, -> { where.not(submitted_at: nil) } scope :held, -> { where(held: true) } scope :not_held, -> { where(held: false) } - scope :awaiting_decision, -> { submitted.joins("LEFT OUTER JOIN decisions ON decisions.claim_id = claims.id AND decisions.undone = false").where(decisions: {claim_id: nil}) } + scope :awaiting_decision, -> do + submitted + .joins("LEFT OUTER JOIN decisions ON decisions.claim_id = claims.id AND decisions.undone = false") + .where(decisions: {claim_id: nil}) + end scope :awaiting_task, ->(task_name) { awaiting_decision.joins(sanitize_sql(["LEFT OUTER JOIN tasks ON tasks.claim_id = claims.id AND tasks.name = ?", task_name])).where(tasks: {claim_id: nil}) } scope :auto_approved, -> { approved.where(decisions: {created_by: nil}) } scope :approved, -> { joins(:decisions).merge(Decision.active.approved) } From 99722c74e88387c4932a61bcdc8e38784877981b Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Tue, 3 Sep 2024 09:09:21 +0100 Subject: [PATCH 2/2] refactor: claims filter form --- app/forms/admin/claims_filter_form.rb | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/forms/admin/claims_filter_form.rb b/app/forms/admin/claims_filter_form.rb index 338c572396..5049eeea57 100644 --- a/app/forms/admin/claims_filter_form.rb +++ b/app/forms/admin/claims_filter_form.rb @@ -7,6 +7,8 @@ class Admin::ClaimsFilterForm attribute :status, :string def claims + return @claims if @claims + @claims = case status when "approved" @@ -23,13 +25,13 @@ def claims Claim.includes(:decisions).held.awaiting_decision when "failed_bank_validation" Claim.includes(:decisions).failed_bank_validation.awaiting_decision + else + Claim.includes(:decisions).not_held.awaiting_decision end - @claims ||= Claim.includes(:decisions).not_held.awaiting_decision - - @claims = @claims.by_policy(filtered_policy) if filtered_policy - @claims = @claims.by_claims_team_member(filtered_team_member, status) if filtered_team_member - @claims = @claims.unassigned if filtered_unassigned + @claims = @claims.by_policy(selected_policy) if selected_policy + @claims = @claims.by_claims_team_member(selected_team_member, status) if selected_team_member + @claims = @claims.unassigned if unassigned? @claims = @claims.includes(:tasks, eligibility: [:claim_school, :current_school]) @claims = @claims.order(:submitted_at) @@ -49,16 +51,17 @@ def approved_awaiting_payroll Claim.current_academic_year.payrollable.or(Claim.current_academic_year.where(id: claim_ids_with_payrollable_topups)) end - def filtered_policy + def selected_policy Policies[policy] end - def filtered_team_member - return if team_member.blank? || filtered_unassigned - DfeSignIn::User.not_deleted.find(team_member).id + def selected_team_member + return if team_member.blank? || unassigned? + + DfeSignIn::User.not_deleted.find(team_member) end - def filtered_unassigned + def unassigned? team_member == "unassigned" end end