Class: EducationForm::DeleteOldApplications

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/education_form/delete_old_applications.rb

Instance Method Summary collapse

Instance Method Details

#delete_records_by_id(edu_claim_ids, saved_claim_ids, stem_automated_decision_ids) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'app/sidekiq/education_form/delete_old_applications.rb', line 36

def delete_records_by_id(edu_claim_ids, saved_claim_ids, stem_automated_decision_ids)
  logger.info("Deleting #{edu_claim_ids.length} education benefits claims")
  logger.info("Deleting #{saved_claim_ids.length} saved claims")
  logger.info("Deleting #{stem_automated_decision_ids.length} stem automated decisions")

  EducationBenefitsClaim.delete(edu_claim_ids)
  SavedClaim::EducationBenefits.delete(saved_claim_ids)
  EducationStemAutomatedDecision.delete(stem_automated_decision_ids)
end

#form_clausesObject



29
30
31
32
33
34
# File 'app/sidekiq/education_form/delete_old_applications.rb', line 29

def form_clauses
  [
    "(saved_claims.form_id != '22-10203' AND processed_at < '#{2.months.ago}')",
    "(saved_claims.form_id = '22-10203' AND processed_at < '#{1.year.ago}')"
  ].join(' OR ')
end

#performObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/sidekiq/education_form/delete_old_applications.rb', line 7

def perform
  edu_claim_ids = []
  saved_claim_ids = []
  stem_automated_decision_ids = []

  # Remove old education benefits claims and saved claims older than 2 months
  EducationBenefitsClaim.eager_load(:saved_claim)
                        .eager_load(:education_stem_automated_decision)
                        .where(form_clauses)
                        .find_each do |record|
    edu_claim_ids << record.id
    saved_claim_ids << record.saved_claim&.id
    stem_automated_decision_ids << record.education_stem_automated_decision&.id
  end

  edu_claim_ids.compact!
  saved_claim_ids.compact!
  stem_automated_decision_ids.compact!

  delete_records_by_id(edu_claim_ids, saved_claim_ids, stem_automated_decision_ids)
end