Skip to content

Commit

Permalink
Use scoped associations to improve query performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kayline committed Oct 25, 2024
1 parent 6f9240c commit 490d7ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/models/appeal_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class AppealSubmission < ApplicationRecord

has_many :appeal_submission_uploads, dependent: :destroy
has_many :secondary_appeal_forms, dependent: :destroy
has_many :incomplete_secondary_appeal_forms, lambda {
where(delete_date: nil)
}, class_name: 'SecondaryAppealForm', dependent: nil, inverse_of: :appeal_submission

has_one :saved_claim, foreign_key: :guid, primary_key: :submitted_appeal_uuid, dependent: :restrict_with_exception,
inverse_of: :appeal_submission
Expand Down
2 changes: 2 additions & 0 deletions app/models/secondary_appeal_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ class SecondaryAppealForm < ApplicationRecord

has_kms_key
has_encrypted :form, key: :kms_key, **lockbox_options

scope :incomplete, -> { where(delete_date: nil) }
end
15 changes: 7 additions & 8 deletions app/sidekiq/decision_review/saved_claim_sc_status_updater_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def perform

supplemental_claims.each do |sc|
status, attributes = get_status_and_attributes(sc.guid)
uploads_metadata = get_evidence_uploads_statuses(sc.guid)
secondary_forms_complete = get_and_update_secondary_form_statuses(sc.guid)
uploads_metadata = get_evidence_uploads_statuses(sc)
secondary_forms_complete = get_and_update_secondary_form_statuses(sc)

timestamp = DateTime.now
params = { metadata: attributes.merge(uploads: uploads_metadata).to_json, metadata_updated_at: timestamp }
Expand Down Expand Up @@ -69,11 +69,11 @@ def get_status_and_attributes(guid)
[status, attributes]
end

def get_evidence_uploads_statuses(submitted_appeal_uuid)
def get_evidence_uploads_statuses(saved_claim)
result = []

attachment_ids = AppealSubmission.find_by(submitted_appeal_uuid:)&.appeal_submission_uploads
&.pluck(:lighthouse_upload_id) || []
attachment_ids = saved_claim.appeal_submission&.appeal_submission_uploads
&.pluck(:lighthouse_upload_id) || []

attachment_ids.each do |uuid|
response = decision_review_service.get_supplemental_claim_upload(uuid:).body
Expand All @@ -84,12 +84,11 @@ def get_evidence_uploads_statuses(submitted_appeal_uuid)
result
end

def get_and_update_secondary_form_statuses(submitted_appeal_uuid)
def get_and_update_secondary_form_statuses(saved_claim)
all_complete = true
return all_complete unless Flipper.enabled?(:decision_review_track_4142_submissions)

secondary_forms = AppealSubmission.find_by(submitted_appeal_uuid:)&.secondary_appeal_forms
secondary_forms = secondary_forms&.filter { |form| form.delete_date.nil? } || []
secondary_forms = saved_claim.appeal_submission&.incomplete_secondary_appeal_forms || []

secondary_forms.each do |form|
response = decision_review_service.get_supplemental_claim_upload(uuid: form.guid).body
Expand Down
10 changes: 10 additions & 0 deletions spec/models/secondary_appeal_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@
it { is_expected.to validate_presence_of(:form_id) }
it { is_expected.to validate_presence_of(:form) }
end

describe 'incomplete scope' do
let(:complete_form) { create(:secondary_appeal_form4142, delete_date: 10.days.ago) }
let(:incomplete_form) { create(:secondary_appeal_form4142) }

it 'only returns records without a delete_date' do
results = SecondaryAppealForm.incomplete
expect(results).to contain_exactly(incomplete_form)
end
end
end

0 comments on commit 490d7ec

Please sign in to comment.