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

Expire requestable synchronously #2254

Merged
merged 5 commits into from
Jun 13, 2024
Merged
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
14 changes: 0 additions & 14 deletions app/jobs/expire_requestable_job.rb

This file was deleted.

9 changes: 6 additions & 3 deletions app/jobs/expire_requestables_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ def perform(requestable_class_name)
requestable_class_name
.constantize
.requested
.where(expired_at: nil, received_at: nil)
.find_each do |requestable|
ExpireRequestableJob.perform_later(requestable)
.not_received
.not_expired
.each do |requestable|
if requestable.expires? && Time.zone.now > requestable.expires_at
ExpireRequestable.call(requestable:, user: "Expirer")
end
end
end
end
4 changes: 4 additions & 0 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ def requested_at
created_at
end

def received_at
submitted_at
end

def expires_after
submitted? ? nil : 6.months
end
Expand Down
15 changes: 12 additions & 3 deletions app/models/concerns/expirable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
module Expirable
extend ActiveSupport::Concern

included do
scope :expired, -> { where.not(expired_at: nil) }
scope :not_expired, -> { where(expired_at: nil) }
end

def expires_at
return nil if requested_at.nil? || expires_after.nil?
return nil if received_at || requested_at.nil? || expires_after.nil?

requested_at + expires_after
end

def expires?
expires_at != nil
end

def days_until_expired
return nil if expires_at.nil?
(expires_at.to_date - Time.zone.today).to_i
@days_until_expired ||=
expires? ? (expires_at.to_date - Time.zone.today).to_i : nil
end

def expired!
Expand Down
9 changes: 6 additions & 3 deletions app/models/further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ class FurtherInformationRequest < ApplicationRecord
FOUR_WEEK_COUNTRY_CODES = %w[AU CA GI NZ US].freeze

def should_send_reminder_email?(_name, number_of_reminders_sent)
(days_until_expired <= 14 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 7 && number_of_reminders_sent == 1) ||
(days_until_expired <= 2 && number_of_reminders_sent == 2)
days_until_expired &&
(
(days_until_expired <= 14 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 7 && number_of_reminders_sent == 1) ||
(days_until_expired <= 2 && number_of_reminders_sent == 2)
)
end

def send_reminder_email(_name, _number_of_reminders_sent)
Expand Down
7 changes: 5 additions & 2 deletions app/models/reference_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ def responses_given?
end

def should_send_reminder_email?(_name, number_of_reminders_sent)
(days_until_expired <= 28 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 14 && number_of_reminders_sent == 1)
days_until_expired &&
(
(days_until_expired <= 28 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 14 && number_of_reminders_sent == 1)
)
end

def send_reminder_email(_name, number_of_reminders_sent)
Expand Down
8 changes: 4 additions & 4 deletions config/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ expire_consent_requests:
args: ["ConsentRequest"]

expire_further_information_requests:
cron: "10 2 * * *"
cron: "12 2 * * *"
class: ExpireRequestablesJob
args: ["FurtherInformationRequest"]

expire_professional_standing_requests:
cron: "20 2 * * *"
cron: "24 2 * * *"
class: ExpireRequestablesJob
args: ["ProfessionalStandingRequest"]

expire_qualification_requests:
cron: "30 2 * * *"
cron: "36 2 * * *"
class: ExpireRequestablesJob
args: ["QualificationRequest"]

expire_reference_requests:
cron: "40 2 * * *"
cron: "48 2 * * *"
class: ExpireRequestablesJob
args: ["ReferenceRequest"]

Expand Down
167 changes: 0 additions & 167 deletions spec/jobs/expire_requestable_job_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@
describe "#perform" do
subject(:perform) { described_class.new.perform(class_name) }

let!(:requested_requestable) { create(:"requested_#{factory_name}") }
let!(:requested_requestable) do
create(:"requested_#{factory_name}", requested_at: 1.year.ago)
end
let!(:received_requestable) { create(:"received_#{factory_name}") }
let!(:expired_requestable) do
create(:"requested_#{factory_name}", :expired)
end

it "enqueues a job for each 'requested' #{class_name}s" do
expect { perform }.to have_enqueued_job(ExpireRequestableJob).with(
requested_requestable,
it "enqueues a job for each requested #{class_name}s" do
expect(ExpireRequestable).to receive(:call).with(
requestable: requested_requestable,
user: "Expirer",
)
perform
end

it "doesn't enqueue a job for 'received' #{class_name}s" do
expect { perform }.to_not have_enqueued_job(ExpireRequestableJob).with(
received_requestable,
it "doesn't enqueue a job for received #{class_name}s" do
expect(ExpireRequestable).to_not receive(:call).with(
requestable: received_requestable,
user: "Expirer",
)
perform
end

it "doesn't enqueue a job for 'expired' #{class_name}s" do
expect { perform }.to_not have_enqueued_job(ExpireRequestableJob).with(
expired_requestable,
it "doesn't enqueue a job for expired #{class_name}s" do
expect(ExpireRequestable).to_not receive(:call).with(
requestable: expired_requestable,
user: "Expirer",
)
perform
end
end
end
Expand Down
Loading