From 8dc06ddb9117724c90169ea7b7a99bb2526c4f6d Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Thu, 10 Oct 2024 13:25:18 +0100 Subject: [PATCH] Fix duplciate claims in list The awaiting provider verification scope joins to notes, claims can have more than one note, if they do duplicate claims are returned from the query. To resolve this we call distinct on the claim id. Claims have a couple of JSON columns so we can't use the `distinct` method directly, instead we have to explicitly use distinct with the claim.id. We also need to wrap the whole query in a where clause so when we chain on a `count` rails generates the correct SQL rather than something like `SELECT COUNT(count_column) FROM (SELECT DISTINCT ON (claims.id), claims.id)` Additionally we have to make sure to include `submitted_at` in the distinct clause as some of the scopes used to build the query include order by submitted_at clauses. --- app/forms/admin/claims_filter_form.rb | 6 +- spec/forms/admin/claims_filter_form_spec.rb | 64 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 spec/forms/admin/claims_filter_form_spec.rb diff --git a/app/forms/admin/claims_filter_form.rb b/app/forms/admin/claims_filter_form.rb index 9e37bb314e..5148a9380d 100644 --- a/app/forms/admin/claims_filter_form.rb +++ b/app/forms/admin/claims_filter_form.rb @@ -67,8 +67,12 @@ def claims @claims = @claims.unassigned if unassigned? @claims = @claims.includes(:tasks, eligibility: [:claim_school, :current_school]) - @claims = @claims.order(:submitted_at) + @claims = @claims.select("DISTINCT ON (claims.id, claims.submitted_at) claims.id") + + @claims = Claim.where(id: @claims) + + @claims = @claims.order(:submitted_at) @claims end diff --git a/spec/forms/admin/claims_filter_form_spec.rb b/spec/forms/admin/claims_filter_form_spec.rb new file mode 100644 index 0000000000..7636442208 --- /dev/null +++ b/spec/forms/admin/claims_filter_form_spec.rb @@ -0,0 +1,64 @@ +require "rails_helper" + +RSpec.describe Admin::ClaimsFilterForm, type: :model do + describe "#claims" do + context "when the status is awaiting_provider_verification" do + it "returns the expected claims" do + claim_awaiting_provider_verification_1 = build( + :claim, + :submitted + ) + + create( + :further_education_payments_eligibility, + claim: claim_awaiting_provider_verification_1, + flagged_as_duplicate: false + ) + + claim_awaiting_provider_verification_2 = build( + :claim, + :submitted + ) + + create( + :further_education_payments_eligibility, + claim: claim_awaiting_provider_verification_2, + flagged_as_duplicate: true + ) + + create( + :note, + claim: claim_awaiting_provider_verification_2, + label: "provider_verification" + ) + + create( + :note, + claim: claim_awaiting_provider_verification_2, + label: "provider_verification" + ) + + _claim_not_awating_provider_verification = build(:claim, :submitted) + + create( + :further_education_payments_eligibility, + :verified + ) + + form = described_class.new( + session: {}, + filters: { + status: "awaiting_provider_verification" + } + ) + + expect(form.claims).to match_array( + [ + claim_awaiting_provider_verification_1, + claim_awaiting_provider_verification_2 + ] + ) + end + end + end +end