reminders in play!

This commit is contained in:
Mark Moser 2016-08-02 15:25:55 -05:00
parent 9ac58773df
commit d5052644c2
4 changed files with 74 additions and 4 deletions

37
app/workers/reminder.rb Normal file
View File

@ -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

7
lib/tasks/reminders.rake Normal file
View File

@ -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

View File

@ -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

View File

@ -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