From 197993e362b228a9f89a01c44a41e775fcb53340 Mon Sep 17 00:00:00 2001 From: Allen Taylor Date: Tue, 12 Jul 2022 13:32:40 -0400 Subject: [PATCH] System archives expired surveys each night --- app/models/form.rb | 8 ++++++++ config/schedule.rb | 4 ++++ lib/tasks/scheduled_jobs.rake | 4 ++++ 3 files changed, 16 insertions(+) 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