-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We're soon going to want to generate a report for the ops team of the duplicate claims. This commit introduces a new claim verifier that records the duplicate claims, if any. Once existing duplicates on production have been back filled we can replace other uses of `MatchingAttributeFinder#matching_claims` with `claim.duplicates` and also join claims to their duplicates when generating the duplicate claims report.
- Loading branch information
Showing
14 changed files
with
323 additions
and
7 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
app/models/automated_checks/claim_verifiers/duplicate_claims_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module AutomatedChecks | ||
module ClaimVerifiers | ||
class DuplicateClaimsCheck | ||
def initialize(claim:) | ||
@claim = claim | ||
end | ||
|
||
def perform | ||
matching_attribute_finder.matching_claims.each do |existing_claim| | ||
if existing_claim.created_at < claim.created_at | ||
original_claim = existing_claim | ||
duplicate_claim = claim | ||
else | ||
original_claim = claim | ||
duplicate_claim = existing_claim | ||
end | ||
|
||
unless Claims::ClaimDuplicate.exists?(original_claim: original_claim, duplicate_claim: duplicate_claim) | ||
Claims::ClaimDuplicate.create!( | ||
original_claim: original_claim, | ||
duplicate_claim: duplicate_claim, | ||
matching_attributes: matching_attribute_finder.matching_attributes(existing_claim) | ||
) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :claim | ||
|
||
def matching_attribute_finder | ||
@matching_attribute_finder ||= Claim::MatchingAttributeFinder.new(claim) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Claims | ||
def self.table_name_prefix | ||
"claims_" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Claims | ||
class ClaimDuplicate < ApplicationRecord | ||
belongs_to :original_claim, class_name: "Claim" | ||
belongs_to :duplicate_claim, class_name: "Claim" | ||
|
||
validates :duplicate_claim, uniqueness: { | ||
scope: :original_claim, | ||
message: "has already been registered as a duplicate" | ||
} | ||
validate :claims_are_not_the_same, if: -> { original_claim && duplicate_claim } | ||
validate :original_claim_is_older, if: -> { original_claim && duplicate_claim } | ||
validates :matching_attributes, presence: true | ||
|
||
private | ||
|
||
def claims_are_not_the_same | ||
return unless original_claim == duplicate_claim | ||
|
||
errors.add(:duplicate_claim, "can't be the same as the original claim") | ||
end | ||
|
||
def original_claim_is_older | ||
return unless original_claim.created_at > duplicate_claim.created_at | ||
|
||
errors.add(:original_claim, "must be older than the duplicate claim") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
db/migrate/20241219115223_create_claims_claim_duplicates.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class CreateClaimsClaimDuplicates < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :claims_claim_duplicates, id: :uuid do |t| | ||
t.belongs_to( | ||
:original_claim, | ||
null: false, | ||
foreign_key: { | ||
to_table: :claims | ||
}, | ||
type: :uuid | ||
) | ||
|
||
t.belongs_to( | ||
:duplicate_claim, | ||
null: false, | ||
foreign_key: { | ||
to_table: :claims | ||
}, | ||
type: :uuid | ||
) | ||
|
||
t.jsonb :matching_attributes, default: [] | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index( | ||
:claims_claim_duplicates, | ||
[:original_claim_id, :duplicate_claim_id], | ||
unique: true | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
spec/models/automated_checks/claim_verifiers/duplicate_claims_check_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe AutomatedChecks::ClaimVerifiers::DuplicateClaimsCheck do | ||
describe "#perform" do | ||
context "when the claim has duplicates" do | ||
it "marks the claim as having duplicates" do | ||
existing_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "one" | ||
) | ||
|
||
new_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "two" | ||
) | ||
|
||
described_class.new(claim: new_claim).perform | ||
|
||
expect(existing_claim.reload.duplicates).to include(new_claim) | ||
expect(new_claim.reload.originals).to include(existing_claim) | ||
|
||
claim_duplicate = new_claim.claim_duplicates_as_duplicate_claim.last | ||
|
||
expect(claim_duplicate.original_claim).to eq(existing_claim) | ||
expect(claim_duplicate.duplicate_claim).to eq(new_claim) | ||
expect(claim_duplicate.matching_attributes).to eq(["email_address"]) | ||
end | ||
|
||
it "is idempotent" do | ||
existing_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "one" | ||
) | ||
|
||
new_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "two" | ||
) | ||
|
||
Claims::ClaimDuplicate.create!( | ||
original_claim: existing_claim, | ||
duplicate_claim: new_claim, | ||
matching_attributes: ["email_address"] | ||
) | ||
|
||
expect { described_class.new(claim: new_claim).perform }.not_to( | ||
change(existing_claim.duplicates, :count) | ||
) | ||
end | ||
end | ||
|
||
context "when the claim has no duplicates" do | ||
it "does not mark the claim as having duplicates" do | ||
existing_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "one" | ||
) | ||
|
||
new_claim = create( | ||
:claim, | ||
:submitted, | ||
email_address: "[email protected]", | ||
first_name: "two" | ||
) | ||
|
||
described_class.new(claim: new_claim).perform | ||
|
||
expect(existing_claim.reload.duplicates).not_to include(new_claim) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.