diff --git a/app/workers/reminder.rb b/app/workers/reminder.rb new file mode 100644 index 0000000..29584e2 --- /dev/null +++ b/app/workers/reminder.rb @@ -0,0 +1,37 @@ +class Reminder + def initialize + @collection = reminder_collection + end + + def count + @collection.count + end + alias size count + + def candidates + Candidate.includes(:recruiter).where(id: @collection.map { |row| row['id'] }) + end + + def send_all + candidates.each do |candidate| + CandidateMailer.reminder(candidate).deliver_now + flag_as_reminded candidate.id + end + end + + private + + def reminder_collection + sql = "select c.id, c.test_hash, c.name, c.email, max(a.updated_at) last_updated + from candidates c + inner join answers a on a.candidate_id = c.id + where completed = false and reminded = false + group by c.id + having MAX(a.updated_at) < DATE_SUB(NOW(), INTERVAL 24 HOUR);" + ActiveRecord::Base.connection.exec_query(sql) + end + + def flag_as_reminded candidate_id + Candidate.find(candidate_id).update(reminded: true) + end +end diff --git a/lib/tasks/reminders.rake b/lib/tasks/reminders.rake new file mode 100644 index 0000000..bd2ee48 --- /dev/null +++ b/lib/tasks/reminders.rake @@ -0,0 +1,7 @@ +namespace :reminders do + desc "send reminders to stagnate quizes" + task send_all: :environment do + reminders = Reminder.new + reminders.send_all + end +end diff --git a/test/fixtures/answers.yml b/test/fixtures/answers.yml index a46385b..7ae3270 100644 --- a/test/fixtures/answers.yml +++ b/test/fixtures/answers.yml @@ -6,8 +6,8 @@ roy1: answer: option2 saved: 0 submitted: true - created_at: <%= DateTime.now() - 90.minutes %> - updated_at: <%= DateTime.now() - 90.minutes %> + created_at: <%= DateTime.now() - 90.minutes - 36.hours %> + updated_at: <%= DateTime.now() - 90.minutes - 36.hours %> roy2: candidate: roy @@ -15,8 +15,8 @@ roy2: answer: ["option-1", "option-4", "option5"] saved: 1 submitted: true - created_at: <%= DateTime.now() - 85.minutes %> - updated_at: <%= DateTime.now() - 85.minutes %> + created_at: <%= DateTime.now() - 85.minutes - 36.hours %> + updated_at: <%= DateTime.now() - 85.minutes - 36.hours %> dawn1: candidate: dawn diff --git a/test/workers/reminder_test.rb b/test/workers/reminder_test.rb new file mode 100644 index 0000000..00d25e5 --- /dev/null +++ b/test/workers/reminder_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +class ReminderTest < ActiveSupport::TestCase + test "collection is created with one result" do + reminders = Reminder.new + assert_equal 1, reminders.size + end + + test "each candidate has needed attributes" do + reminders = Reminder.new + + assert_instance_of String, reminders.candidates.first.name + assert_instance_of String, reminders.candidates.first.test_hash + assert_instance_of String, reminders.candidates.first.email + end + + test "send reminders sends email, and flags reminded" do + reminders = Reminder.new + pre_reminded = Candidate.find(reminders.candidates.first.id).reminded + + assert_difference("ActionMailer::Base.deliveries.size", reminders.count) do + reminders.send_all + end + refute_equal pre_reminded, Candidate.find(reminders.candidates.first.id).reminded + end +end