-
-
Notifications
You must be signed in to change notification settings - Fork 472
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
base: main
Are you sure you want to change the base?
Changes from all commits
9de7d46
b182557
6b6d353
ac66d5d
d34fae5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
.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 |
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." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good desc There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
There was a problem hiding this comment.
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:
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.