diff --git a/app/models/form.rb b/app/models/form.rb index 0a68df37e..f943ec71f 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -183,6 +183,14 @@ def check_expired end end + def self.archive_expired! + Form.where("expiration_date is not null and expiration_date < NOW() and aasm_state <> 'archived'").find_each do |form| + Event.log_event(Event.names[:form_archived], 'Form', form.uuid, "Form #{form.name} archived at #{DateTime.now}", 1) + form.archive! + UserMailer.form_status_changed(form:, action: 'archived').deliver_later + end + end + def set_uuid self.uuid = SecureRandom.uuid if uuid.blank? end diff --git a/config/schedule.rb b/config/schedule.rb index 3ab64283c..af4e7ca67 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -32,3 +32,7 @@ every 1.day, at: '10:30pm' do rake 'scheduled_jobs:send_two_weeks_until_inactivation_warning' end + +every 1.day, at: '11:00pm' do + rake 'scheduled_jobs:archive_surveys' +end diff --git a/lib/tasks/scheduled_jobs.rake b/lib/tasks/scheduled_jobs.rake index f5493d928..4f752131d 100644 --- a/lib/tasks/scheduled_jobs.rake +++ b/lib/tasks/scheduled_jobs.rake @@ -18,4 +18,8 @@ namespace :scheduled_jobs do task send_two_weeks_until_inactivation_warning: :environment do User.send_account_deactivation_notifications(14) end + + task archive_surveys: :environment do + Form.archive_expired! + end end