Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft case contact cleanup #5918

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 0 additions & 6 deletions app/models/case_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ def active_or_expenses?

scope :no_drafts, ->(checked) { (checked == 1) ? where(status: "active") : all }

scope :drafts_for_removal, -> {
where.not(status: "active")
.where("case_contacts.created_at < ?", 1.week.ago)
.or(where(draft_case_ids: []).where("case_contacts.created_at < ?", 1.day.ago))
}

filterrific(
default_filter_params: {sorted_by: "occurred_at_desc"},
available_filters: [
Expand Down
12 changes: 12 additions & 0 deletions app/services/draft_case_contact_cleanup_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class DraftCaseContactCleanupService
def self.call
draft_expiration = 1.week.ago
draft_expiration_without_case = 1.day.ago
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant constants like DRAFT_EXPIRATION_DELAY and ORPHANED_DRAFT_EXPIRATION_DELAY

Not sure if orphaned is a better name

drafts_for_removal = CaseContact.where.not(status: "active").where("case_contacts.created_at < ?", draft_expiration)
.or(CaseContact.where.not(status: "active").where(draft_case_ids: []).where("case_contacts.created_at < ?", draft_expiration_without_case))

drafts_for_removal.each do |draft|
draft.destroy
end
end
end
4 changes: 1 addition & 3 deletions lib/tasks/case_contact_draft_cleanup.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
desc "delete draft case contacts marked for clean up, run by heroku scheduler."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good desc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for later PR thought - we may want to move away from heroku scheduler towards something like

      include Sidekiq::Job
      sidekiq_options queue: :general_12h

      def self.crontab
        "0 8 * * *"
      end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with heroku scheduler but would like to learn so I'm curious, what's the rationale for moving away from it in favor of Sidekiq?

task case_contact_draft_cleanup: :environment do
CaseContact.drafts_for_removal.each do |draft|
draft.destroy
end
DraftCaseContactCleanupService.call
end
13 changes: 0 additions & 13 deletions spec/models/case_contact_spec.rb
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good test 👍

Original file line number Diff line number Diff line change
Expand Up @@ -547,19 +547,6 @@
end
end
end

describe ".drafts_for_removal" do
let!(:active_case_contact) { create(:case_contact, status: "active") }
let!(:new_draft_case_contact) { create(:case_contact, status: "started", created_at: Time.now, draft_case_ids: []) }
let!(:draft_case_contact_without_draft_case_id) { create(:case_contact, status: "started", created_at: Time.now - 2.days, draft_case_ids: []) }
let!(:draft_case_contact_with_draft_case_id) { create(:case_contact, status: "started", created_at: Time.now - 2.days, draft_case_ids: [1]) }
let!(:eight_day_draft_case_contact) { create(:case_contact, status: "details", created_at: Time.now - 8.days) }
let!(:eight_day_active_case_contact) { create(:case_contact, status: "active", created_at: Time.now - 8.days) }

it "returns drafts not attached to cases and drafts older than a week" do
expect(described_class.drafts_for_removal).to match_array [draft_case_contact_without_draft_case_id, eight_day_draft_case_contact]
end
end
end

describe "#contact_groups_with_types" do
Expand Down
21 changes: 21 additions & 0 deletions spec/services/draft_case_contact_cleanup_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

RSpec.describe DraftCaseContactCleanupService do
describe ".call" do
let!(:past_expiration) { Time.now - 8.days }
let!(:past_exp_without_case) { Time.now - 2.days }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do 8.days.ago, etc.

let!(:active_case_contact) { create(:case_contact, status: "active") }
let!(:new_draft_case_contact) { create(:case_contact, status: "started", created_at: Time.now, draft_case_ids: []) }
let!(:draft_case_contact_without_draft_case_id) { create(:case_contact, status: "started", created_at: past_exp_without_case, draft_case_ids: []) }
let!(:draft_case_contact_with_draft_case_id) { create(:case_contact, status: "started", created_at: past_exp_without_case, draft_case_ids: [1]) }
let!(:eight_day_draft_case_contact) { create(:case_contact, status: "details", created_at: past_expiration) }
let!(:eight_day_active_case_contact) { create(:case_contact, status: "active", created_at: past_expiration) }

it "returns drafts not attached to cases and drafts older than a week" do
expect { described_class.call }.to change { CaseContact.count }.by(-2)

expect(CaseContact.where(id: draft_case_contact_without_draft_case_id.id)).to be_empty
expect(CaseContact.where(id: eight_day_draft_case_contact.id)).to be_empty
end
end
end
Loading