Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/campaign_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Indexes
#
# index_campaign_emails_on_campaign_contact_id (campaign_contact_id)
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name) UNIQUE
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name)
# index_campaign_emails_on_mailgun_message_id (mailgun_message_id) UNIQUE
#
# Foreign Keys
Expand Down
1 change: 1 addition & 0 deletions app/models/doc_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: doc_assessments
#
# id :bigint not null, primary key
# confirmed :boolean
# error :text
# prompt_version :string default("v1"), not null
# raw_response_json :jsonb not null
Expand Down
24 changes: 24 additions & 0 deletions app/models/doc_assessment_feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ class DocAssessmentFeedback < ApplicationRecord
enum feedback: { unfilled: 0, correct: 1, incorrect: 2 }, _prefix: :feedback

validates :feedback, presence: true

after_commit :update_document_type_from_confirmed_assessment, on: :create

private

def update_document_type_from_confirmed_assessment
return unless feedback_correct?

assessment = doc_assessment
suggested_type = assessment.suggested_document_type
assessment.update(confirmed: true)
document = assessment.document

return if suggested_type.blank?
return if suggested_type == document.document_type
return unless valid_document_type?(suggested_type)

document.skip_screener_rerun = true
document.update!(document_type: suggested_type)
end

def valid_document_type?(document_type)
DocumentTypes::ALL_TYPES.map(&:key).include?(document_type)
end
end
4 changes: 3 additions & 1 deletion app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
require "mini_magick"

class Document < ApplicationRecord
attr_accessor :skip_screener_rerun
ACCEPTED_FILE_TYPES = [:browser_native_image, :document]
has_paper_trail on: [:destroy]
belongs_to :intake, optional: true
Expand Down Expand Up @@ -91,7 +92,8 @@ class Document < ApplicationRecord
end
after_save_commit { SearchIndexer.refresh_filterable_properties([client_id]) }
after_destroy_commit { SearchIndexer.refresh_filterable_properties([client_id]) }
after_update_commit :rerun_screener_if_document_type_changed, if: :saved_change_to_document_type?
after_update_commit :rerun_screener_if_document_type_changed,
if: -> { saved_change_to_document_type? && !skip_screener_rerun }

# has_one_attached needs to be called after defining any callbacks that access attachments, like
# the HEIC conversion; see https://github.com/rails/rails/issues/37304
Expand Down
2 changes: 1 addition & 1 deletion app/views/hub/documents/_ai_screener.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if current_user.admin? %>
<% if current_user.admin? && @document.latest_assessment&.confirmed.nil? %>
<% verdict = @document.latest_assessment&.matches_doc_type_verdict %>
<% unless verdict.blank? %>
<%= render "hub/documents/smart_scan_card",
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20260330183210_add_confirmed_to_doc_assessments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddConfirmedToDocAssessments < ActiveRecord::Migration[7.1]
def change
add_column :doc_assessments, :confirmed, :boolean
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2026_03_03_001000) do
ActiveRecord::Schema[7.1].define(version: 2026_03_30_183210) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "plpgsql"
Expand Down Expand Up @@ -615,7 +615,7 @@
t.text "subject"
t.string "to_email"
t.datetime "updated_at", null: false
t.index ["campaign_contact_id", "message_name"], name: "index_campaign_emails_on_contact_id_and_message_name", unique: true
t.index ["campaign_contact_id", "message_name"], name: "index_campaign_emails_on_contact_id_and_message_name"
t.index ["campaign_contact_id"], name: "index_campaign_emails_on_campaign_contact_id"
t.index ["mailgun_message_id"], name: "index_campaign_emails_on_mailgun_message_id", unique: true
end
Expand Down Expand Up @@ -851,6 +851,7 @@
end

