moving weekend logic into the next_attempt_at logic in recipient_schedule

pull/1/head
Jared Cosulich 9 years ago
parent ac7858c5f6
commit a2300a58b9

@ -60,12 +60,18 @@ class RecipientSchedule < ApplicationRecord
attempted_question_ids: attempted.empty? ? nil : attempted.join(','),
queued_question_ids: queued.empty? ? nil : queued.join(','),
last_attempt_at: (unanswered_attempt || attempt).sent_at,
next_attempt_at: next_attempt_at + (60 * 60 * schedule.frequency_hours)
next_attempt_at: next_valid_attempt_time
)
end
return (unanswered_attempt || attempt)
end
def next_valid_attempt_time
local_time = (next_attempt_at + (60 * 60 * schedule.frequency_hours)).in_time_zone('Eastern Time (US & Canada)')
local_time += 1.day while local_time.on_weekend?
return local_time
end
def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at=nil)
if next_attempt_at.nil?
next_attempt_at = Time.at(schedule.start_date.to_time.to_i + (60 * schedule.time))

@ -2,11 +2,9 @@ namespace :survey do
desc 'Text all Recipients ready for an Attempt (only on weekdays)'
task :attempt_questions => :environment do
if Date.today.on_weekday?
Schedule.active.each do |schedule|
schedule.recipient_schedules.ready.each do |recipient_schedule|
recipient_schedule.attempt_question
end
Schedule.active.each do |schedule|
schedule.recipient_schedules.ready.each do |recipient_schedule|
recipient_schedule.attempt_question
end
end
end

@ -13,6 +13,7 @@ describe "survey:attempt_questions" do
n += 1.day until n.on_weekday?
return n
}
let(:ready_recipient_schedule) { double('ready recipient schedule', attempt_question: nil) }
let(:recipient_schedules) { double("recipient schedules", ready: [ready_recipient_schedule]) }
let(:active_schedule) { double("active schedule", recipient_schedules: recipient_schedules) }

@ -21,7 +21,7 @@ RSpec.describe RecipientSchedule, type: :model do
recipient_list_id: recipient_list.id,
question_list: question_list,
random: false,
frequency_hours: 24 * 7
frequency_hours: 24
)
end
let!(:recipient_schedule) { schedule.recipient_schedules.for(recipient).first }
@ -32,8 +32,8 @@ RSpec.describe RecipientSchedule, type: :model do
schedule: schedule,
upcoming_question_ids: '1,3',
attempted_question_ids: '2',
last_attempt_at: 1.day.ago,
next_attempt_at: 1.day.ago + (60 * 60 * schedule.frequency_hours)
last_attempt_at: Date.today + (60 * 60 * schedule.frequency_hours),
next_attempt_at: 1.day.from_now + (60 * 60 * schedule.frequency_hours)
)
end
@ -73,7 +73,21 @@ RSpec.describe RecipientSchedule, type: :model do
it 'should not do anything' do
expect(attempt).to be_nil
end
end
describe 'right before a weekend' do
before :each do
friday_time = ActiveSupport::TimeZone["UTC"].parse('2017-04-21T20:00:00')
Timecop.freeze()
recipient_schedule.update_attributes(next_attempt_at: friday_time)
end
let!(:attempt) { recipient_schedule.attempt_question }
it 'should schedule the next attempt for after the weekend' do
next_weekday_time = ActiveSupport::TimeZone["UTC"].parse('2017-04-24T20:00:00')
expect(recipient_schedule.reload.next_attempt_at).to eq(next_weekday_time)
end
end
describe 'with an opted in recipient' do
@ -103,10 +117,11 @@ RSpec.describe RecipientSchedule, type: :model do
it 'should update next_attempt_at' do
now = DateTime.now
date = ActiveSupport::TimeZone["UTC"].parse(now.strftime("%Y-%m-%dT16:00:00%z"))
time = date.to_time.to_i + (60 * 60 * 24 * 7)
date = ActiveSupport::TimeZone["Eastern Time (US & Canada)"].parse(now.strftime("%Y-%m-%dT16:00:00%z"))
date += 1.day
date += 1.day if date.on_weekend?
expect(recipient_schedule.next_attempt_at.to_i).to eq(time)
expect(recipient_schedule.next_attempt_at).to eq(date.to_time)
end
end
end

Loading…
Cancel
Save