Skip to content

Commit

Permalink
Return duplicate ids
Browse files Browse the repository at this point in the history
Return the duplicate claim ids from the query and introduce a struct to
wrap the result. This change will make it easier to reuse this query if
trying to find duplicates of a specific claim.
  • Loading branch information
rjlynch committed Dec 16, 2024
1 parent 0b73704 commit 9df634c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions app/models/admin/reports/duplicate_approved_claims.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ def to_csv

private

class DuplicateClaimRow < Struct.new(:id, :other_claim_id, keyword_init: true); end

attr_reader :academic_year

def rows
scope.map(&ClaimPresenter.method(:new)).map(&:to_a)
end

def scope
Claim.where(
id: Set.new(duplicates_by_eligibility + duplicates_by_attributes)
).includes(decisions: :created_by)
Claim.where(id: Set.new(duplicates.map(&:id)))
.includes(decisions: :created_by)
end

def duplicates
duplicates_by_eligibility + duplicates_by_attributes
end

def duplicates_by_eligibility
ActiveRecord::Base.connection.execute(
Policies::POLICIES.map do |policy|
policy_with_claimable_policies(policy)
end.compact.join("\nUNION\n")
).map { |row| row["id"] }
).map(&DuplicateClaimRow.method(:new))
end

def policy_with_claimable_policies(policy)
Expand All @@ -70,7 +75,7 @@ def policy_with_claimable_policies(policy)
)

<<~SQL
SELECT claims.id
SELECT claims.id, other_claims.id AS other_claim_id
FROM #{left_table}
JOIN #{right_table} #{right_table_alias}
ON #{left_table}.id != #{right_table_alias}.id
Expand Down Expand Up @@ -173,7 +178,7 @@ def duplicates_by_attributes
end.join(" AND ")

<<~SQL
SELECT current_claims.id
SELECT current_claims.id, other_claims.id AS other_claim_id
FROM current_claims
JOIN current_claims other_claims
ON #{join_condition}
Expand All @@ -183,7 +188,7 @@ def duplicates_by_attributes

query = current_claims + "\n" + filter

ActiveRecord::Base.connection.execute(query).map { |row| row["id"] }
ActiveRecord::Base.connection.execute(query).map(&DuplicateClaimRow.method(:new))
end

class ClaimPresenter
Expand Down

0 comments on commit 9df634c

Please sign in to comment.