create_table "doc_assessments", force: :cascade do |t|
t.boolean "confirmed"
t.datetime "created_at", null: false
t.bigint "document_id", null: false
t.text "error"
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/campaign_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Indexes
#
# index_campaign_emails_on_campaign_contact_id (campaign_contact_id)
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name) UNIQUE
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name)
# index_campaign_emails_on_mailgun_message_id (mailgun_message_id) UNIQUE
#
# Foreign Keys
Expand Down
1 change: 1 addition & 0 deletions spec/factories/doc_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: doc_assessments
#
# id :bigint not null, primary key
# confirmed :boolean
# error :text
# prompt_version :string default("v1"), not null
# raw_response_json :jsonb not null
Expand Down
2 changes: 1 addition & 1 deletion spec/models/campaign_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Indexes
#
# index_campaign_emails_on_campaign_contact_id (campaign_contact_id)
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name) UNIQUE
# index_campaign_emails_on_contact_id_and_message_name (campaign_contact_id,message_name)
# index_campaign_emails_on_mailgun_message_id (mailgun_message_id) UNIQUE
#
# Foreign Keys
Expand Down
106 changes: 106 additions & 0 deletions spec/models/doc_assessment_feedback_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: doc_assessment_feedbacks
#
# id :bigint not null, primary key
# feedback :integer default("unfilled"), not null
# feedback_notes :text
# created_at :datetime not null
# updated_at :datetime not null
# doc_assessment_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_doc_assessment_feedbacks_on_doc_assessment_id (doc_assessment_id)
# index_doc_assessment_feedbacks_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (doc_assessment_id => doc_assessments.id)
# fk_rails_... (user_id => users.id)
#
require "rails_helper"

RSpec.describe DocAssessmentFeedback, type: :model do
describe "callbacks" do
describe "after_commit" do
let(:user) { create(:user) }
let(:document) { create(:document, document_type: DocumentTypes::SsnItin.key) }

context "when feedback is correct and the suggested document type differs from the current document type" do
let!(:doc_assessment) do
create(
:doc_assessment,
document: document,
result_json: {
"suggested_document_type" => DocumentTypes::PrimaryIdentification::DriversLicense.key
},
raw_response_json: {}
)
end

it "updates the document type to the suggested document type" do
create(
:doc_assessment_feedback,
doc_assessment: doc_assessment,
user: user,
feedback: :correct
)

expect(document.reload.document_type).to eq(DocumentTypes::PrimaryIdentification::DriversLicense.key)
end
end

context "when feedback is correct but the suggested document type matches the current document type" do
let!(:doc_assessment) do
create(
:doc_assessment,
document: document,
result_json: {
"suggested_document_type" => DocumentTypes::SsnItin.key
},
raw_response_json: {}
)
end

it "does not change the document type" do
expect {
create(
:doc_assessment_feedback,
doc_assessment: doc_assessment,
user: user,
feedback: :correct
)
}.not_to change { document.reload.document_type }
end
end

context "when feedback is incorrect" do
let!(:doc_assessment) do
create(
:doc_assessment,
document: document,
result_json: {
"suggested_document_type" => DocumentTypes::PrimaryIdentification::DriversLicense.key
},
raw_response_json: {}
)
end

it "does not update the document type" do
expect {
create(
:doc_assessment_feedback,
doc_assessment: doc_assessment,
user: user,
feedback: :incorrect
)
}.not_to change { document.reload.document_type }
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/models/doc_assessment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: doc_assessments
#
# id :bigint not null, primary key
# confirmed :boolean
# error :text
# prompt_version :string default("v1"), not null
# raw_response_json :jsonb not null
Expand Down
28 changes: 28 additions & 0 deletions spec/models/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,34 @@
end
end

describe "after_update_commit" do
include ActiveJob::TestHelper

let!(:document) { create(:document, document_type: DocumentTypes::SsnItin.key) }

before do
clear_enqueued_jobs
end

context "when document_type changes" do
it "enqueues the screener job" do
expect {
document.update!(document_type: DocumentTypes::PrimaryIdentification::DriversLicense.key)
}.to have_enqueued_job(DocScreenerJob).with(document.id)
end
end

context "when skip_screener_rerun is true" do
it "does not enqueue the screener job" do
document.skip_screener_rerun = true

expect {
document.update!(document_type: DocumentTypes::PrimaryIdentification::DriversLicense.key)
}.not_to have_enqueued_job(DocScreenerJob)
end
end
end

describe "creating a document" do
let(:document) { build :document }
let(:object) { document }
Expand Down
Loading