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 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
13 changes: 13 additions & 0 deletions app/services/draft_case_contact_cleanup_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class DraftCaseContactCleanupService
ORPHANED_DRAFT_EXPIRATION_DELAY = 1.week.ago
ORPHANED_DRAFT_EXPIRATION_DELAY_WITHOUT_CASE = 1.day.ago

def self.call
drafts_for_removal = CaseContact.where.not(status: "active").where("case_contacts.created_at < ?", ORPHANED_DRAFT_EXPIRATION_DELAY)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was just looking at some enum things:

Suggested change
drafts_for_removal = CaseContact.where.not(status: "active").where("case_contacts.created_at < ?", ORPHANED_DRAFT_EXPIRATION_DELAY)
drafts_for_removal = CaseContact.not_active.where("case_contacts.created_at < ?", ORPHANED_DRAFT_EXPIRATION_DELAY)

status enum should provide this scope, and will not break if enum definition changes, such as switch to integer column. (Unlikely to happen, just thought this is useful to know). Same could be used below.

.or(CaseContact.where.not(status: "active").where(draft_case_ids: []).where("case_contacts.created_at < ?", ORPHANED_DRAFT_EXPIRATION_DELAY_WITHOUT_CASE))

drafts_for_removal.each do |draft|
draft.destroy
end
end
end
4 changes: 4 additions & 0 deletions lib/tasks/case_contact_draft_cleanup.rake
Original file line number Diff line number Diff line change
@@ -0,0 +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
DraftCaseContactCleanupService.call
end
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) { 8.days.ago }
let!(:past_exp_without_case) { 2.days.ago }
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