Skip to content

Commit

Permalink
Merge pull request #3143 from DFE-Digital/filter-refactor
Browse files Browse the repository at this point in the history
Admin claims filter refactor
  • Loading branch information
asmega authored Sep 3, 2024
2 parents 14d1c55 + 99722c7 commit a06c432
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 52 deletions.
58 changes: 7 additions & 51 deletions app/controllers/admin/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
67 changes: 67 additions & 0 deletions app/forms/admin/claims_filter_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Admin::ClaimsFilterForm
include ActiveModel::Model
include ActiveModel::Attributes

attribute :team_member, :string
attribute :policy, :string
attribute :status, :string

def claims
return @claims if @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
else
Claim.includes(:decisions).not_held.awaiting_decision
end

@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)

@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 selected_policy
Policies[policy]
end

def selected_team_member
return if team_member.blank? || unassigned?

DfeSignIn::User.not_deleted.find(team_member)
end

def unassigned?
team_member == "unassigned"
end
end
6 changes: 5 additions & 1 deletion app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit a06c432

Please sign in to comment.