Skip to content

Commit

Permalink
Don't skip check if eligibility is missing attrs
Browse files Browse the repository at this point in the history
If the two eligibilities don't have all the same attributes, rather than
skipping the comparison only compare the attributes they have in common.
Also uses select rather than pluck so we don't need to run a query for
each iteration of the loop
  • Loading branch information
rjlynch committed Nov 1, 2024
1 parent 166ec2a commit 13e17f0
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions app/models/claim/matching_attribute_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,24 @@ def matching_claims
end

# Eligibility attributes
eligibility_ids = eligibility_attributes_groups_to_match.map { |attributes|
eligibility_ids = eligibility_attributes_groups_to_match.flat_map do |attributes|
vals = values_for_attributes(@source_claim.eligibility, attributes)
next if vals.blank?

concatenated_columns = "CONCAT(#{attributes.join(",")})" # current policies will only have a single value in attributes
policies_to_find_matches.map { |policy|
policy::Eligibility.where("LOWER(#{concatenated_columns}) = LOWER(?)", vals.join) if all_attributes_exist_on_the_eligibility(attributes, policy::Eligibility)
}
}.flatten.compact.map(&:id)
policies_to_find_matches.map do |policy|
# Not all eligibility models have the same columns
attributes_to_check = attributes & policy::Eligibility.column_names

if attributes_to_check.any?
concatenated_columns = "CONCAT(#{attributes_to_check.join(",")})"

policy::Eligibility.where(
"LOWER(#{concatenated_columns}) = LOWER(?)",
vals
).select(:id)
end
end
end.compact_blank

eligibility_match_query = Claim.where(eligibility_id: eligibility_ids)
match_queries = match_queries.or(eligibility_match_query)
Expand Down Expand Up @@ -91,9 +100,5 @@ def values_for_attributes(object, attributes)
object.read_attribute(attribute)
}.reject(&:blank?)
end

def all_attributes_exist_on_the_eligibility(attributes, eligibility)
(attributes - eligibility.column_names).empty?
end
end
end

0 comments on commit 13e17f0

Please sign in to comment.